【VB开源代码栏目提醒】:网学会员,鉴于大家对VB开源代码十分关注,论文会员在此为大家搜集整理了“Wince5 0下实现XP风格的开始菜单源码(VB net编码实现) - 技术总结”一文,供大家参考学习!
如果想学自定义控件及自己写事件和绑定事件处理过程请仔细阅读源码此源码属100
原创遵循GPL协议但不限于GPL本人给予复制创新者以用于商业
软件的权力但复制创新者对于本源码实现功能的提升修改必须遵循GPL
开源精神。
下面是源码分两部分第一部分图文混编按钮自定义控件源码Imports System.ReflectionImports System.Drawing.ImagingPublic Class PictureButtonMenuInherits System.Windows.Forms.UserControlPrivate ChangeColor As BooleanPrivate _BorderColor As Color Color.Gold标识文本的位置Public Enum LayoutLeft 0right 1up 2down 3middle 4End Enum字段起始按钮颜色Private BackgroundImgColor As Color Me.BackColor按下后按钮颜色Private PressedImgColor As Color Color.SteelBlue是否按下Public Pressed As Boolean False是否移动到上面Public Moved As Boolean False当前图文布局Private TxtImgLayout As Layout 1当前按钮是否可用标志Private BtnEnabled As Boolean True属性Private Ctext As String quot按钮文本按钮文本quotPublic imageHeight As Integer 16Public imageWidth As Integer 16当作为工具栏最终重新设置其大小时的变量Public BtnWidth As Integer 0Public BtnHeight As Integer 0 ltsummarygt 设置Icon图标的高度 lt/summarygt ltremarksgt设置Icon图标的高度lt/remarksgt ltvaluegt32lt/valuegtPublic Property setImageHeight As IntegerGetReturn Me.ImageHeightEnd GetSetByVal Value As IntegerMe.ImageHeight ValueEnd SetEnd PropertyPublic Property SetTag As IntegerGetReturn MyBase.TagEnd GetSetByVal value As IntegerMyBase.Tag valueEnd SetEnd Property单击按钮后启动的外接
程序平台可以任意添加EXE了Public BtnAppName As String quotquotPublic Property SetAppName As StringGetReturn Me.BtnAppNameEnd GetSetByVal value As StringMe.BtnAppName valueEnd SetEnd PropertyPublic Property setImageWidth As IntegerGetReturn Me.ImageWidthEnd GetSetByVal Value As IntegerMe.ImageWidth ValueEnd SetEnd Property平时的背景色Public Property BackgroundImgColorValue As ColorGetReturn Me.BackgroundImgColorEnd GetSetByVal Value As ColorMe.BackgroundImgColor ValueEnd SetEnd Property鼠标按下后的背景色Public Property PressedImageColorValue As ColorGetReturn Me.pressedImgColorEnd GetSetByVal Value As ColorMe.pressedImgColor ValueEnd SetEnd Property显示的文本Public Property Caption As StringGetReturn Me.CtextEnd GetSetByVal Value As StringMe.Ctext ValueEnd SetEnd Property文字的布局Public Property TextImageLayout As LayoutGetReturn Me.TxtImgLayoutEnd GetSetByVal Value As LayoutMe.TxtImgLayout ValueEnd SetEnd Property控件大小是否自动调整Private ControlSizeSet As Boolean FalsePublic Property ControlSizeSetF As BooleanGetReturn ControlSizeSetEnd GetSetByVal value As BooleanMe.ControlSizeSet valueEnd SetEnd PropertyPrivate bmpIconCount As Integer 0Private bmp As ImagePublic Sub AddBmpByVal bmpSl As Imagebmp bmpSlbmpIconCount 1End Sub是否是父菜单默认不是Private YNParentMenu As Boolean FalsePublic Property SetParentMenu As BooleanGetReturn YNParentMenuEnd GetSetByVal value As BooleanYNParentMenu valueEnd SetEnd Property绘制表面Protected Overrides Sub OnPaintByVal e As PaintEventArgsDim gp As Graphics e.Graphics控件大小不自动调整If ControlSizeSet False ThenSelect Case bmpIconCountCase 0 画背景Dim backgroundImg As Image MakeBitmapBackgroundImgColor Me.Width Me.HeightDim pressedImg As Image MakeBitmapPressedImgColor Me.Width Me.HeightIf Me.Pressed AndAlso pressedImg IsNot Nothing Thengp.DrawImagepressedImg 0 0e.Graphics.DrawRectangleDirectCastNew Pen_BorderColor Pen 0 0 Me.Width -1 Me.Height -1ElseIf Me.Moved AndAlso pressedImg IsNot Nothing Thengp.DrawImagepressedImg 0 0ElseIf Me.BtnEnabled False Then按钮不可用时Dim UnenableImg As Image MakeBitmapColor.FromArgb247 247 239 Me.Width Me.Widthgp.DrawImageUnenableImg 0 0e.Graphics.DrawRectangleDirectCastNew PenColor.FromArgb206 199 189 3 Pen 0 0 Me.Width -1 Me.Height -1UnenableImg.DisposeElsegp.DrawImagebackgroundImg 0 0三维效果 e.Graphics.DrawRectangleDirectCastNew PenColor.Transparent Pen 0 0 Me.Width Me.HeightEnd IfbackgroundImg.DisposepressedImg.DisposeIf Me.Ctext.Length gt 0 ThenDim size As SizeF e.Graphics.MeasureStringMe.Ctext Me.FontIf Me.BtnEnabled False Thene.Graphics.DrawStringMe.Ctext Me.Font New SolidBrushColor.FromArgb173 170 156 Me.ClientSize.Width -size.Width / 2 Me.ClientSize.Height -size.Height / 2Elsee.Graphics.DrawStringMe.Ctext Me.Font New SolidBrushMe.ForeColor Me.ClientSize.Width -size.Width / 2 Me.ClientSize.Height -size.Height / 2End IfEnd IfCase 1 画图像Dim vsizef As SizeFIf Me.Pressed AndAlso bmp IsNot Nothing Then文本颜色取按下的颜色Dim cbrush As New System.Drawing.SolidBrushMe.PressedImgColorSelect Case Me.TxtImgLayoutCase Layout.Leftvsizef gp.MeasureStringCtext Me.Font虽然预留了8个像素 但是只平均8个像素gp.DrawStringCtext Me.Font cbrush 4 Me.ClientSize.Height -vsizef.Height / 2图像的X坐标是vsizef.Width 8Dim imgRect As Rectangle New Rectanglevsizef.Width 8 Me.ClientSize.Height -imageHeight / 2 ImageWidth ImageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.rightvsizef gp.MeasureStringCtext Me.Font图像的Y坐标是cHeight -ImageHeight / 2Dim imgRect As Rectangle New Rectangle0 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing虽然预留了8个像素 但是只平均6个像素gp.DrawStringCtext Me.Font cbrush Me.ImageWidth 3 Me.ClientSize.Height -vsizef.Height / 2Case Layout.upvsizef gp.MeasureStringCtext Me.Font文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 2图像的Y坐标是vsizef.Height 4Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 vsizef.Height 4 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.downvsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 0 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 -1 ImageHeight 2Case Layout.middlevsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 Me.ClientSize.Height -vsizef.Height / 2End Selectcbrush.Disposecbrush Nothing四周画颜色矩形e.Graphics.DrawRectangleDirectCastNew Pen_BorderColor Pen 0 0 Me.ClientSize.Width -1 Me.ClientSize.Height -1gp.Disposegp NothingElseIf Me.Moved AndAlso bmp IsNot Nothing Then文本颜色取前景色Dim cbrush As New System.Drawing.SolidBrushMe.PressedImgColorSelect Case Me.TxtImgLayoutCase Layout.Leftvsizef gp.MeasureStringCtext Me.Font虽然预留了8个像素 但是只平均8个像素gp.DrawStringCtext Me.Font cbrush 4 Me.ClientSize.Height -vsizef.Height / 2图像的X坐标是vsizef.Width 8Dim imgRect As Rectangle New Rectanglevsizef.Width 8 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.rightvsizef gp.MeasureStringCtext Me.Font图像的Y坐标是cHeight -ImageHeight / 2Dim imgRect As Rectangle New Rectangle0 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing虽然预留了8个像素 但是只平均6个像素gp.DrawStringCtext Me.Font cbrush Me.imageWidth 3 Me.ClientSize.Height -vsizef.Height / 2Case Layout.upvsizef gp.MeasureStringCtext Me.Font文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 2图像的Y坐标是vsizef.Height 4Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 vsizef.Height 4 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.downvsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 0 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 -1 imageHeight 2Case Layout.middlevsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 Me.ClientSize.Height -vsizef.Height / 2End Selectcbrush.Disposecbrush Nothinggp.Disposegp NothingElseIf Me.BtnEnabled False Then文本颜色取前景色Dim cbrush As New System.Drawing.SolidBrushColor.FromArgb173 170 156Select Case Me.TxtImgLayoutCase Layout.Leftvsizef gp.MeasureStringCtext Me.Font虽然预留了8个像素 但是只平均8个像素gp.DrawStringCtext Me.Font cbrush 4 Me.ClientSize.Height -vsizef.Height / 2图像的X坐标是vsizef.Width 8Dim imgRect As Rectangle New Rectanglevsizef.Width 8 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.rightvsizef gp.MeasureStringCtext Me.Font图像的Y坐标是cHeight -ImageHeight / 2Dim imgRect As Rectangle New Rectangle0 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing虽然预留了8个像素 但是只平均6个像素gp.DrawStringCtext Me.Font cbrush Me.imageWidth 3 Me.ClientSize.Height -vsizef.Height / 2Case Layout.upvsizef gp.MeasureStringCtext Me.Font文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 2图像的Y坐标是vsizef.Height 4Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 vsizef.Height 4 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.downvsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 0 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 -1 imageHeight 2Case Layout.middlevsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 Me.ClientSize.Height -vsizef.Height / 2End Selectcbrush.Disposecbrush Nothinge.Graphics.DrawRectangleDirectCastNew PenColor.FromArgb206 199 189 3 Pen 0 0 Me.Width -1 Me.Height -1gp.Disposegp NothingElse文本颜色取前景色Dim cbrush As New System.Drawing.SolidBrushMe.ForeColorSelect Case Me.TxtImgLayoutCase Layout.Leftvsizef gp.MeasureStringCtext Me.Font虽然预留了8个像素 但是只平均8个像素gp.DrawStringCtext Me.Font cbrush 4 Me.ClientSize.Height -vsizef.Height / 2图像的X坐标是vsizef.Width 8Dim imgRect As Rectangle New Rectanglevsizef.Width 8 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.rightvsizef gp.MeasureStringCtext Me.Font图像的Y坐标是cHeight -ImageHeight / 2Dim imgRect As Rectangle New Rectangle0 Me.ClientSize.Height -imageHeight / 2 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing虽然预留了8个像素 但是只平均6个像素gp.DrawStringCtext Me.Font cbrush Me.imageWidth 3 Me.ClientSize.Height -vsizef.Height / 2Case Layout.upvsizef gp.MeasureStringCtext Me.Font文本起始坐标32gp.DrawStringCtext Me.Font cbrush Me.ClientSize.Width -vsizef.Width / 2 2图像的Y坐标是vsizef.Height 4Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 vsizef.Height 4 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr NothingCase Layout.downvsizef gp.MeasureStringCtext Me.Font图像的X坐标是Me.Width -ImageWidth / 2 -1Dim imgRect As Rectangle New RectangleMe.ClientSize.Width -imageWidth / 2 -1 0 imageWidth imageHeightDim imageAttr As New ImageAttributesimageAttr.SetColorKeyBackgroundImageColorbmp BackgroundImageColorbmpgp.DrawImagebmp imgRect 0 0 imageWidth imageHeight GraphicsUnit.Pixel imageAttrimgRect NothingimageAttr Nothing文本起始坐标32gp.DrawStringCtext Me.Font .