天天看點

MFC Button控件自繪制----詳細講解

VC下的界面着實難看 有時候我們不得不自己進行控件的繪制 以前 一直不了解最近再次看了學了一遍終于明白了一點   與大家分享下...       需要源代碼的Q我 尋找一起學VC的朋友 

   比如說

我們要改變一個編輯框的背景 我們響應WM_CTLCOLOR函數 進行OnCtlColor進行修改但是對與 Button控件就不行了 ..      這時候我們要進行自繪制    相關函數   virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );

  要覆寫掉這個虛函數  并且類型要設定為 BS_OWNERDRAW 這時候 放 應用程式進行初始化界面的時候 會進入我們的

  DrawItem函數 進行控件的繪制   是以說 自繪制 就2個步驟    

  ASSERT 宏       Evaluate an expression and generate a debug report when the result is FALSE (debug version only).       計算表達是當結果是false的時候生成調試報告 (僅僅在debug下 )  

1. 類型要設定為 BS_OWNERDRAW

2.重寫 virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );函數   代碼就我們自己設計了

 重繪需要的函數  注意 都是SDK中的函數       BOOL DrawFrameControl(   //這個函數畫一個指定類型控件的架構   HDC hdc,     // handle to device context  DC   LPRECT lprc, // bounding rectangle   舉行區域    UINT uType,  // frame-control type   類型   UINT uState  // frame-control state  狀态 具體看MSDN  );   

   int DrawText(   //在指定的矩形區域 輸出文本   HDC hDC,          // handle to DC   LPCTSTR lpString, // text to draw   int nCount,       // text length   LPRECT lpRect,    // formatting dimensions   UINT uFormat      // text-drawing options );

COLORREF SetTextColor(  //設定指定DC的文本顔色    HDC hdc,           // handle to DC   COLORREF crColor   // text color );

int FillRect(  // 用給定畫刷填充矩形區域    HDC hDC,           // handle to DC   CONST RECT *lprc,  // rectangle   HBRUSH hbr         // handle to brush );

int SetBkMode(    //設定背景模式   TRANSPARENT透明    HDC hdc,      // handle to DC   int iBkMode   // background mode );

typedef struct tagDRAWITEMSTRUCT {    //具體看MSDN    UINT      CtlType;      //控件類型    UINT      CtlID;    //id   UINT      itemID;    //項ID     UINT      itemAction;  行為       UINT      itemState;  //狀态    HWND      hwndItem;    //控件句柄   HDC       hDC;    //dc句柄    RECT      rcItem;   //舉行區域    ULONG_PTR itemData;    } DRAWITEMSTRUCT  ;

          Draw3dRect       (    LPCRECT lpRect,        COLORREF clrTopLeft,    COLORREF clrBottomRight     );    此函數用于實作繪制3D矩形的位置大小,其中lpRect是填入整個3D矩形的位置大小,   clrTopLeft和clrBottomRight分别是3D效果中左上方和右下方的顔色RGB的值。

   BOOL DrawFocusRect     (  畫一個虛線矩形      HDC hDC,          // handle to device context     CONST RECT* lprc  // logical coordinates     );       數功能: 畫一個焦點矩形。這個矩形是在标志焦點的樣式中通過異或運算完成的(焦點通常用一個點線表示)。    如用同樣的參數再次調用這個函數,就表示删除焦點矩形

下面是程式代碼: 

   void  CBtnXiaoWei::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CString btnCaption;  //儲存button标題  GetWindowText(btnCaption);  //獲得button标題  CRect drawRect; //定義CRect對象 HDC dc= lpDrawItemStruct->hDC;//控件DC CDC*pDC=CDC::FromHandle(dc);//獲得CDC指針 通過 HDC UINT nStyle=lpDrawItemStruct->CtlType; drawRect.CopyRect(&(lpDrawItemStruct->rcItem)); //拷貝控件矩形區域到我們的CRect對象    DrawFrameControl(dc,&drawRect,DFC_MENU,nStyle); //繪制控件架構  CBrush pBrush;//建立畫刷

static int n=0; pBrush.CreateSolidBrush(RGB(100+n,130,n)); //建立 pDC->FillRect(drawRect,&pBrush);//畫矩形 pDC->SetTextColor(m_clo); //設定文本顔色

CRect textRect;//定義一個CRect用于繪制文本 textRect.CopyRect(&drawRect); //拷貝矩形區域 CSize sz=pDC->GetTextExtent(btnCaption);//獲得字元串尺寸 textRect.top+=(textRect.Height()-sz.cy)/2;//調整文本位置 居中 pDC->SetBkMode(TRANSPARENT);//設定文本背景透明 pDC->DrawText(btnCaption,&textRect,DT_RIGHT|DT_CENTER|DT_BOTTOM);//繪制文本 n+=10; }

void CBtnXiaoWei::SetTextColer(COLORREF clo) { m_clo=clo; Invalidate(); //是局部無效引起重畫  }

繼續閱讀