#include "windows.h" 
#include "stdio.h" static int ii=0; LRESULT CALLBACK WinSunProc( 
HWND hwnd, 
    UINT uMsg, 
WPARAM wParam, 
LPARAM lParam) 
; int WINAPI WinMain( 
  HINSTANCE hInstance, 
  HINSTANCE HPrevInstance, 
  LPSTR     lpCmdLine, 
  int nCmdShow) 

WNDCLASS wndcls; wndcls.cbClsExtra=0; 
wndcls.cbWndExtra=0; 
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); 
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); 
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); 
wndcls.hInstance=hInstance; 
wndcls.lpfnWndProc=WinSunProc; 
wndcls.lpszClassName="Weixin2003"; 
wndcls.lpszMenuName=NULL; 
wndcls.style=CS_HREDRAW|CS_VREDRAW; 
RegisterClass(&wndcls);   
HWND hwnd; 
hwnd=CreateWindow("Weixin2003","Windows 程序设计", WS_OVERLAPPEDWINDOW, 
300,200,600,400,NULL,NULL,hInstance,NULL); 
ShowWindow(hwnd,SW_SHOWNORMAL); 
UpdateWindow(hwnd); MSG msg; 
while(GetMessage(&msg,NULL,0,0)) 

  TranslateMessage(&msg); 
  DispatchMessage(&msg); 

   return 0; 
} LRESULT CALLBACK WinSunProc( 
HWND hwnd, 
    UINT uMsg, 
WPARAM wParam, 
LPARAM lParam 


switch(uMsg)//,uMsg 为标识的消息 
{ case WM_LBUTTONDOWN: 
          InvalidateRect(hwnd,NULL,0); 
          UpdateWindow(hwnd); 
          break; 
case WM_PAINT:     
         HDC  hDC; 
         PAINTSTRUCT ps; 
        hDC=BeginPaint(hwnd,&ps); 
        if(ii==0)   
        { 
          TextOut(hDC,220,170,"ii==0",strlen("ii==0")); 
          ii++; 
        } 
        else if(ii==1) 
        { 
          TextOut(hDC,220,170,"ii==1",strlen("ii==1")); 
          ii++; 
        } 
       else if(ii==2) 
        { 
          TextOut(hDC,220,170,"ii==2",strlen("ii==2")); 
          ii++; 
        } 
       else if(ii==3) 
        { 
          TextOut(hDC,220,170,"ii==3",strlen("ii==3")); 
           ii++; 
        } 
       else if(ii==4) 
       { 
          TextOut(hDC,220,170,"ii==4",strlen("ii==4")); 
           ii++; 
        } 
       else if(ii==5) 
       { 
         TextOut(hDC,220,170,"ii==5",strlen("ii==5")); 
         ii++; 
       } 
       else if(ii==6) 
       { 
         TextOut(hDC,220,170,"ii==6",strlen("ii==6")); 
         ii++; 
      } 
      else 
      { 
        TextOut(hDC,220,170,"ii很大",strlen("ii很大")); 
      } 
      EndPaint(hwnd,&ps); 
      break; 
case  WM_CLOSE: 
   if(IDYES==MessageBox(hwnd,"真的结束程序","weixin",MB_YESNO)) 
   { 
    DestroyWindow(hwnd); 
   } 
   break; 
case WM_DESTROY: 
        PostQuitMessage(0); 
        break; 
default: 
       return DefWindowProc(hwnd,uMsg,wParam,lParam); 

return 0; 

我的程序的原意是: 
每点击一下左键,窗口上的显示增一,一次为ii==0,ii==1,ii==2,ii==3,ii==4,ii==5,ii==6;ii很大 但是实际的是: 依次点击7次,中间不是ii=0,ii==1,ii=2,ii=3,ii=4,ii=5,这个顺序,而是有间隔,比如ii=0,ii==1,ii=2,ii=4,ii=5,ii=6;或ii=0,ii==1,ii=3,ii=4,ii=5,真奇怪

解决方案 »

  1.   

    Seems no problem :)No need to call UpdateWindow()/////
    case WM_LBUTTONDOWN: 
              InvalidateRect(hwnd,NULL,0); 
              UpdateWindow(hwnd); ====>
    case WM_LBUTTONDOWN: 
              InvalidateRect(hwnd,NULL,0); 
              // UpdateWindow(hwnd); 
      

  2.   

    Problems:1) InvalideRect() will send a WM_PAINT msg, so no need to call UpdateWindow;
    2) You've increase the variable ii in the WM_PAINT msg. But remember, other actions
    you don't explicity treat will also send a WM_PAINT, for example resizing your
    application window, so ii may increase even if you don't click the left button of the mouse . This maybe the reason causing your result. :)Try this://============================
    LRESULT CALLBACK WinSunProc( 
    HWND hwnd, 
        UINT uMsg, 
    WPARAM wParam, 
    LPARAM lParam 


    switch(uMsg)//,uMsg 为标识的消息 
    { case WM_LBUTTONDOWN: 
              InvalidateRect(hwnd,NULL,0); 
     ii++;
     // UpdateWindow(hwnd); 
              break; 
    case WM_PAINT:     
             HDC  hDC; 
             PAINTSTRUCT ps; 
             hDC=BeginPaint(hwnd,&ps); 
    char text[100];
    sprintf(text, "ii==%d", ii);
       TextOut(hDC,220,170,text,strlen(text)); 
            
          EndPaint(hwnd,&ps); 
          break; 
    case  WM_CLOSE: 
       if(IDYES==MessageBox(hwnd,"真的结束程序","weixin",MB_YESNO)) 
       { 
        DestroyWindow(hwnd); 
       } 
       break; 
    case WM_DESTROY: 
            PostQuitMessage(0); 
            break; 
    default: 
           return DefWindowProc(hwnd,uMsg,wParam,lParam); 

    return 0; 

      

  3.   

    我的意思是,
    想试一下,Invalidate()产生一条消息,而Updatewindow()是发送这个消息(Invalidate()产生的这条消息),用你的方法试不出来!!
      

  4.   

    InvalidateRect(hwnd,NULL,0) (note the NULL parameter) will cause the system to send a WM_PAINT msg to the window (hwnd). UpdateWindow doesn't send the msg generated by the InvalideRect. It just send
    a WM_PAINT msg to the window!! Try the code above, you will see what you want.