51上位机VC MFC实现自绘的图文按钮
51上位机VC MFC实现自绘的图文按钮
功能展示
按钮是我们程序中极为常用的控件,自绘是我们美化控件常用的方法,图文并茂的按钮MFC并不自带,得我们自行完成,我们当前例程用自绘方法实现了图文并茂的功能,效果如图 要点提示 在MFC中实现控件的自绘可以通过控件的DrawItem函数来实现:virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );当控件带有属性BS_OWNERDRAW时,应用程序就会自动调用DrawItem函数,实现我们按钮的自绘; BS_OWNERDRAW风格可以在对话框资源中对控件进行设置,也可以在自己派生的控件类中添加一个函数PreSubclassWindow()再函数内部调用ModifyStyle(0,BS_OWNERDRAW)实现属性的设置; LPDRAWITEMSTRUCT带有系统返回的一些自信供我们使用,其结构成员有CtlType表示控件类型 值可以为ODT_BUTTON ODT_COMBOBOX ODT_LISTBOXODT_LISTVIEW ODT_MENU ODT_STATIC ODT_TAB; CtlID表示控件的ID;itemID表示菜单项ID或列表框,组合框 中的项目索引;ItemAction表示绘制的动作,可选值为ODA_DRAWENTIRE 整个旁若无人要被绘制时设置该标识;ODA_FOCUS控件获得或失去焦点时设置该标识; ODA_SELECT控件 处于选中状态时设置该标识; itemState 表示需要绘画的状态,可选值如下ODS_CHECKED The menu item is to bechecked. This bit is used only in a menu. ODS_COMBOBOXEDIT The drawing takesplace in the selection field (edit control) of an owner-drawn combo box.ODS_DEFAULT The item is the default item. ODS_DISABLED The item is to be drawnas disabled. ODS_FOCUS The item has the keyboard focus. ODS_GRAYED The item isto be grayed. This bit is used only in a menu. ODS_SELECTED The menu item‘sstatus is selected.; hwndItem 表示控件的句柄; hDC 表示控件的DC; rcItem 表示控件的矩形区域; itemData 控件的附加信息;
实现功能 1.新建基于对话框的应用程序 2.从Cbutton派生一个自已的类CImageBtn;并添加三个变量private:CImageList*m_pImagelist; //图像列表指针 int m_nImageIndex; //图标索引BOOL m_bIsPressed; //按钮是否被按下;构造函数中初始化m_bIsPressed = FALSE;
3.添加另两个变量的设置函数 - void CImageBtn::SetImageIndex(UINT index)
- {
- m_nImageIndex = index;
- }
- void CImageBtn::SetImageList(CImageList *pImage)
- {
- m_pImagelist = pImage;
- }
复制代码3.添加与处理鼠标按下,放开,移动及控件子类化消息消息
- void CImageBtn::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_bIsPressed = TRUE;
- CButton::OnLButtonDown(nFlags, point);
- }
- void CImageBtn::OnLButtonUp(UINT nFlags, CPoint point)
- {
- m_bIsPressed = FALSE;
- CButton::OnLButtonUp(nFlags, point);
- }
- void CImageBtn::OnMouseMove(UINT nFlags, CPoint point)
- {
- CRect rect;
- GetClientRect(rect);
- if (!rect.PtInRect(point)) //判断当前鼠标点是否在按钮区域内
- {
- if (m_bIsPressed) //如果鼠标不在按钮的区域内,并且按钮处于按下状态,将按钮设置为正常状态
- m_bIsPressed = FALSE;
- }
- CButton::OnMouseMove(nFlags, point);
- }
- void CImageBtn::PreSubclassWindow()
- {
- ModifyStyle(0,BS_OWNERDRAW);
- CButton::PreSubclassWindow();
- }
复制代码4.添加自绘函数DrawItem - void CImageBtn::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_nImageIndex,&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_nImageIndex,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.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 (m_bIsPressed) //在按钮被按下时绘制按下效果
- {
- 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);
- }
- }
复制代码5.在主对话框中添加一个CImageList m_images;和几个按钮关联CImageBtn变量,并初始化设置 m_images.Create(24,24,ILC_COLOR24|ILC_MASK,1,0); m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON2)); m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON3)); m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON4));
m_Btn.SetImageList(&m_images); m_Btn2.SetImageList(&m_images); m_Btn3.SetImageList(&m_images); m_Btn4.SetImageList(&m_images);
//设置按钮显示的图像索引 m_Btn.SetImageIndex(0); m_Btn2.SetImageIndex(1); m_Btn3.SetImageIndex(2); m_Btn4.SetImageIndex(3); 我们来演示下功能实现过程
|