|
|
Title | Use the CommonDialog control to select a color |
Keywords | CommonDialog, color, get color, select color, ShowColor |
Categories | Graphics, Controls |
|
|
Use the dialog's ShowColor method.
To determine whether the user canceled, set the dialog's CancelError property to True, use On Error Resume Next, and watch for the cdlCancel error.
|
|
Private Sub Command1_Click()
dlgColor.CancelError = True
On Error Resume Next
dlgColor.ShowColor
If Err.Number = cdlCancel Then
Exit Sub
ElseIf Err.Number <> 0 Then
MsgBox "Error " & Format$(Err.Number) & _
" selecting color." & vbCrLf & Err.Description
Exit Sub
End If
BackColor = dlgColor.Color
ShowColors
End Sub
|
|
This example also displays the color's red, green, and blue components. It shows how to use the GetSysColor API function to get the RGB color value for system colors.
|
|
Private Sub ShowColors()
Dim clr As OLE_COLOR
' If the color is a system color, get its RGB value.
clr = BackColor
lblColor.Caption = Hex$(clr)
If clr And &H80000000 Then clr = GetSysColor(clr And _
&HFFFFFF)
' Display the hex value.
lblRGB.Caption = Hex$(clr)
' Display the color component values.
lblRed.Caption = Hex$(clr And &HFF&)
lblGreen.Caption = Hex$((clr And &HFF00&) \ &H100&)
lblBlue.Caption = Hex$((clr And &HFF0000) \ &H10000)
End Sub
|
|
|
|
|
|