按钮加载位图的几个方法 vc

一,只加载一张位图的方法:

1,

装入bmp资源,id为IDB_BMP,按钮的bitmap属性设为true,icon属性为false。

 2,

CButton *pBtn = (CButton *)GetDlgItem(IDB_BMP);
 CBitmap bitMap;
 HBITMAP hBit ;
 if(bitMap.LoadBitmapW(IDB_NORMAL_BTN))
 {
  hBit = (HBITMAP)bitMap.Detach();
  pBtn->SetBitmap(hBit);
 }

缺点:图片不会自动拉伸。


二,为按钮的不同状态加载不同的位图:(使用CBitmapButton类)这种方法还可以。
1,按钮属性 Owner Draw选上,按钮ID: IDC_BUTTON1

为IDC_BUTTON1添加CButton变量m_button,
2,然后手动将CButton m_button改为
CBitmapButton m_button;
3,在初始化里边
m_button.LoadBitmaps(IDB_BITMAP1, IDB_BITMAP2);//IDB_BITMAP1:平时;IDB_BITMAP2:按下
m_button.SubclassDlgItem(IDC_BUTTON1, this);
m_button.SizeToContent(); 

或者用另一种方式,不必为IDC_BUTTON1添加关联的变量。
直接声明CBitmapButton m_button;,在初始化里边调用

m_BitmapBtn.AutoLoad(IDC_BUTTON1_AREA,this);//把按钮和变量联系起来

m_BitmapBtn.LoadBitmaps(IDB_BITMAP1, IDB_BITMAP2);

m_button.SizeToContent(); 

 

缺点:1,无法显示文字,需重载DrawItem函数。

            2,无法去掉图片的背景色,即只能显示矩形按钮。

// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"), 
//      WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW, 
//      CRect(10,10,100,30), pParentWnd, 1);
//

// This example implements the DrawItem method for a CButton-derived 
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
   UINT uStyle = DFCS_BUTTONPUSH;

   // This code only works with buttons.
   ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

   // If drawing selected, add the pushed style to DrawFrameControl.
   if (lpDrawItemStruct->itemState & ODS_SELECTED)
      uStyle |= DFCS_PUSHED;

   // Draw the button frame.
   ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
      DFC_BUTTON, uStyle);

   // Get the button's text.
   CString strText;
   GetWindowText(strText);

   // Draw the button text using the text color red.
   COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
   ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
      &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
   ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}//继承CBitmapButton的例子

void   MyButton::DrawItem(LPDRAWITEMSTRUCT   lpDrawItemStruct)   
{ 
//   TODO:   Add   your   code   to   draw   the   specified   item 
//从lpDrawItemStruct获取控件的相关信息 
CRect   rect=lpDrawItemStruct-> rcItem; 
CDC   *pDC=CDC::FromHandle(lpDrawItemStruct-> hDC); 
int   nSaveDC=pDC-> SaveDC(); 
UINT   state   =   lpDrawItemStruct-> itemState; 
TCHAR   strText[MAX_PATH   +   1]; 
::GetWindowText(m_hWnd,   strText,   MAX_PATH); 

CBitmapButton::DrawItem(lpDrawItemStruct); 

CRect   rect1=rect; 
rect.SetRect(rect1.left,rect1.top,rect1.left+75,rect1.top+24); 
//显示按钮的文本 
pDC-> SetTextColor(TextColor); 
if   (strText!=NULL) 
{ 
CFont*   hFont   =   GetFont(); 
CFont*   hOldFont   =   pDC-> SelectObject(hFont); 
CSize   szExtent   =   pDC-> GetTextExtent(strText,   lstrlen(strText)); 
CPoint   pt(   rect.CenterPoint().x   -   szExtent.cx   /   2,   rect.CenterPoint().y   -   szExtent.cy   /   2); 
if   (state   &   ODS_SELECTED)   
pt.Offset(1,   1); 
int   nMode   =   pDC-> SetBkMode(TRANSPARENT); 
if   (state   &   ODS_DISABLED) 
pDC-> DrawState(pt,   szExtent,   strText,   DSS_DISABLED,   TRUE,   0,   (HBRUSH)NULL); 
else 
pDC-> DrawState(pt,   szExtent,   strText,   DSS_NORMAL,   TRUE,   0,   (HBRUSH)NULL); 
pDC-> SelectObject(hOldFont); 
pDC-> SetBkMode(nMode); 
} 

pDC-> RestoreDC(nSaveDC); 

} 


三,继承CButton类,重写OnPaint函数,为Button的不同状态画位图(BitBlt,TransparentBlt,StretchBlt ,MaskBlt,

PlgBlt

)。

 

四,
可以采用CButtonST控件,挺好用的,比MFC的那些封装好用。
假设按钮ID为IDC_BUTTON1 
1.添加成员变量 
CButtonST m_btn; 
2.添加位图资源,ID设为IDB_BITMAP1 
3.在OnInitDialog函数中初始化按钮m_btn.SubclassDlgItem(IDC_BUTTON1,this); 
m_btn.SetBitmaps(IDB_BITMAP1,RGB(0,0,0)); 
m_btn.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); 
注: 上面的SetBitmaps函数会将图片中颜色值为RGB(0,0,0)的点设为透明。

 

五,

把button按钮属性设置为ower draw
然后映射onDrawItem消息
在ondraw函数内自己绘制就可以了
void CUi6Dlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(nIDCtl == IDC_HELLO_CFAN)
{
//绘制按钮框架
UINT uStyle = DFCS_BUTTONPUSH;
//是否按下去了?
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);
//输出文字
dc.SelectObject(&m_Font);
dc.SetTextColor(RGB(0, 0, 255));
dc.SetBkMode(TRANSPARENT);
CString sText;
m_HelloCFan.GetWindowText(sText);
dc.TextOut(lpDrawItemStruct->rcItem.left + 20, lpDrawItemStruct->rcItem.top + 20, sText);
//是否得到焦点
if(lpDrawItemStruct->itemState & ODS_FOCUS)
{
//画虚框
CRect rtFocus = lpDrawItemStruct->rcItem;
rtFocus.DeflateRect(3, 3);
dc.DrawFocusRect(&rtFocus);
}
return;
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

 


 

 


 

你可能感兴趣的:(按钮加载位图的几个方法 vc)