关于Windows更新窗口内容的问题(作为一个实验,效果很明显)

时间:2021-11-09 04:16:38

Windows中的窗口在特定情况下会由系统进行重绘,如无效区域重新显现时,,会向窗口的处理过程发送VM_PAINT消息,但是,可能还有Windows自己的更新窗口处理,如在下面的代码中,将击键显式地转换为VM_PAINT消息,让窗口过程处理,但是运行的结果却是,,击键后窗口没有马上重绘(显示字体没有改变),而是在下次重绘时才改变字体,但是如果改变为一个UpdateWindow()函数时,就会马上改变字体。猜想UpdateWindow(),InvaliddateRect()和Windows系统调用都有更新窗口处理。

#include<windows.h>
//LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;
 RECT rect;
 static int a=0;
 switch(message)
 {
 case WM_CREATE:
  return 0;
 case WM_PAINT:
  hdc=BeginPaint(hwnd,&ps);
  GetClientRect(hwnd,&rect);
  int c;
  if(a==0)
   DrawText(hdc,TEXT("Liu Wangsheng !"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  else
   c=DrawText(hdc,TEXT("HELLO HELLO!"),-1,&rect,DT_SINGLELINE);
  EndPaint(hwnd,&ps);
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
 case WM_KEYDOWN:
  switch(wParam)
  {
  case VK_UP:
   a=1;
   //SendMessage(hwnd,WM_PAINT,0,0);
   InvalidateRect(hwnd,NULL,TRUE);
   return 0;
  case VK_DOWN:
   PostQuitMessage(0);
   return 0;
  }
  return 0;
 }
 return DefWindowProc(hwnd,message,wParam,lParam);
 //return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR  lpCmdLine,int nCmdShow)
{
  // TODO: Place code here.
 static TCHAR szAppName[]=TEXT("LWS");
 HWND hwnd;
 MSG msg;
 WNDCLASS wndclass;

wndclass.style=CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hInstance;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 wndclass.lpszMenuName=NULL;
 wndclass.lpszClassName=szAppName;
 RegisterClass(&wndclass);
 hwnd=CreateWindow(szAppName,
  TEXT("hello!"),
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  NULL,
  NULL,
  hInstance,
  NULL);
 ShowWindow(hwnd,nCmdShow);
 UpdateWindow(hwnd);
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return msg.wParam;
}

关于Windows更新窗口内容的问题(作为一个实验,效果很明显)