📄 command button.ctl
字号:
ColorB = 218
For i = t + 3 To B - 2 Step 4
hPen = CreatePen(PS_SOLID, 4, RGB(ColorR, ColorG, ColorB))
Pen = SelectObject(.Hdc, hPen)
MoveToEx .Hdc, l, i, pt
LineTo .Hdc, R, i
SelectObject .Hdc, Pen
DeleteObject hPen
If ColorB >= 218 Then
ColorR = ColorR - 1
ColorG = ColorG - 1
ColorB = ColorB - 1
End If
Next i
'top shadow
hPen = CreatePen(PS_SOLID, 1, RGB(209, 204, 192))
Pen = SelectObject(.Hdc, hPen)
MoveToEx .Hdc, l, t + 1, pt
LineTo .Hdc, R, t + 1
SelectObject .Hdc, Pen
DeleteObject hPen
hPen = CreatePen(PS_SOLID, 1, RGB(220, 216, 207))
Pen = SelectObject(.Hdc, hPen)
MoveToEx .Hdc, l, t + 2, pt
LineTo .Hdc, R, t + 2
SelectObject .Hdc, Pen
DeleteObject hPen
'bottom shadow
hPen = CreatePen(PS_SOLID, 1, RGB(234, 233, 227))
Pen = SelectObject(.Hdc, hPen)
MoveToEx .Hdc, l, B - 3, pt
LineTo .Hdc, R, B - 3
SelectObject .Hdc, Pen
DeleteObject hPen
hPen = CreatePen(PS_SOLID, 1, RGB(242, 241, 238))
Pen = SelectObject(.Hdc, hPen)
MoveToEx .Hdc, l, B - 2, pt
LineTo .Hdc, R, B - 2
SelectObject .Hdc, Pen
DeleteObject hPen
End With
End Sub
Private Sub DrawButtonDisabled()
Dim pt As POINTAPI, Pen As Long, hPen As Long
Dim i As Long, ColorR As Long, ColorG As Long, ColorB As Long
Dim hBrush As Long
With UserControl
.AutoRedraw = True
.Cls
.ScaleMode = 3
hBrush = CreateSolidBrush(RGB(245, 244, 234))
FillRect UserControl.Hdc, rc, hBrush
DeleteObject hBrush
hBrush = CreateSolidBrush(RGB(201, 199, 186))
FrameRect UserControl.Hdc, rc, hBrush
DeleteObject hBrush
'Left top corner
SetPixel .Hdc, l, t + 1, RGB(216, 213, 199)
SetPixel .Hdc, l + 1, t + 1, RGB(216, 213, 199)
SetPixel .Hdc, l + 1, t, RGB(216, 213, 199)
SetPixel .Hdc, l + 1, t + 2, RGB(234, 233, 222)
SetPixel .Hdc, l + 2, t + 1, RGB(234, 233, 222)
'right top corner
SetPixel .Hdc, R - 1, t, RGB(216, 213, 199)
SetPixel .Hdc, R - 1, t + 1, RGB(216, 213, 199)
SetPixel .Hdc, R, t + 1, RGB(216, 213, 199)
SetPixel .Hdc, R - 2, t + 1, RGB(234, 233, 222)
SetPixel .Hdc, R - 1, t + 2, RGB(234, 233, 222)
'left bottom corner
SetPixel .Hdc, l, B - 2, RGB(216, 213, 199)
SetPixel .Hdc, l + 1, B - 2, RGB(216, 213, 199)
SetPixel .Hdc, l + 1, B - 1, RGB(216, 213, 199)
SetPixel .Hdc, l + 1, B - 3, RGB(234, 233, 222)
SetPixel .Hdc, l + 2, B - 2, RGB(234, 233, 222)
'right bottom corner
SetPixel .Hdc, R, B - 2, RGB(216, 213, 199)
SetPixel .Hdc, R - 1, B - 2, RGB(216, 213, 199)
SetPixel .Hdc, R - 1, B - 1, RGB(216, 213, 199)
SetPixel .Hdc, R - 1, B - 3, RGB(234, 233, 222)
SetPixel .Hdc, R - 2, B - 2, RGB(234, 233, 222)
End With
End Sub
Private Sub DrawButton2()
Dim pt As POINTAPI, Pen As Long, hPen As Long
Dim i As Long, ColorR As Long, ColorG As Long, ColorB As Long
Dim hBrush As Long
With UserControl
hBrush = CreateSolidBrush(RGB(0, 60, 116))
FrameRect UserControl.Hdc, rc, hBrush
DeleteObject hBrush
'Left top corner
SetPixel .Hdc, l, t + 1, RGB(122, 149, 168)
SetPixel .Hdc, l + 1, t + 1, RGB(37, 87, 131)
SetPixel .Hdc, l + 1, t, RGB(122, 149, 168)
'SetPixel .hdc, l + 1, t + 2, RGB(191, 206, 220)
'SetPixel .hdc, l + 2, t + 1, RGB(192, 207, 221)
'right top corner
SetPixel .Hdc, R - 1, t, RGB(122, 149, 168)
SetPixel .Hdc, R - 1, t + 1, RGB(37, 87, 131)
SetPixel .Hdc, R, t + 1, RGB(122, 149, 168)
'SetPixel .hdc, r - 2, t + 1, RGB(234, 233, 222)
'SetPixel .hdc, r - 1, t + 2, RGB(234, 233, 222)
'left bottom corner
SetPixel .Hdc, l, B - 2, RGB(122, 149, 168)
SetPixel .Hdc, l + 1, B - 2, RGB(37, 87, 131)
SetPixel .Hdc, l + 1, B - 1, RGB(122, 149, 168)
'SetPixel .hdc, l + 1, b - 3, RGB(234, 233, 222)
'SetPixel .hdc, l + 2, b - 2, RGB(234, 233, 222)
'right bottom corner
SetPixel .Hdc, R, B - 2, RGB(122, 149, 168)
SetPixel .Hdc, R - 1, B - 2, RGB(37, 87, 131)
SetPixel .Hdc, R - 1, B - 1, RGB(122, 149, 168)
'SetPixel .hdc, r - 1, b - 3, RGB(234, 233, 222)
'SetPixel .hdc, r - 2, b - 2, RGB(234, 233, 222)
End With
End Sub
Private Sub RedrawButton(Optional ByVal Stat As Integer = -1)
If mEnabled Then
If Stat = 1 And LastButton = 1 Then
DrawButtonDown
Else
DrawButtonFace
If isOver = True Then
DrawHighlight
Else
If flgFocus = True Then
DrawFocus
End If
End If
End If
DrawButton2
Else
DrawButtonDisabled
End If
DrawCaption
MakeRegion
End Sub
Private Sub DrawCaption()
Dim vh As Long, rcTxt As RECT
With UserControl
GetClientRect .Hwnd, rcTxt
If mEnabled Then
If isOver Then
SetTextColor .Hdc, mForeHover
Else
SetTextColor .Hdc, .ForeColor
End If
Else
SetTextColor .Hdc, RGB(161, 161, 146)
End If
vh = DrawText(.Hdc, mCaption, Len(mCaption), rcTxt, DT_CALCRECT Or DT_CENTER Or DT_WORDBREAK)
'If Button = 1 Then
' SetRect rcTxt, 0, (.ScaleHeight * 0.5) - (vh * 0.5) + 1, .ScaleWidth, (.ScaleHeight * 0.5) + (vh * 0.5) + 1
' DrawText .hdc, mCaption, Len(mCaption), rcTxt, DT_CENTER Or DT_WORDBREAK
'Else
SetRect rcTxt, 0, (.ScaleHeight * 0.5) - (vh * 0.5), .ScaleWidth, (.ScaleHeight * 0.5) + (vh * 0.5)
DrawText .Hdc, mCaption, Len(mCaption), rcTxt, DT_CENTER Or DT_WORDBREAK
'End If
End With
End Sub
Private Sub HoverTimer_Timer()
If Not isMouseOver Then
HoverTimer.Enabled = False
isOver = False
flgHover = 0
RedrawButton 0
RaiseEvent MouseOut
End If
End Sub
Private Sub UserControl_AccessKeyPress(KeyAscii As Integer)
LastButton = 1
Call UserControl_Click
End Sub
Private Sub UserControl_Click()
If LastButton = 1 Then
RedrawButton 0
' LastButton = 1
' RedrawButton 1
UserControl.Refresh
RaiseEvent Click
End If
End Sub
Private Sub UserControl_DblClick()
If LastButton = 1 Then
Call UserControl_MouseDown(1, 0, 0, 0)
SetCapture Hwnd
End If
End Sub
Private Sub UserControl_GotFocus()
flgFocus = True
If mEnabled = True Then
LastButton = 1
UserControl.Refresh
RedrawButton 0
End If
End Sub
Private Sub UserControl_InitProperties()
Set UserControl.Font = Ambient.Font
mCaption = "Command" & Mid(Ambient.DisplayName, 9)
mEnabled = True
End Sub
Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
LastKey = KeyCode
Select Case KeyCode
Case vbKeySpace, vbKeyReturn
RedrawButton 1
Case vbKeyLeft, vbKeyRight 'right and down arrows
SendKeys "{Tab}"
Case vbKeyDown, vbKeyUp 'left and up arrows
SendKeys "+{Tab}"
End Select
RaiseEvent KeyDown(KeyCode, Shift)
End Sub
Private Sub UserControl_KeyPress(KeyAscii As Integer)
RaiseEvent KeyPress(KeyAscii)
End Sub
Private Sub UserControl_KeyUp(KeyCode As Integer, Shift As Integer)
If ((KeyCode = vbKeySpace) And (LastKey = vbKeySpace)) Or ((KeyCode = vbKeyReturn) And (LastKey = vbKeyReturn)) Then
RedrawButton 0
LastButton = 1
UserControl.Refresh
RaiseEvent Click
End If
RaiseEvent KeyUp(KeyCode, Shift)
End Sub
Private Sub UserControl_LostFocus()
flgFocus = False
RedrawButton 0
End Sub
Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If mEnabled = True Then
RaiseEvent MouseDown(Button, Shift, X, Y)
LastButton = Button
UserControl.Refresh
DoEvents
RedrawButton 1
End If
End Sub
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' UserControl_GotFocus
If Button < 2 Then
If Not isMouseOver Then
If flgHover = 0 Then Exit Sub
RedrawButton 0
Else
If flgHover = 1 Then Exit Sub
flgHover = 1
If Button = 0 And Not isOver Then
HoverTimer.Enabled = True
isOver = True
flgHover = 0
RedrawButton 0
RaiseEvent MouseOver
ElseIf Button = 1 Then
isOver = True
RedrawButton 1
isOver = False
End If
End If
End If
RaiseEvent MouseMove(Button, Shift, X, Y)
End Sub
Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseUp(Button, Shift, X, Y)
RedrawButton 0
UserControl.Refresh
End Sub
Private Sub UserControl_Resize()
GetClientRect UserControl.Hwnd, rc
With rc
R = .Right - 1: l = .Left: t = .Top: B = .Bottom
W = .Right: H = .Bottom
End With
RedrawButton 0
End Sub
Private Function isMouseOver() As Boolean
Dim pt As POINTAPI
GetCursorPos pt
isMouseOver = (WindowFromPoint(pt.X, pt.Y) = Hwnd)
End Function
Private Sub MakeRegion()
DeleteObject rgMain
rgMain = CreateRectRgn(0, 0, W, H)
rgn1 = CreateRectRgn(0, 0, 1, 1) 'Left top coner
CombineRgn rgMain, rgMain, rgn1, RGN_DIFF
DeleteObject rgn1
rgn1 = CreateRectRgn(0, H - 1, 1, H) 'Left bottom corner
CombineRgn rgMain, rgMain, rgn1, RGN_DIFF
DeleteObject rgn1
rgn1 = CreateRectRgn(W - 1, 0, W, 1) 'Right top corner
CombineRgn rgMain, rgMain, rgn1, RGN_DIFF
DeleteObject rgn1
rgn1 = CreateRectRgn(W - 1, H - 1, W, H) 'Right bottom corner
CombineRgn rgMain, rgMain, rgn1, RGN_DIFF
DeleteObject rgn1
SetWindowRgn UserControl.Hwnd, rgMain, True
End Sub
Public Property Get Enabled() As Boolean
Enabled = mEnabled
End Property
Public Property Let Enabled(ByVal NewValue As Boolean)
mEnabled = NewValue
PropertyChanged "Enabled"
UserControl.Enabled = NewValue
RedrawButton 0
End Property
Public Property Get Font() As Font
Set Font = UserControl.Font
End Property
Public Property Set Font(ByVal NewValue As Font)
Set UserControl.Font = NewValue
RedrawButton 0
PropertyChanged "Font"
End Property
Public Property Get Caption() As String
Caption = mCaption
End Property
Public Property Let Caption(ByVal NewValue As String)
mCaption = NewValue
RedrawButton 0
SetAccessKeys
PropertyChanged "Caption"
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End Property
Public Property Let ForeColor(ByVal NewValue As OLE_COLOR)
UserControl.ForeColor = NewValue
RedrawButton 0
PropertyChanged "ForeColor"
End Property
Public Property Get ForeHover() As OLE_COLOR
ForeHover = mForeHover
End Property
Public Property Let ForeHover(ByVal NewValue As OLE_COLOR)
mForeHover = NewValue
PropertyChanged "ForeHover"
End Property
Private Sub UserControl_Show()
RedrawButton 0
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
With PropBag
mEnabled = .ReadProperty("Enabled", True)
Set UserControl.Font = .ReadProperty("Font", Ambient.Font)
mCaption = .ReadProperty("Caption", Ambient.DisplayName)
UserControl.ForeColor = .ReadProperty("ForeColor", Ambient.ForeColor)
mForeHover = .ReadProperty("ForeHover", UserControl.ForeColor)
End With
UserControl.Enabled = mEnabled
SetAccessKeys
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
With PropBag
.WriteProperty "Enabled", mEnabled, True
.WriteProperty "Font", UserControl.Font, Ambient.Font
.WriteProperty "Caption", mCaption, Ambient.DisplayName
.WriteProperty "ForeColor", UserControl.ForeColor
.WriteProperty "ForeHover", mForeHover, Ambient.ForeColor
End With
End Sub
Private Sub SetAccessKeys()
Dim i As Long
UserControl.AccessKeys = ""
If Len(mCaption) > 1 Then
i = InStr(1, mCaption, "&", vbTextCompare)
If (i < Len(mCaption)) And (i > 0) Then
If Mid$(mCaption, i + 1, 1) <> "&" Then
UserControl.AccessKeys = LCase$(Mid$(mCaption, i + 1, 1))
Else
i = InStr(i + 2, mCaption, "&", vbTextCompare)
If Mid$(mCaption, i + 1, 1) <> "&" Then
UserControl.AccessKeys = LCase$(Mid$(mCaption, i + 1, 1))
End If
End If
End If
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -