VB图片动态按钮控件

在这里插入图片描述
'新建一个ActiveX控件工程,粘贴以下代码:
Option Explicit
Private Declare Function GetCursorPos Lib “user32” (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowRect Lib “user32” (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim Zt As String, Dx As Integer, Ct As Boolean, Xt As Boolean, Scx As Boolean, Xhx As Boolean
Dim Pic As stdole.IPictureDisp '定义图片
Dim WithEvents Tim As Timer '计时器
Dim W As Long, H As Long '图片大小(分割后单张)
Dim txt As String '标题
Dim But As Integer
Dim Sw As OLE_TRISTATE
Dim Butss As Boolean
Public Event Click()
Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Private Sub UserControl_Initialize()
UserControl.AutoRedraw = True
Set Tim = Controls.Add(“vb.Timer”, “Tim”)
W = 250
H = 250
UserControl.BackColor = RGB(60, 60, 60)
Sw = Checked
End Sub

Private Sub UserControl_Resize()
If Enabled = True Then
Call JiexuanPic(0)
Else
Call JiexuanPic(2)
End If
UserControl.Width = W: UserControl.Height = H
End Sub

'颜色
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End Property

Public Property Let ForeColor(ByVal vNewValue As OLE_COLOR)
UserControl.ForeColor = vNewValue
Call txtStr(txt)
PropertyChanged “ForeColor”
End Property

'图片
Public Property Get Picture() As StdPicture
Set Picture = Pic
End Property

Public Property Set Picture(ByVal vNewValue As StdPicture)
Set Pic = vNewValue
Call JiexuanPic(0)
UserControl.Width = W: UserControl.Height = H
PropertyChanged “Picture”
End Property

Private Function JiexuanPic(N As Integer) '分割图片
On Error GoTo er
UserControl.Cls
If N = 0 Then
UserControl.PaintPicture Pic, 0, 0, Int(ScaleX(Pic.Width)) / 3, Pic.Height, 0, 0, Int(ScaleX(Pic.Width)) / 3, Pic.Height
ElseIf N = 1 Then
UserControl.PaintPicture Pic, 0, 0, Int(ScaleX(Pic.Width)) / 3, Pic.Height, Int(ScaleX(Pic.Width)) / 3, 0, Int(ScaleX(Pic.Width)) / 3, Pic.Height
ElseIf N = 2 Then
UserControl.PaintPicture Pic, 0, 0, Int(ScaleX(Pic.Width)) / 3, Pic.Height, Int(ScaleX(Pic.Width)) / 3 * 2, 0, Int(ScaleX(Pic.Width)) / 3, Pic.Height
End If
W = Int(ScaleX(Pic.Width)) / 3: H = Int(ScaleY(Pic.Height))
er:
Call txtStr(txt)
End Function

Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
But = Button: Butss = True
If But = 1 Then Call JiexuanPic(2)
End Sub

Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseMove(Button, Shift, X, Y)
If But = 1 Then Exit Sub
If (X > 0 And X < UserControl.Width) And (Y > 0 And Y < UserControl.Height) Then
Tim.Interval = 1
Call JiexuanPic(1)
End If
End Sub

Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button > 1 Then Exit Sub
Call JiexuanPic(0)
But = 0
If Button = 1 And Butss = True And (X > 0 And X < UserControl.Width) And (Y > 0 And Y < UserControl.Height) Then
RaiseEvent Click
End If
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag) '读
UserControl.ForeColor = PropBag.ReadProperty(“ForeColor”, RGB(255, 255, 255))
Set Pic = PropBag.ReadProperty(“Picture”, Nothing)
txt = PropBag.ReadProperty(“txt”, “”)
UserControl.Enabled = PropBag.ReadProperty(“Enabled”, “True”)
UserControl.MousePointer = PropBag.ReadProperty(“MousePointer”, 0)
Sw = PropBag.ReadProperty(“SiteWord”, 0)

Zt = PropBag.ReadProperty(“Zt”, “”)
Dx = PropBag.ReadProperty(“Dx”, 9)
Ct = PropBag.ReadProperty(“Ct”, “False”)
Xt = PropBag.ReadProperty(“Xt”, “False”)
Scx = PropBag.ReadProperty(“Scx”, “False”)
Xhx = PropBag.ReadProperty(“Xhx”, “False”)

UserControl.Font.Name = Zt
If Dx > 0 Then
UserControl.Font.Size = Val(Dx)
Else
UserControl.Font.Size = UserControl.FontSize
End If
UserControl.Font.Bold = Ct
UserControl.Font.Italic = Xt
UserControl.Font.Strikethrough = Scx
UserControl.Font.Underline = Xhx

Call JiexuanPic(0)
Call txtStr(txt)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag) '存
Call PropBag.WriteProperty(“ForeColor”, UserControl.ForeColor, RGB(255, 255, 255))
Call PropBag.WriteProperty(“Picture”, Pic, Nothing)
Call PropBag.WriteProperty(“txt”, txt, “”)
Call PropBag.WriteProperty(“Enabled”, UserControl.Enabled, “True”)

Call PropBag.WriteProperty(“Zt”, Zt, “”)
Call PropBag.WriteProperty(“Dx”, Dx, 9)
Call PropBag.WriteProperty(“Ct”, Ct, “False”)
Call PropBag.WriteProperty(“Xt”, Xt, “False”)
Call PropBag.WriteProperty(“Scx”, Scx, “False”)
Call PropBag.WriteProperty(“Xhx”, Xhx, “False”)

Call PropBag.WriteProperty(“MousePointer”, UserControl.MousePointer, 0)
Call PropBag.WriteProperty(“SiteWord”, Sw, 0)
End Sub

Private Sub Tim_Timer()
Dim R As RECT, P As POINTAPI
Call GetWindowRect(UserControl.hwnd, R)
Call GetCursorPos§
If P.X < R.Left Or P.X > R.Right Or P.Y < R.Top Or P.Y > R.Bottom Then
Call JiexuanPic(0)
But = 0: Butss = False
Tim.Interval = 0
End If
End Sub

Public Property Get Caption() As String
Caption = txt
End Property

Public Property Let Caption(ByVal vNewValue As String)
txt = vNewValue
UserControl.Cls
Call JiexuanPic(0)
Call txtStr(vNewValue)
PropertyChanged “Caption”
End Property

Private Sub txtStr(t As String) '标题
If Sw = 0 Then
UserControl.CurrentX = 0
ElseIf Sw = 1 Then
UserControl.CurrentX = (UserControl.Width - UserControl.TextWidth(t)) / 2
ElseIf Sw = 2 Then
UserControl.CurrentX = UserControl.Width - UserControl.TextWidth(t)
End If
UserControl.CurrentY = (UserControl.Height - UserControl.TextHeight(t)) / 2
UserControl.Print t
End Sub

Public Property Get Enabled() As OLE_CANCELBOOL
Enabled = UserControl.Enabled
End Property

Public Property Let Enabled(ByVal vNewValue As OLE_CANCELBOOL)
UserControl.Enabled = vNewValue
If Enabled = True Then
Call JiexuanPic(0)
Else
Call JiexuanPic(2)
End If
PropertyChanged “Enabled”
End Property

'------------------------------------------------------------------------

Public Property Get Font() As StdFont
Set Font = UserControl.Font
End Property

Public Property Set Font(mnewFont As StdFont)
Zt = mnewFont.Name
Dx = mnewFont.Size
Ct = mnewFont.Bold
Xt = mnewFont.Italic
Scx = mnewFont.Strikethrough
Xhx = mnewFont.Underline

UserControl.FontName = Zt
UserControl.FontSize = Dx
UserControl.FontBold = Ct
UserControl.FontItalic = Xt
UserControl.FontStrikethru = Scx
UserControl.FontUnderline = Xhx

Call JiexuanPic(0)
Call txtStr(txt)
PropertyChanged “Font”
End Property
'---------------------------------------------------------------//

Public Property Get MousePointer() As MousePointerConstants
MousePointer = UserControl.MousePointer
End Property

Public Property Let MousePointer(ByVal vNewValue As MousePointerConstants)
UserControl.MousePointer = vNewValue
End Property

Public Property Get SiteWord() As OLE_TRISTATE
SiteWord = Sw
End Property

Public Property Let SiteWord(ByVal vNewValue As OLE_TRISTATE)
UserControl.Cls
Sw = vNewValue
Call JiexuanPic(0)
Call txtStr(txt)
PropertyChanged “SiteWord”
End Property

'将控件拉到Form窗体上,在属性Picture导入提前做好的图片(用PS做一张三格的图片)即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Imports System.ComponentModel _ Public Class DSButton Private _ButtonColor As Color = Color.White Private SF As New System.Drawing.StringFormat Private _Text As String Public Property ButtonColor As Color Get Return _ButtonColor End Get Set(ByVal value As Color) _ButtonColor = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Public Property RoundRectValue As Integer = 10 Private nIndex As Integer = 0 Private IsMouseEnter As Boolean = False Public Property IsShowAnimate As Boolean = False Public Property ButtonText As String Get Return _Text End Get Set(ByVal value As String) _Text = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Private _TextColor As Color = Color.White Public Property TextColor As Color Get Return _TextColor End Get Set(ByVal value As Color) _TextColor = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Private Sub DSButton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SetStyle(ControlStyles.UserPaint, True) SetStyle(ControlStyles.AllPaintingInWmPaint, True) SetStyle(ControlStyles.ResizeRedraw, True) SetStyle(ControlStyles.Selectable, True) SF.LineAlignment = StringAlignment.Center SF.Alignment = StringAlignment.Center MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Sub Private Sub MakeRoundedRect(ByVal Rounded As Integer, ByVal Ct As Control, ByVal ButtonColor As Color) If Ct.BackgroundImage IsNot Nothing Then Ct.BackgroundImage.Dispose() Ct.BackgroundImage = New Bitmap(Ct.Width, Ct.Height) Dim WW, HH As Integer WW = Ct.Width - 1 HH = Ct.Height - 1 Using G As Graphics = Graphics.FromImage(Ct.BackgroundImage) G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit Using Gp As New Drawing2D.GraphicsPath Gp.AddArc(New Rectangle(0, 0, Rounded, Rounded), 180, 90) Gp.AddArc(New Rectangle(WW - Rounded, 0, Rounded, Rounded), -90, 90) Gp.AddArc(New Rectangle(WW - Rounded, HH - Rounded, Rounded, Rounded), 0, 90) Gp.AddArc(New Rectangle(0, HH - Rounded, Rounded, Rounded), 90, 90) Gp.AddLine(New Point(0, HH - Rounded), New Point(0, Rounded / 2)) Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH), ControlPaint.Dark(ButtonColor, 0.5), ButtonColor) G.FillPath(Lg, Gp) G.DrawPath(Pens.Black, Gp) End Using End Using WW = WW - 3 HH = HH - 3 Using Gp As New Drawing2D.GraphicsPath Gp.AddArc(New Rectangle(3, 3, Rounded, Rounded), 180, 90) Gp.AddArc(New Rectangle(WW - Rounded, 3, Rounded, Rounded), -90, 90) Gp.AddArc(New Rectangle(WW - Rounded, HH / 2 - Rounded - 1, Rounded, Rounded), 0, 90) Gp.AddArc(New Rectangle(3, HH / 2 - Rounded - 1, Rounded, Rounded), 90, 90) Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH / 2), Color.FromArgb(220, 255, 255, 255), Color.FromArgb(50, 255, 255, 255)) G.FillPath(Lg, Gp) End Using End Using Using Gp As New Drawing2D.GraphicsPath Gp.AddEllipse(New Rectangle(3, HH / 2 + 10, WW, HH / 2)) Using Lg As New Drawing2D.PathGradientBrush(Gp) Lg.CenterColor = Color.FromArgb(150, 255, 255, 255) Lg.SurroundColors = New Color() {Color.Transparent} Gp.FillMode = Drawing2D.FillMode.Winding G.FillPath(Lg, Gp) End Using End Using Try If _Text.Length 0 Then G.DrawString(_Text, Me.Font, New SolidBrush(TextColor), New Rectangle(0, 0, Me.Width, Me.Height), SF) Catch End Try End Using End Sub Private Sub DSButton_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick End Sub Private Sub DSButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If e.Button = MouseButtons.Left Then MakeRoundedRect(RoundRectValue, Me, Color.Black) End If End Sub Private Sub DSButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter If DesignMode = False Then IsMouseEnter = True Timer1.Enabled = True End If End Sub Private Sub DSButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave If DesignMode = False Then IsMouseEnter = False Timer1.Enabled = True End If End Sub Private Sub DSButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp MakeRoundedRect(RoundRectValue, Me, _ButtonColor) End Sub Private Sub DSButton_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged If Me.IsHandleCreated Then MakeRoundedRect(RoundRectValue, Me, ButtonColor) End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Select Case IsMouseEnter Case True If IsShowAnimate = True Then nIndex = IIf(nIndex + 30 >= 225, 255, nIndex + 30) If nIndex >= 255 Then Timer1.Enabled = False Else nIndex = 255 Timer1.Enabled = False End If Case False nIndex = IIf(nIndex - 20 <= 50, 50, nIndex - 20) If nIndex <= 50 Then Timer1.Enabled = False End Select Try MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * nIndex, ButtonColor.G / 255 * nIndex, ButtonColor.B / 255 * nIndex)) Catch End Try End Sub End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘上的舞指

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值