如何設定命令鈕的「前景顏色」(ForeColor)?Part II


Q: 如何設定命令鈕的「前景顏色」(ForeColor)?Part II
A: 如何設定命令鈕的「前景顏色」(ForeColor)?Part I, 必須事先存檔, 遇到必須修改標題(Caption)時, 就比較麻煩。

其實我們可以在表單上佈置一個 PictureBox, 並且將 PictureBox 的 Visible 設定為 False, 將 BorderStyle 屬性設定為 "0 - 沒有框線", 然後利用以下方法機動地設定命令鈕的前景顏色:



Command1.Caption = "命令鈕的標題"
Picture1.ForeColor = RGB(255, 0, 0)
' 先設定 PictureBox 的 ForeColor 屬性
SetButtonColorFromPicture Command1, Picture1
' SetButtonColorFromPicture 的用途是:在 PictureBox 上面輸出 Command1.Caption,
' 然後將 PictureBox 上面的圖像設定給 Command1.Picture
' 請注意 Command1 的 Style 屬性必須在設計階段先設定成 "1 - 圖片外觀"

Sub SetButtonColorFromPicture(Button As CommandButton, P As PictureBox)

Dim Cap As String

Cap = Button.Caption
Button.Caption = ""
P.Cls
P.AutoRedraw = True

P.BackColor = Button.BackColor
Set P.Font = Button.Font
P.Height = Button.Height
P.Width = Button.Width
P.ScaleMode = Me.ScaleMode

P.CurrentX = (P.Width - P.TextWidth(Cap)) / 2
P.CurrentY = (P.Height - P.TextHeight(Cap)) / 2
P.Print Cap
Set Button.Picture = P.Image
P.AutoRedraw = False

End Sub