2013-02-26 23 views
0

我想在主窗口中註冊一個靜態控件,然後用一些文本填充它。將文本添加到在運行時註冊的靜態控件

下面是完整的代碼:

#include <windows.h> 
#include "resource.h" 
#include <commctrl.h> 
void RegisterCommonControls(); 
#pragma comment(lib, "comctl32.lib") 

HINSTANCE hInstance; 
HINSTANCE hPrevInstance; 
int nCmdShow; 

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (Msg) 
    { 
     case WM_DESTROY: 
      PostQuitMessage(0); 
      return 0; 
     default: 
      return DefWindowProc(hWnd, Msg, wParam, lParam); 
    } 
} 

LRESULT CALLBACK LblStateProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) 
{ 
    return DefWindowProc(hWnd, Msg, wParam, lParam); 
} 

void RegisterCommonControls() 
{ 
    INITCOMMONCONTROLSEX iccex; 

    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX); 
    iccex.dwICC = ICC_TREEVIEW_CLASSES | ICC_LISTVIEW_CLASSES; 
    InitCommonControlsEx(&iccex); 
} 

bool RegisterWindow(void) 
{ 
    if (hPrevInstance) 
     return false; 
    WNDCLASS wc; 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = MainWndProc; 
    wc.cbClsExtra = wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = LoadIcon(0, IDI_APPLICATION); 
    wc.hCursor = LoadCursor(0, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); 
    wc.lpszMenuName = MAKEINTRESOURCE(IDC_WIN_API); 
    wc.lpszClassName = L"Class1"; 
    if (!RegisterClass(&wc)) 
     return false; 
    return true; 
} 

bool RegisterEdit(void) 
{ 
    if (hPrevInstance) 
     return false; 
    WNDCLASS wc; 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = LblStateProc; 
    wc.cbClsExtra = wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = wc.hCursor = NULL; 
    wc.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = L"STATIC"; 
    if (!RegisterClass(&wc)) 
     return false; 
    return true; 
} 

int CALLBACK wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) 
{ 
    const int size = 600; 
    MSG msg; 
    RegisterWindow(); 
    HWND hMainWnd = CreateWindow(L"Class1", L"Main Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, size, size, 0, 0, hPrevInstance, NULL); 
    RegisterCommonControls(); 
    RegisterEdit(); 
    HWND hLblState = CreateWindow(L"STATIC", L"1", WS_CHILD | WS_VISIBLE | SS_SIMPLE | SS_OWNERDRAW, 200, 200, 100, 50, hMainWnd, 0, NULL, NULL); 
    ShowWindow(hMainWnd, nCmdShow); 
    UpdateWindow(hMainWnd); 
    SendMessage(hLblState, WM_SETTEXT, NULL, (LPARAM)L"Rectangle"); 
    while(GetMessage(&msg, 0, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return 0; 
} 

所以,我得到一個白色的「靜態」窗口在主窗口內(控制是在正確的座標),無文本中(不是「1」,也不是「矩形「)。我的代碼中有什麼壞處?

回答

1

您正在註冊名爲「STATIC」的窗口類。這優先於內置的「STATIC」窗口類,因此您的窗口不像靜態控件。

要修復它,請不要註冊名爲「STATIC」的窗口類。標準的將被用來代替。

+0

那麼我該如何註冊一個標準控件?在這種情況下如何填寫wc? – alexdelphi 2013-02-26 18:38:14

+0

你沒有。基本控件將自動爲您註冊。其他控件需要InitCommonControlsEx。 – arx 2013-02-26 18:53:46

+0

我明白了。以及如何真正設置編輯中的文字?我試着將WM_SETTEXT發送到控件並使用SetWindowText。兩者都失敗了,因爲我認爲他們將文本寫入到控件的窗口文本中,在我的示例中它不可見。 – alexdelphi 2013-02-26 19:12:07

相關問題