选择字体.docx
- 文档编号:15706501
- 上传时间:2023-07-06
- 格式:DOCX
- 页数:9
- 大小:31.58KB
选择字体.docx
《选择字体.docx》由会员分享,可在线阅读,更多相关《选择字体.docx(9页珍藏版)》请在冰点文库上搜索。
选择字体
标题:
自定义控件--xp风格按钮(可设置文字颜色)
正文:
ImportsSystem.Drawing
ImportsSystem.ComponentModel
PublicClasswinxpbutton
InheritsSystem.Windows.Forms.Button
Privatemy_mouseDownAsBoolean=False'鼠标按下
Privatemy_mouseHoverAsBoolean=False'鼠标移到上面
Privatem_textcolorAsColor=System.Drawing.Color.Black'字体颜色
")>_ PublicPropertytextcolor()AsColor Get Returnm_textcolor EndGet Set(ByValValueAsColor) m_textcolor=Value Me.Invalidate() EndSet EndProperty PublicSubNew() MyBase.New() '该调用是Windows窗体设计器所必需的。 InitializeComponent() '在InitializeComponent()调用之后添加任何初始化,true表示将指定的样式应用到控件 '设置控件样式位能够充分地更改控件行为 Me.SetStyle(ControlStyles.UserPaint,True) '关联事件委托 AddHandlerMe.MouseDown,AddressOfmy_OnMouseDown AddHandlerMe.MouseUp,AddressOfmy_OnMouseUp AddHandlerMe.MouseEnter,AddressOfmy_OnMouseEnter AddHandlerMe.MouseLeave,AddressOfmy_OnMouseLeave Height=23 Width=75 EndSub ProtectedOverridesSubOnPaint(ByValpeventAsSystem.Windows.Forms.PaintEventArgs) 'pevent.ClipRectangle指在其中绘制的矩形,即使用父控件的背景色来画这个矩形按钮 pevent.Graphics.FillRectangle(NewSolidBrush(Me.Parent.BackColor),pevent.ClipRectangle) If(Enabled=False)Then '画不可用状态 DrawDisableButton(pevent.Graphics) ElseIf(my_mouseDown)Then'画鼠标按下状态 DrawMouseDownButton(pevent.Graphics) ElseIf(my_mouseHover)Then'画鼠标移动到其上状态 DrawMouseHoverButton(pevent.Graphics) ElseIf(Focused)Then'有焦点,但鼠标未移动到其上 DrawContainFocusButton(pevent.Graphics) Else'一般情况下 DrawNormalButton(pevent.Graphics) EndIf '写文本 WriteText(pevent.Graphics) EndSub '鼠标按下的状态处理 PrivateSubmy_OnMouseDown(ByValsenderAsObject,ByValeAsMouseEventArgs) my_mouseDown=True'鼠标按下 EndSub '鼠标松开状态的处理 PrivateSubmy_OnMouseUp(ByValsenderAsObject,ByValeAsMouseEventArgs) my_mouseDown=False'鼠标松开 '重新绘制控件时发生Paint事件。 PaintEventArgs指定绘制控件所用的Graphics '以及绘制控件所在的ClipRectangle。 DimpeAsPaintEventArgs=NewPaintEventArgs(CreateGraphics(),ClientRectangle) OnPaint(pe) EndSub '鼠标进入 PrivateSubmy_OnMouseEnter(ByValsenderAsObject,ByValeAsEventArgs) my_mouseHover=True'鼠标移动到其上 ' DimpeAsPaintEventArgs=NewPaintEventArgs(CreateGraphics(),ClientRectangle) OnPaint(pe) EndSub '鼠标移动开 PrivateSubmy_OnMouseLeave(ByValsenderAsObject,ByValeAsEventArgs) my_mouseHover=False'鼠标移动开 ' DimpeAsPaintEventArgs=NewPaintEventArgs(CreateGraphics(),ClientRectangle) OnPaint(pe) EndSub PrivateSubDrawBorder(ByValgAsGraphics,ByValstateAsInteger) If(state=1)Then'绘制一般边框 '绘制一个画笔,高光点,宽度2 DimpAsPen=NewPen(SystemColors.ControlLightLight,2) 'g.DrawLine画线,p是画笔,后面是第一个点的坐标,第二个点的坐标 g.DrawLine(p,1,1,1,Height-2)'绘制左侧竖线 g.DrawLine(p,1,1,Width-2,1)'绘制上面横线 g.DrawLine(p,Width-1,2,Width-1,Height-2)'绘制右侧竖线,由于已经在上面绘制了横线(纵坐标为1),所以从2开始 g.DrawLine(p,2,Height-1,Width-2,Height-1)'绘制下面横线 ElseIf(state=2)Then'绘制移动到其上的边框 '与一般边框用高光区别的是显示黄色 DimpAsPen=NewPen(Color.Yellow,2) g.DrawLine(p,1,1,1,Height-2) g.DrawLine(p,1,1,Width-2,1) g.DrawLine(p,Width-1,2,Width-1,Height-2) g.DrawLine(p,2,Height-1,Width-2,Height-1) ElseIf(state=3)Then'绘制按下的显示边框 '与一般边框用高光区别的是显示暗褐色 DimpAsPen=NewPen(SystemColors.ControlDark,2) g.DrawLine(p,1,1,1,Height-2) g.DrawLine(p,1,1,Width-2,1) g.DrawLine(p,Width-1,2,Width-1,Height-2) g.DrawLine(p,2,Height-1,Width-2,Height-1) ElseIf(state=4)Then'绘制不可用状态边框 '与一般边框用高光区别的是显示亮色 DimpAsPen=NewPen(SystemColors.ControlLight,2) g.DrawLine(p,1,1,1,Height-2) g.DrawLine(p,1,1,Width-2,1) g.DrawLine(p,Width-1,2,Width-1,Height-2) g.DrawLine(p,2,Height-1,Width-2,Height-1) ElseIf(state=5)Then'绘制有焦点但鼠标不在其上的状态 '与一般边框用高光区别的是显示兰色 DimpAsPen=NewPen(Color.SkyBlue,2) g.DrawLine(p,1,1,1,Height-2) g.DrawLine(p,1,1,Width-2,1) g.DrawLine(p,Width-1,2,Width-1,Height-2) g.DrawLine(p,2,Height-1,Width-2,Height-1) EndIf '//做完如上的处理后再对可用和不可用做圆化边缘处理(也就是把按钮的4个角进行圆化处理) If(state=4)Then'不可用时 '使用画笔,Color.FromArgb(161,161,146)是从一个32位的ARGB值创建系统颜色,宽度为1 DimpAsPen=NewPen(Color.FromArgb(161,161,146),1) g.DrawLine(p,0,2,0,Height-3)'左侧竖线(除了两个边角剩下的线) g.DrawLine(p,2,0,Width-3,0)'上面横线(除了两个边角剩下的线) g.DrawLine(p,Width-1,2,Width-1,Height-3)'右侧竖线(除了两个边角剩下的线) g.DrawLine(p,2,Height-1,Width-3,Height-1)'下面的横线 g.DrawLine(p,0,2,2,0)'左上角 g.DrawLine(p,0,Height-3,2,Height-1)'左下角 g.DrawLine(p,Width-3,0,Width-1,2)'右上角 g.DrawLine(p,Width-3,Height-1,Width-1,Height-3)'右下角 Else'drawnormalstyleborder '采用默认的黑色进行绘制边角 g.DrawLine(Pens.Black,0,2,0,Height-3) g.DrawLine(Pens.Black,2,0,Width-3,0) g.DrawLine(Pens.Black,Width-1,2,Width-1,Height-3) g.DrawLine(Pens.Black,2,Height-1,Width-3,Height-1) g.DrawLine(Pens.Black,0,2,2,0) g.DrawLine(Pens.Black,0,Height-3,2,Height-1) g.DrawLine(Pens.Black,Width-3,0,Width-1,2) g.DrawLine(Pens.Black,Width-3,Height-1,Width-1,Height-3) EndIf EndSub '一般状态 PrivateSubDrawNormalButton(ByValgAsGraphics) '绘制边框,宽度为1 DrawBorder(g,1) '绘制背景,用高光点颜色 PaintBack(g,SystemColors.ControlLightLight) EndSub '鼠标移动到其上的状态 PrivateSubDrawMouseHoverButton(ByValgAsGraphics) DrawBorder(g,2) PaintBack(g,SystemColors.ControlLightLight) EndSub PrivateSubDrawMouseDownButton(ByValgAsGraphics) DrawBorder(g,3) '绘制背景,用三维元素的亮色 PaintBack(g,SystemColors.ControlLight) EndSub PrivateSubDrawDisableButton(ByValgAsGraphics) DrawBorder(g,4) '亮色 PaintBack(g,SystemColors.ControlLight) EndSub PrivateSubDrawContainFocusButton(ByValgAsGraphics) DrawBorder(g,5) '高光点 PaintBack(g,SystemColors.ControlLightLight) EndSub '绘制背景色 PrivateSubPaintBack(ByValgAsGraphics,ByValcAsColor) '填充时采用: 单色画刷,相对与(0,0)坐标(3,3)的位置,大小为宽-6,高-6 g.FillRectangle(NewSolidBrush(c),3,3,Width-6,Height-6) EndSub '写文本 PrivateSubWriteText(ByValgAsGraphics) '计算文本的位置 DimxAsInteger=0 DimyAsInteger=0 'size用宽高有序对表示矩形区域 DimsAsSize=g.MeasureString(Text,Font).ToSize() x=(Width-s.Width)/2'文字相对控件x偏移 y=(Height-s.Height)/2'文字相对控件y偏移 '写文本 If(Enabled)Then'如果控件可用,则黑色文字 'g.DrawString(Text,Font,Brushes.Black,x,y) DimbAsNewSolidBrush(m_textcolor) g.DrawString(Text,Font,b,x,y) Else'如果控件不可用,则灰色文字 g.DrawString(Text,Font,Brushes.Gray,x,y) EndIf EndSub EndClass
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 选择 字体
![提示](https://static.bingdoc.com/images/bang_tan.gif)