将文本放到运行时注册的静态控件中
putting text to static control registered at runtime
我想在主窗口注册一个静态控件,然后用一些文本填充它。
完整代码:
#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"也不是"矩形")。我的代码有什么问题?
您正在注册一个名为"STATIC"的窗口类。这优先于内置的"STATIC"窗口类,因此您的窗口不会像静态控件那样运行。
要解决这个问题,不要注册一个名为"STATIC"的窗口类。
相关文章:
- 如何在MFC中的静态文本控件上插入图标?
- 如果我有五个 Win32 静态控件,如何使用特定的前景色设置其中一个?
- 如何检测将鼠标悬停在静态 Win32 控件上
- win32 c++ 我想在静态标签的文本之后直接插入一个 EDIT 控件
- Win32 c++ 更改透明静态控件中的文本将其置于底部
- 为什么将鼠标悬停在静态 Win32 控件上会增加内存并删除我的 GUI?
- 如何注册 ActiveX 控件
- 如何在Win32(C++)静态控件中正确渲染OpenGL
- STM_SETIMAGE不会使静态控件失效/重新绘制
- 如何在 ATL/WTL 对话框中注册自定义控件
- 如何在改变文本时重新绘制静态控件
- 在静态文本控件上设置图标
- 如何在指定的注册表值更改时在ATL ActiveX控件中获取事件通知以用作windows移动ActiveX控件
- 静态控件设置文本颜色
- 如何使图片适合于一个静态控件vc++ win32
- 如何改变静态文本控件的字体(winapi)
- 正在运行的线程中静态控件的重绘窗口
- 将文本放到运行时注册的静态控件中
- 正在将文本发送到静态控件
- 如何在“选择文件夹”对话框中为静态控件的背景着色