我最近让我的主窗口使用 WM_PAINT 编写文本,但现在我意识到这可能不是最好的消息,所以我正在尝试另一个版本;
主窗口包含一个菜单,单击菜单项时会发送 ID_FILE_PID 消息,它会构建 4 个新窗口并在主窗口中显示文本(paintEditSigns 函数)。4 个窗口工作正常,但文本根本不起作用,除非我在 main() 函数中这样做,如图所示......这到底是什么?O_O
顺便说一句:我仍然不知道为什么 StackOverflow 上的代码显示在我发布时看起来如此奇怪,这是为什么呢?
switch(message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILE_PID:
{
HWND hWndButton;
HWND hWndEdit;
HWND hWndEdit2;
HWND hWndDisplay;
// drawing the text in mainwindow
trigger=true;
// adding new windows in the mainwindow
hWndButton = CreateWindowEx(0,TEXT("BUTTON"),"Modify",WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, 170,56,80,30,hWnd,(HMENU)ID_BUTTON,hThisInstance,NULL);
hWndEdit = CreateWindowEx(0,RICHEDIT_CLASS,TEXT(""),WS_CHILD | WS_VISIBLE | WS_BORDER,
120,30,80,25,hWnd,(HMENU)ID_EDIT,hThisInstance,NULL);
hWndEdit2 = CreateWindowEx(0,RICHEDIT_CLASS,TEXT(""),WS_CHILD | WS_VISIBLE | WS_BORDER,
220,30,80,25,hWnd,(HMENU)ID_EDIT2,hThisInstance,NULL);
hWndDisplay = CreateWindowEx(0,TEXT("STATIC"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,
0,100,450,140,hWnd,(HMENU)ID_DISPLAY,hThisInstance,NULL);
UpdateWindow(hWnd);
break;
}
.....
case WM_PAINT:
{
if (trigger) {
paintEditSigns()
}
break;
}
//
// Main function
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
hThisInstance = hInstance;
LoadLibrary("Riched20.dll");
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
if(!(wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MYICON)))) {
HRESULT res = GetLastError();
}
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = TEXT("testcpp");
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
wc.lpszClassName,
TEXT("test"),
WS_OVERLAPPEDWINDOW,
300,
200,
450,
300,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd,nCmdShow);
//paintEditSigns() -- here it works, but not when in the message part
MSG msg;
while (GetMessage(&msg, NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void paintEditSigns() {
HFONT hf = createFont();
PAINTSTRUCT ps;
HWND hWnd = FindWindow(TEXT("testcpp"),TEXT("test"));
HBRUSH hbruzh = CreateSolidBrush(RGB(0,0,0));
HDC hdz = BeginPaint(hWnd,&ps);
string s = "Memory Address";
SelectBrush(hdz,hbruzh);
SelectFont(hdz,hf);
TextOut(hdz,0,100,s.c_str(),s.length());
EndPaint(hWnd,&ps);
DeleteObject(hbruzh);
}
HFONT createFont() {
HDC hdc;
long lfHeight;
hdc = GetDC(NULL);
lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
HFONT hf = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "MS Sans Serif");
return hf;
}