gpt4 book ai didi

c++ - win32 : TextOut not being displayed

转载 作者:行者123 更新时间:2023-11-28 08:24:29 30 4
gpt4 key购买 nike

我最近让我的主窗口使用 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;

}

最佳答案

只能使用BeginPaint/EndPaint 来响应WM_PAINTWM_PAINT 是进行这种绘图的合适位置。

当窗口的一部分“无效”时,Windows 调用 WM_PAINT。例如,如果您恢复窗口,或者在将窗口移开或调整窗口大小后窗口的一部分变得可见。

当您需要手动更新显示时,通过调用 InvalidateRect 使您需要重绘的区域“无效”(这会告诉 Windows 窗口中需要重绘的区域)。

常见的是使整个窗口无效,而不是计算要绘制区域的实际像素完美边界。

关于c++ - win32 : TextOut not being displayed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4519783/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com