如何使button中绘制图标

1.首先我们需要建立一个新的类是从CButton中派生的,命名为MyButton        

2.我们在类中添加如下成员:

 CImageList* m_pImagelist;//图像列表指针
 int m_ImageIndex;//图像索引
 BOOL IsPressed;//按钮是都被按下

3.在类中添加方法:

void CMyButton::SetImageList(CImageList *pImage)
{
m_pImagelist = pImage;
}
void CMyButton::SetImageIndex(UINT index)
{
 m_ImageIndex = index;
}

4.添加按钮类的虚拟方法:

void CMyButton::PreSubclassWindow()
{
 
 CButton::PreSubclassWindow();
 ModifyStyle(0,BS_OWNERDRAW);//使按钮具有BS_OWNERDRAW风格
}

5.添加按钮的虚拟方法DrawItem:

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{ CDC dc ;
 dc.Attach(lpDrawItemStruct->hDC); 
 if (m_pImagelist)
 {
  UINT state = lpDrawItemStruct->itemState; //获取状态
  UINT action = lpDrawItemStruct->itemAction;

  //获取图像列中图像大小
  IMAGEINFO imageinfo;
  m_pImagelist->GetImageInfo(m_ImageIndex,&imageinfo);
  CSize imagesize;
  imagesize.cx = imageinfo.rcImage.right-imageinfo.rcImage.left;
  imagesize.cy = imageinfo.rcImage.bottom - imageinfo.rcImage.top;

  //在按钮垂直方向居中显示位图
  CRect rect;
  GetClientRect(rect);
  CPoint point;
  point.x = 5;
  point.y = (rect.Height() - imagesize.cy)/2;
  m_pImagelist->Draw(&dc,m_ImageIndex,point,ILD_NORMAL|ILD_TRANSPARENT);


  //按钮被选中或者获得焦点时
  if ((state&ODS_SELECTED)||(state&ODS_FOCUS))
  {
  
   CRect focusRect (rect); //焦点矩形

   focusRect.DeflateRect(4,4,4,4);
   CPen pen(PS_DASHDOTDOT,1,RGB(0,0,0));


   CBrush brush;
   brush.CreateStockObject(NULL_BRUSH);

   dc.SelectObject(&brush);
   dc.SelectObject(&pen);

   //绘制焦点矩形
   dc.DrawFocusRect(focusRect);
   //dc.Rectangle(focusRect);

   //绘制立体效果
   dc.DrawEdge(rect,BDR_RAISEDINNER|BDR_RAISEDOUTER,BF_BOTTOMLEFT|BF_TOPRIGHT); 
  
   //获得焦点时绘制黑色边框
   dc.Draw3dRect(rect,RGB(51,51,51),RGB(0,0,0));

  }
  else  //默认情况下
  { 

   CRect focusRect (rect);
   focusRect.DeflateRect(4,4,4,4);
   
   CPen pen(PS_DOT,1,RGB(192,192,192));
   CBrush brush;
   brush.CreateStockObject(NULL_BRUSH);

   dc.SelectObject(&brush);
   dc.SelectObject(&pen);
   dc.Rectangle(focusRect);
   
   dc.DrawEdge(rect,BDR_RAISEDINNER|BDR_RAISEDOUTER,BF_BOTTOMLEFT|BF_TOPRIGHT); 
  }

  if (IsPressed) //在按钮被按下时绘制按下效果
  {

   CRect focusRect1(rect);
   focusRect1.DeflateRect(4,4,4,4);
   dc.DrawFocusRect(focusRect1);   
   dc.DrawEdge(rect,BDR_SUNKENINNER |BDR_SUNKENOUTER ,BF_BOTTOMLEFT|BF_TOPRIGHT);

   dc.Draw3dRect(rect,RGB(51,51,51),RGB(0,0,0)); 
   //dc.DrawFocusRect(focusRect1);
  }

  //绘制按钮文本
  CString text;
  GetWindowText(text);
  rect.DeflateRect(point.x+imagesize.cx+2,0,0,0);
  dc.SetBkMode(TRANSPARENT);
  dc.DrawText(text,rect,DT_LEFT|DT_SINGLELINE|DT_VCENTER);
 }
}

 

6.添加处理按钮的三个消息如下:

void CMyBitButton::OnLButtonDown(UINT nFlags, CPoint point)
{
IsPressed = TRUE; //按钮被按下
 
 
 CButton::OnLButtonDown(nFlags, point);
}

void CMyBitButton::OnLButtonUp(UINT nFlags, CPoint point)
{
 IsPressed = FALSE; //释放鼠标按钮
 CButton::OnLButtonUp(nFlags, point);
}

void CMyBitButton::OnMouseMove(UINT nFlags, CPoint point)

 CRect rect;
 GetClientRect(rect);

 if (!rect.PtInRect(point)) //判断当前鼠标点是否在按钮区域内
 {
  if (IsPressed) //如果鼠标不在按钮的区域内,并且按钮处于按下状态,将按钮设置为正常状态
   IsPressed = FALSE;
 }
 
 CButton::OnMouseMove(nFlags, point);
}

该类就完成了

7.在程序中使用:首先我们为按钮命名通过类向导。

在类中定义一个CImageList   m_images;

在初始化中就可以完成我们队按钮的设置啦!

m_images.Create(24,24,ILC_COLOR24|ILC_MASK,1,0);

 m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON_BCON1));
 m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON_BCON2));
 m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON_BCON3));
 m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON_BCON4));

 
 m_Control1.SetImageList(&m_images); //默认图像索引为0
 m_Control2.SetImageList(&m_images);
 m_Control3.SetImageList(&m_images);
 m_Control4.SetImageList(&m_images);

 //设置按钮显示的图像索引
 m_Control1.SetImageIndex(0);
 m_Control2.SetImageIndex(1);
 m_Control3.SetImageIndex(2);
 m_Control4.SetImageIndex(3);
 

你可能感兴趣的:(null,action,button)