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

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

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);
    }

最佳答案

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

您可以通过调用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

关于c++ - 自定义控件上的文字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6010207/

10-11 00:54