52上位机VC MF实现多边形不规则按钮
52上位机VC MF实现多边形不规则按钮 功能展示 VC提供的系统默认按钮都是方形的矩形按钮,如果我们想实现多边形按钮,10边形按钮等不规则按钮呢?多边形的不规则按钮是给程序界面添加了一定的效果,我们当前例程就实现了任意边数的按钮,效果如图 要点提示 和我上一例程【51上位机VC MFC实现自绘的图文按钮】一样,个性化的按钮控件,我们是通过自绘方法实现,自绘方法是通过DrawItem()函数来完成;绘制多边开得用函数Polygon()来完成;BOOL Polygon( LPPOINT lpPoints, int nCount ); lpPoints为顶点数组指针, nCount 为数组中顶点数 实现功能 1.新建基于对话框的应用程序 2.从Cbutton派生一个自己的类classCPolygonBtn : public Cbutton,添加几个变量private: int m_nNum;//按钮的边数标识 BOOLm_bSetToPol; //标识 多边效果有效 COLORREF m_color; CPointm_arrays[10]; BOOL m_bIsPressed; //按钮是否被按下标识,并在构造函数中初始化 m_bSetToPol = TRUE; m_bIsPressed =FALSE; m_nNum = 4; m_color = RGB(255,255,255); 3.添加鼠标按下,放开,及移动处理函数,参数设置函数及DrawItem()函数 - void CPolygonBtn::OnLButtonDown(UINT nFlags, CPoint point)
- {m_bIsPressed = TRUE; CButton::OnLButtonDown(nFlags, point);
- }
- void CPolygonBtn::OnLButtonUp(UINT nFlags, CPoint point)
- {m_bIsPressed = FALSE; CButton::OnLButtonUp(nFlags, point);
- }
- void CPolygonBtn::OnMouseMove(UINT nFlags, CPoint point)
- {
- CRect rect;
- GetClientRect(rect);
- if(rect.PtInRect(point))//判断当前鼠标点是否在按钮区域内
- {
- if(m_bIsPressed)//如果鼠标不在按钮的区域内,并且按钮处于按下状态,将按钮设置为正常状态
- m_bIsPressed = FALSE;
- }
- CButton::OnMouseMove(nFlags, point);
- }
复制代码- void CPolygonBtn::SetEffect(int num, COLORREF bkColor,BOOL bSetToPol)
- {
- m_nNum = num;
- m_nNum=m_nNum>10?10:m_nNum;//保证最大边数在10内
- m_bSetToPol = bSetToPol;
- m_color = bkColor;
- }
- void CPolygonBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- CRect rect;
- GetClientRect(rect);
- CDC dc;
- dc.Attach(lpDrawItemStruct->hDC);
- int x,y,r;
- x = rect.Width()/2;
- y = rect.top;
- r = rect.Height()/2;
- double lpi=0;
- m_arrays[0] = CPoint(x,y);
- if(m_bSetToPol)
- {
- for(int i=1;i<m_nNum;i++)
- {lpi+=(2*PI/m_nNum);
- if(lpi<=2*PI/4)
- {m_arrays[i] = CPoint(x+r*sin(2*i*PI/m_nNum),r-r*cos(2*i*PI/m_nNum));
- }
- if(lpi>2*PI/4 && lpi<=2*PI/2)
- {m_arrays[i] = CPoint(x+r*sin(PI-2*i*PI/m_nNum),r+r*cos(PI-2*i*PI/m_nNum));
- }
- if(lpi>2*PI/2 && lpi<=2*PI*3/4)
- {m_arrays[i] = CPoint(x-r*sin(2*i*PI/m_nNum-2*PI/2),r+r*cos(2*i*PI/m_nNum-2*PI/2));
- }
- if(lpi>2*PI*3/4 && lpi<=2*PI)
- {m_arrays[i] = CPoint(x-r*sin(2*PI-2*i*PI/m_nNum),r-r*cos(2*PI-2*i*PI/m_nNum));
- }
- }
- }
复制代码- //设置背景透明
- dc.SetBkMode(TRANSPARENT);
- //创建一个位图画刷
- CBrush brush(m_color);
- dc.SelectObject(&brush);
- CPen pen(PS_NULL,1,m_color);
- dc.SelectObject(&pen);
- if(m_bSetToPol) dc.Polygon(m_arrays,m_nNum);
- else dc.Ellipse(0,0,rect.Width(),rect.Height());
- if(m_bIsPressed)
- {
- CPen pen(PS_DASHDOTDOT,2,RGB(0,0,0));
- dc.SelectObject(&pen);
- if(m_bSetToPol)
- {
- dc.MoveTo(m_arrays[0]);
- for(int i=1;i<m_nNum;i++)
- { dc.LineTo(m_arrays[i]);
- }
- dc.LineTo(m_arrays[0]);
- }
- else dc.Ellipse(0,0,rect.Width(),rect.Height());
- }
- else
- {
- CPen pen(PS_DASHDOTDOT,2,m_color);
- dc.SelectObject(&pen);
- if(m_bSetToPol)
- {
- dc.MoveTo(m_arrays[0]);
- for(int i=1;i<m_nNum;i++)
- { dc.LineTo(m_arrays[i]);
- }
- dc.LineTo(m_arrays[0]);
- }
- else dc.Ellipse(0,0,rect.Width(),rect.Height());
- }
-
复制代码4.最后添加我们类中计算要用到的头文件#include<cmath>,及添加一个宏定义#define PI 2.0*asin(1.0) 完成类的编程 5.类的使用,在对话框中添加几个按钮,关联我们的类变量。进行效果设置便可如我们例程中的设置;m_Btn.SetEffect(3,RGB(255,0,0),TRUE);
我们来演示下实现的整个过程
|