c++ - 将文本放入运行时注册的静态控件

标签 c++ winapi static

我想在主窗口中注册一个静态控件,然后用一些文本填充它。

完整代码如下:

#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”也不是“Rectangle”)。我的代码中有什么问题?

最佳答案

您正在注册一个名为“STATIC”的窗口类。这优先于内置的“STATIC”窗口类,因此您的窗口的行为不像静态控件。

要修复它,请不要注册名为“STATIC”的窗口类。将改用标准的。

关于c++ - 将文本放入运行时注册的静态控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095961/

相关文章:

c++ 带有静态变量的静态函数的奇怪行为

c++ - 从同一索引的两个不同数组中获取数据

c++ - 有没有办法知道用户是否在控制面板中启用了 "single click to open an item"?

c# - 需要对象引用才能访问非静态成员

c# - 在 C# 中查找鼠标的速度

delphi - 如何在 Delphi 中以编程方式获取 GDI 和用户对象计数?

asp.net - ASP.NET 中静态对象的范围

c++ - 关键字 "virtual"是否传递给中间基类?

计算值集合的标量的函数的 C++11esque 签名

c++ - SFINAE:检测是否存在需要显式专门化的模板函数