自定义控件上的文字

时间:2011-05-15 18:04:44

标签: c++ winapi

我已阅读并阅读,试图找到如何将文本放在自定义控件上。我找到了东西,但没有一个干净简单。

那么如何在自定义控件上绘制文本?这是代码......

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK CustProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam) ;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
wchar_t * windowname = L"window Class";
wchar_t * cust = L"custctrl";

WNDCLASS wc = {0};
wc.lpszClassName = windowname;
wc.lpfnWndProc = WindowProc;
RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
    0,
    windowname,
    L"app",
     WS_VISIBLE | WS_THICKFRAME| WS_OVERLAPPEDWINDOW  ,
    50, 50,
    500, 500,
    NULL, 
    NULL,
    hInstance,
    NULL
    );



    WNDCLASS button = {0};
button.lpfnWndProc = CustProc;
button.lpszClassName = cust;
button.hInstance = hInstance;
button.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
button.hCursor = LoadCursor(NULL, IDC_HAND);
RegisterClass(&button);



     HWND click =   CreateWindowEx(
    WS_EX_CLIENTEDGE,
    cust,
    L"Custom Control", //doesnt show up on the window, not to my suprise
    WS_VISIBLE | WS_CHILD ,
    0, 0,
    500, 500,
    hwnd,
    NULL,
    hInstance,
    NULL
    );
  //all the rest...
}

LRESULT CALLBACK CustProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam) {
switch(uMsg) {
    case WM_CREATE:
    SetWindowText(hwnd, L"button"); //also doesn't work, also not to my suprise
case WM_LBUTTONDOWN: {
MessageBox(hwnd, L"you clicked the custom button", L"cool", 0); // works fine
break;
                         }
return  0;
}
    return  DefWindowProc(hwnd, uMsg, wparam, lparam);
    }

1 个答案:

答案 0 :(得分:3)

您可以在CustProc函数中捕获WM_PAINT消息并自行绘制文本。

您可以通过调用BeginPaint来获取绘图上下文,绘制文本并通过调用EndPaint来关闭绘图上下文。您可以使用TextOut功能绘制文本。以下是MSDN的一个示例:

LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    PAINTSTRUCT ps; 
    HDC hdc; 

    switch (message) 
    { 
        case WM_PAINT: 
            hdc = BeginPaint(hwnd, &ps); 
            TextOut(hdc, 0, 0, "Hello, Windows!", 15); 
            EndPaint(hwnd, &ps); 
            return 0L; 

        // Process other messages.   
    } 
} 

完整示例here