简体   繁体   English

win32:WM_PAINT调用但不应该这样!

[英]win32: WM_PAINT calls but not supposed to be!

I have a problem with WM_PAINT. 我对WM_PAINT有问题。 Basically I want WM_PAINT to be called after a user WM_COMMAND, but for some reason it's called anyway in the main function. 基本上,我希望在用户WM_COMMAND之后调用WM_PAINT,但是由于某种原因,它总是在main函数中被调用。

 case WM_PAINT:
    {
     createFont();
     PAINTSTRUCT ps;
     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,0,s.c_str(),s.length());
     EndPaint(hWnd,&ps);

     DeleteObject(hbruzh);
     DeleteObject(hdz);

     break;
    }




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("uTest"),
       WS_OVERLAPPEDWINDOW,
       300,
       200,
       450,
       300,
       NULL,
       NULL,
       hInstance,
       NULL);
 ShowWindow(hWnd,nCmdShow);

 MSG msg;
 while (GetMessage(&msg, NULL,0,0)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);

 }


 return msg.wParam;
}

According to MSDN, WM_PAINT is only called automatically after either UpdateWindow() or ReDrawWindow(), or when you SendMessage with it as message. 根据MSDN,WM_PAINT仅在UpdateWindow()或ReDrawWindow()之后或在您将SendMessage与消息一起发送时才自动调用。 I do none, however. 我什么也没做。 I basically only want to call WM_PAINT after a user interaction, not before... is there any way to fix this? 我基本上只想在用户交互后调用WM_PAINT,而不是在...之前有什么办法解决此问题? What is causing this? 是什么原因造成的? (I guess its some bizare side-effect I cant find documentation for >.<) (我想我找不到>。<的文档有一些缺点。)

WM_PAINT is called any time your window needs to be redrawn. 每当需要重绘窗口时,都会调用WM_PAINT。 That's what it's for. 那就是它的目的。 Showing the window, resizing the window, restoring the window from minimized state, bringing the window to the front after it's been covered by another window, minimizing another app that was covering up your window... these are just a few of the things that will send a WM_PAINT. 显示窗口,调整窗口大小,将窗口从最小化状态还原,在被另一个窗口覆盖后将窗口置于最前面,最小化另一个覆盖窗口的应用程序……这些只是其中的一些事情将发送一个WM_PAINT。

I think you're trying to use WM_PAINT for something it's not intended for. 我认为您正在尝试将WM_PAINT用于不适合的用途。

What is causing this? 是什么原因造成的?

My guess is that when the user selects the menu item from the menu, the act of displaying the menu has covered part of the client window. 我的猜测是,当用户从菜单中选择菜单项时,显示菜单的动作已覆盖了客户端窗口的一部分。

So when the menu is finally remove, a *WM_PAINT* message is generated to recreated that missing portion of the client window. 因此,当最终删除菜单时,将生成* WM_PAINT *消息以重新创建客户端窗口的缺失部分。

According to MSDN, WM_PAINT is only called automatically after either UpdateWindow() or ReDrawWindow(), or when you SendMessage with it as message. 根据MSDN,WM_PAINT仅在UpdateWindow()或ReDrawWindow()之后或在您将SendMessage与消息一起发送时才自动调用。

It's more complicated than that. 比这更复杂。 WM_PAINT might be generated almost any time; WM_PAINT几乎可以在任何时间生成; for example see also Synchronous and Asynchronous Drawing . 例如,另请参见同步和异步绘图

I don't think you can prevent a WM_PAINT. 我认为您无法阻止WM_PAINT。 You can: 您可以:

  • Force an immediate WM_PAINT (eg by calling Update ) 强制立即执行WM_PAINT(例如,通过调用Update
  • Try to combine/delay several paints into one (eg by using several calls to InvalidateRect ) 尝试将几种绘画组合/延迟成一幅(例如,通过多次调用InvalidateRect

Instead of preventing WM_PAINT, you should concentrate on avoiding/fixing whatever "side effect" you say you're getting when WM_PAINT is processed. 而不是阻止WM_PAINT,您应该集中精力避免/修复在处理WM_PAINT时说的“副作用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM