Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自定义控件上的文本_C++_Winapi - Fatal编程技术网

C++ 自定义控件上的文本

C++ 自定义控件上的文本,c++,winapi,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(HI

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

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

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);
    }
您可以在
CustProc
函数中捕获消息,然后自己绘制文本

可以通过调用获取图形上下文,绘制文本,并通过调用关闭图形上下文。您可以使用该函数绘制文本。以下是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.   
    } 
} 
完整示例。

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

可以通过调用获取图形上下文,绘制文本,并通过调用关闭图形上下文。您可以使用该函数绘制文本。以下是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.   
    } 
} 

完整示例。

如果没有额外功能,您可以捕获WM_PAINT消息并使用GDI自己绘制文本。如果没有额外功能,您可以捕获WM_PAINT消息并使用GDI自己绘制文本。这给了我一个白色背景,你知道如何使它与背景融合吗?看看SetBkMode(hdc,透明):(在googlesearch-away上)这是一个非常基本的例子。我建议您使用DrawText而不是TextOut,因为它提供了更多的控制。此外,您应该创建第二个设备上下文,并在该上下文上绘制所有图形,然后将其bitblt到主设备上下文。这将大大有助于防止自定义控件中的闪烁。这给了我一个白色背景,你知道如何使其与背景混合吗?看看SetBkMode(hdc,透明):(在google search away上)这是一个非常基本的示例。我建议您使用DrawText而不是TextOut,因为它提供了更多的控制。此外,您应该创建第二个设备上下文,并在该上下文上绘制所有图形,然后将其bitblt到主设备上下文。这将对防止自定义控件中的闪烁起到很大作用。