MFC Button控件自繪

文章參考地址:  http://blog.csdn.net/yue7603835/article/details/6649458框架

   VC下的界面着實難看 有時候咱們不得不本身進行控件的繪製 之前 一直不理解最近再次看了學了一遍終於明白了一點
  與你們分享下...       須要源代碼的Q我 尋找一塊兒學VC的朋友 函數

   好比說spa

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

  要覆蓋掉這個虛函數  而且類型要設置爲 BS_OWNERDRAW 這時候 放 應用程序進行初始化界面的時候 會進入咱們的設計

  DrawItem函數 進行控件的繪製   因此說 自繪製 就2個步驟    3d

  •   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 
    );   
   //函數功能: 畫一個焦點矩形。這個矩形是在標誌焦點的樣式中經過異或運算完成的(焦點一般用一個點線表示)。 
   //如用一樣的參數再次調用這個函數,就表示刪除焦點矩形

下面是程序代碼: code

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(); //是局部無效引發重畫 
}
相關文章
相關標籤/搜索