为什么着段自绘BUTTON代码可以编译,但是就达到不了自己要的效果

类别:VC语言 点击:0 评论:0 推荐:
*----------------------------------------
   BTNLOOK.C -- Button Look Program
                (c) Charles Petzold, 1998
  ----------------------------------------*/

#include <windows.h>


#define NUM (sizeof button / sizeof button[0])

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("BtnLook") ;
     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 ;
    
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
    
     hwnd = CreateWindow (szAppName, TEXT ("Button Look"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, 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 HWND  hwndButton ;
     static RECT  rect ;
     BOOL InRect=FALSE;
     POINT pt;
     RECT rt;
     DRAWITEMSTRUCT *lpdrawitem;
      HBITMAP bmp;
     HDC dcMem;  
     HBRUSH hBrushStatic,hBrushStaticTemp;
    
    
    
     static int   cxChar, cyChar,cxClient,cyClient ;
;
    
    
     switch (message)
     {
     case WM_CREATE :
          cxChar = LOWORD (GetDialogBaseUnits ()) ;
          cyChar = HIWORD (GetDialogBaseUnits ()) ;
          
          
               hwndButton = CreateWindow ( TEXT("button"),
                                   "i love you",
                                   WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
                                   500,500,
                                   100,24,
                                   hwnd, (HMENU) 0,
                                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
          return 0 ;

     case WM_SIZE :
         cxClient=LOWORD(lParam);
         cyClient=HIWORD(lParam);
         MoveWindow(hwndButton,cxClient/2,cyClient/2,100,24,TRUE);
          
          return 0 ;
     case WM_MOUSEMOVE:
          
          pt.x=LOWORD(lParam);
          pt.y=HIWORD(lParam);
          rt.left=cxClient/2;
          rt.right=cyClient/2;
          rt.top=cxClient/2+100;
          rt.bottom=cyClient/2+24;
    
          if(InRect==FALSE)
          {
              if(PtInRect(&rt,pt))
              {
                  InRect=TRUE;
                
                    InvalidateRect(hwndButton,NULL,TRUE);
              }
          }
          
          if(InRect==TRUE)
          {
             if(!PtInRect(&rt,pt))
             {
                  InRect=FALSE;
                  InvalidateRect(hwndButton,NULL,TRUE);
             }
          }
        
            
          
          return 0;

          
          

      
          
     case WM_DRAWITEM :
         lpdrawitem=(LPDRAWITEMSTRUCT)lParam;
        
         hBrushStatic=CreateSolidBrush(RGB(255,255,255));
         FillRect(lpdrawitem->hDC,&lpdrawitem->rcItem, hBrushStatic);
          hBrushStaticTemp=CreateSolidBrush(RGB(235,246,111));
         SelectObject(lpdrawitem->hDC,hBrushStaticTemp);
         Ellipse(lpdrawitem->hDC,lpdrawitem->rcItem.left,lpdrawitem->rcItem.top,lpdrawitem->rcItem.right,lpdrawitem->rcItem.bottom);
         DeleteObject(hBrushStaticTemp);

        
        
         if(InRect==TRUE)
         {
               bmp = (HBITMAP)LoadImage((HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
                    "iloveyou.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
               dcMem = CreateCompatibleDC(lpdrawitem->hDC);
                
               SelectObject(dcMem, bmp);
               BitBlt(lpdrawitem->hDC, 0, 0, 100, 100, dcMem, 0, 0, SRCCOPY);
               PlaySound(TEXT("tujunyan.wav"),NULL,SND_FILENAME | SND_ASYNC);
               ReleaseDC(hwnd, dcMem);
         }
         return 0;

          
     case WM_DESTROY :
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

本文地址:http://com.8s8s.com/it/it67.htm