WINCE 自绘位图按钮

//TITLE:
//      自绘按钮的实现
//AUTHOR:
//      norains
//DATE:
//      Saturday  12-January-2008
//Environment:
//      VS2005 + SDK-WINCE5.0-MIPSII   
//      EVC + SDK-WINCE5.0-MIPSII 
//========================================================================

    自绘按钮的实现并不难,只不过如果是在非MFC类库中实现,却是显得有点麻烦,或是说,更需要一点小技巧.
   
    文章打算在CMainWnd窗口类中实现一个自绘按钮,为了方便讲解以及突出重点,CMainWnd直接派生于CWndBase类.关于CWndBase类的信息,可以在此找到:http://blog.csdn.net/norains/archive/2008/01/12/2040109.aspx
   
    首先我们先要创建一个按钮,我将它命名为m_hBnExit:
 

    m_hBnExit CreateWindowEx(WS_EX_TOPMOST,
                                TEXT("BUTTON"),
                                TEXT(""),
                                BS_PUSHBUTTON  WS_VISIBLE WS_CHILD BS_OWNERDRAW ,
                                POS_EXIT.left,
                                POS_EXIT.top,
                                POS_EXIT.right POS_EXIT.left,
                                POS_EXIT.bottom POS_EXIT.top,
                                m_hWnd,
                                (HMENU)IDC_BTN_EXIT,
                                m_hInst,
                                NULL);


  这里有点需要注意,因为我们需要重新绘制按钮,所以BS_OWNERDRAW风格一定需要设置.一旦设置该风格,CMainWnd窗口的消息处理函数就能接收到按钮的WM_DRAWITEM消息:

        LRESULT CMainWnd::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
        {
          ...
          
            switch(wMsg)
            {
                case WM_DRAWITEM:
                if(((LPDRAWITEMSTRUCT)lParam)->CtlID == IDC_BTN_EXIT)
                {
                    //TODO something;
                }
                
                ...
                
            }
        
            ...
        }


  接下来需要做的就很简单了,我们只需要在接收到WM_DRAWITEM消息时,判断是否为退出按键的ID号(在本文例子中为IDC_BTN_EXIT),如果是的话就重新绘制.为此,我们定义一个OnDrawItemBtnExit()函数,用来绘制该按钮:

        void CMainWnd::OnDrawItemBtnExit(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
        {
            LPDRAWITEMSTRUCT lpdis (LPDRAWITEMSTRUCT) lParam;    
        
          //读取按钮的图片
            HANDLE hBmp LoadImage(m_hInst,MAKEINTRESOURCE(IDB_EXIT),IMAGE_BITMAP,0,0,0);
            if(hBmp == NULL)
            {
                return ;
               
        
            //Draw the button
            HDC hdcBmp CreateCompatibleDC(lpdis->hDC);
            HGDIOBJ hOldSel SelectObject(hdcBmp,hBmp);
        
            if((lpdis->itemState ODS_SELECTED && !(lpdis->itemState ODS_DISABLED))
            {
             //按下状态
                TransparentBlt(lpdis->hDC,
                                lpdis->rcItem.left,
                                lpdis->rcItem.top,
                                lpdis->rcItem.right lpdis->rcItem.left,
                                lpdis->rcItem.bottom lpdis->rcItem.top,
                                hdcBmp,
                                IMG_EXIT_PUSH.left,
                                IMG_EXIT_PUSH.top,
                                IMG_EXIT_PUSH.right IMG_EXIT_PUSH.left,
                                IMG_EXIT_PUSH.bottom IMG_EXIT_PUSH.top,
                                DEFAULT_TRANSPARENT_COLOR);
            }
            else if(lpdis->itemState ODS_DISABLED)
            {
             //无效状态
                TransparentBlt(lpdis->hDC,
                                    lpdis->rcItem.left,
                                    lpdis->rcItem.top,
                                    lpdis->rcItem.right lpdis->rcItem.left,
                                    lpdis->rcItem.bottom lpdis->rcItem.top,
                                    hdcBmp,
                                    IMG_EXIT_DISABLE.left,
                                    IMG_EXIT_DISABLE.top,
                                    IMG_EXIT_DISABLE.right IMG_EXIT_DISABLE.left,
                                    IMG_EXIT_DISABLE.bottom IMG_EXIT_DISABLE.top,
                                    DEFAULT_TRANSPARENT_COLOR);
            }
            else
            {
              //正常状态
                TransparentBlt(lpdis->hDC,
                                lpdis->rcItem.left,
                                lpdis->rcItem.top,
                                lpdis->rcItem.right lpdis->rcItem.left,
                                lpdis->rcItem.bottom lpdis->rcItem.top,
                                hdcBmp,
                                IMG_EXIT_ENABLE.left,
                                IMG_EXIT_ENABLE.top,
                                IMG_EXIT_ENABLE.right IMG_EXIT_ENABLE.left,
                                IMG_EXIT_ENABLE.bottom IMG_EXIT_ENABLE.top,
                                DEFAULT_TRANSPARENT_COLOR);
            }
        
            if(hdcBmp != NULL && hOldSel != NULL)
            {
                SelectObject(hdcBmp,hOldSel);    
                DeleteDC(hdcBmp);
        
                hdcBmp NULL;
                hOldSel NULL;
            }
        
            if(hBmp != NULL)
            {
                DeleteObject(hBmp);
                hBmp NULL;
               
        
        }

你可能感兴趣的:(WinCE)