下面是我的源程序,如果我在运行这个程序的时候更改A.TXT的内容,要最小化或者拖动后,显示的文字才会作出更新,我原本设计的是鼠标左键一点击就会重新读入a.txt的内容,并且在窗口作出更新
#include   "windows.h" 
#include   "stdio.h" LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_INFORMATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="WGH_normal_window";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd; hwnd=CreateWindow("WGH_normal_window","GZS32  STRDP自动检查程序",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0,0,
600,400,
NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
 
// SetTimer(hwnd,2000,1000,NULL); MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
PAINTSTRUCT   ps; 
static   char*   p=0; 
switch(uMsg)
{
UpdateWindow(hwnd);
case WM_LBUTTONDOWN:

case WM_PAINT:

FILE*   fp=   fopen( "a.txt", "r");//打开文件的名字
fseek(   fp   ,   0   ,   SEEK_END   ); 
long len   =   ftell(   fp   ); 
fseek(fp,0,SEEK_SET   ); 
p   =   new   char[len]; 
fread(p,1,len,fp); 
fwrite(p,1,len,fp); 
        fclose(fp);  HDC   hdc   =   BeginPaint(hwnd,   &ps); 
RECT   rt; 
GetClientRect(hwnd,   &rt); 
DrawText(hdc,   p,   strlen(p),   &rt,   DT_LEFT); 
EndPaint(hwnd,   &ps);
ReleaseDC(hwnd,hdc);

        break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
PostMessage(hwnd,WM_PAINT,NULL,NULL);
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

解决方案 »

  1.   

    把读文件那段代码放到 “case WM_LBUTTONDOWN:”下,然后调用::PostMessage( m_hWnd, WM_PAINT, 0, 0 );
      

  2.   

    原因是WM_PAINT消息无论是Post还是Send都无效,因为没有无效矩形区。BeginPaint只有在有有效矩形区时绘制才会生效,而这个无效矩形区必须由Invalidate或者InvalidateRect函数才能创建把PostMessage(hwnd,WM_PAINT,NULL,NULL); 改为Invalidate(hwnd)应该就可以了。但是不要在WM_PAINT里读文件,否则这个消息会非常多,文件读写代价太高。应该在一开始(WM_CREATE消息)就把所有文件读号