如题,在某个格子里点击鼠标左键的时候,有的响应绘制,有的却没反应,为什么呢?
代码如下:
#include <windows.h>
#include <math.h>
        
#define NUM 1000
#define DIVISIONS 10        
#define TWOPI      (2 * 3.14159)char szAppName[]="Hello World";int fun1(int yBlock,int j)
{
 return
  (int)(yBlock / 2 -sqrt(yBlock * yBlock / 4 -( yBlock/2 + j ) * ( yBlock/2 + j ) ));}int fun2(int yBlock,int j)
{
 return
  (int)(yBlock / 2 +sqrt(yBlock * yBlock / 4 -( yBlock/2 + j ) * ( yBlock/2 + j ) ));}          LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevinstance,
                   LPSTR szCmdLine, int iCmdShow)
{
 
 HWND hwnd;
 MSG msg;
 WNDCLASSEX wndclass;
 wndclass.lpfnWndProc=WndProc;
 wndclass.style=CS_DBLCLKS;
 wndclass.cbSize=sizeof(WNDCLASSEX);
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hInstance;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hbrBackground=(HBRUSH) COLOR_BACKGROUND;
 wndclass.lpszMenuName= NULL;
 wndclass.lpszClassName=szAppName;
 if (!RegisterClassEx (&wndclass))
        return 0;
  hwnd=CreateWindowEx(0,
                     szAppName,
                    "MyWindows",
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    544,
                    375,
                    HWND_DESKTOP,
                    NULL,
                    hInstance,
                    NULL);       
  ShowWindow(hwnd,iCmdShow);
  UpdateWindow(hwnd);
  while(GetMessage(&msg,NULL,0,0))
  {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
  }
  return msg.wParam;
 }
 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
 {
 static bool   fState[DIVISIONS][DIVISIONS] ={false};
        
           static int    cxBlock, cyBlock ;
        
           HDC     hdc ;
                   
           int     x, y ;
  
           PAINTSTRUCT  ps ;
        
          RECT    rect,rect2 ;
          
        
           switch (message)
        
    {
        
           case   WM_SIZE :
        
                  cxBlock = LOWORD (lParam) / DIVISIONS ;
        
                  cyBlock = HIWORD (lParam) / DIVISIONS ;
        
                  return 0 ;
        
        
        
           case WM_LBUTTONDOWN :
        
                  x = LOWORD (lParam) / cxBlock ;
        
                  y = HIWORD (lParam) / cyBlock ;
        
        
        
                  if (x < DIVISIONS && y < DIVISIONS && fState [x][y]!=true )
        
                  {
                      fState [x][y] ^= 1 ;
                      rect.left      = x * cxBlock ;
        
                      rect.top       = y * cyBlock ;
        
                      rect.right     = (x + 1) * cxBlock ;
        
                      rect.bottom    = (y + 1) * cyBlock ;
                      
                      InvalidateRect (hwnd, &rect, FALSE) ;
                  }
        
                  else                  MessageBeep (0) ;
                  
  return 0 ;
                 case   WM_PAINT :                       
                  hdc = BeginPaint (hwnd, &ps) ;
                  
 
                
                  for (x = 0 ; x < DIVISIONS ; x++)
        
                  for (y = 0 ; y < DIVISIONS ; y++)
        
              {
        
                   Rectangle (hdc, x * cxBlock, y * cyBlock,
        
                   (x + 1) * cxBlock, (y + 1) * cyBlock) ;
       
                   if (fState [x][y])
        
                 {
                                
                  int i,j;
                  
for(j=0;j>-cyBlock;j--)
                  for(i=fun1(cyBlock,j);i<fun2(cyBlock,j);i++)
                  SetPixel(hdc,x * cxBlock+i,y * cyBlock+j,0x70);
  
     
for(j=0;j>-cyBlock;j--)
                  for(i=fun1(cyBlock,j);i<fun2(cyBlock,j);i++)
                  SetPixel(hdc,x * cxBlock+i,y * cyBlock+j,0xff);   
           
 }
  }
                          EndPaint (hwnd,&ps); 
        
            return 0 ;
        
             
        
           case   WM_DESTROY :
        
                  PostQuitMessage (0) ;
        
                  return 0 ;
        
           }
        
           return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}

解决方案 »

  1.   

    寒!居然无人关注!
    不过问题自己是解决了,把SetPixel(hdc,x * cxBlock+i,y * cyBlock+j,0xff);里的y * cyBlock+j变为y * cyBlock-j即可。不过新的问题又来了。我想将两个格子同时着色,不同的颜色,如一个为红色,一个为黑色,如
                      int i,j;
                      
    for(j=0;j>-cyBlock;j--)
                      for(i=fun1(cyBlock,j);i<fun2(cyBlock,j);i++)
                      SetPixel(hdc,x * cxBlock+i,y * cyBlock+j,0x00);
      
         
    for(j=0;j>-cyBlock;j--)
                      for(i=fun1(cyBlock,j);i<fun2(cyBlock,j);i++)
                      SetPixel(hdc,(x+1) * cxBlock+i,(y+1) * cyBlock+j,0xff);当然在case WM_LBUTTONDOWN :还要加上
                          
                          fState [x+1][y+1] ^= 1 ;
                          rect2.left      = (x+1) * cxBlock ;
            
                          rect2.top       = (y+1) * cyBlock ;
            
                          rect2.right     = (x + 2) * cxBlock ;
            
                          rect2.bottom    = (y + 2) * cyBlock ;
                          
                          InvalidateRect (hwnd, &rect2, FALSE) ;
      
    代码是对第(X,Y)个和第(X+1,Y+1)个格子分别着黑色和红色,但总是着同一个颜色。应该怎样才能实现着不同的颜色呢?
      

  2.   

    up!
    give me some suggestion,please!
      

  3.   

    这个问题太简单,就像上个问题一样,自己多思考一下,解决了会更有收获。你的代码不是从程序中直接拷贝过来的吧?看着别扭。提示一下:
                          rect2.left      = (x+1) * cxBlock ;
            
                          rect2.top       = (y+1) * cyBlock ;
            
                          rect2.right     = (x + 2) * cxBlock ;
            
                          rect2.bottom    = (y + 2) * cyBlock ;
    这样定义的矩形范围是哪里?自己在纸上画一下。