|
|
Title | Let the user select a color using the CommonDialog control or red, green, and blue sliders |
Keywords | CommonDialog, color, pick, select, RGB, slider |
Categories | Graphics |
|
|
To use the CommonDialog control, set the control's CancelError property to True. Then enable an error handler and call the dialog's ShowColor method.
When the dialog returns, check the Err object to see if the user canceled, if there was an unexpected error, or if the user clicked Ok. If the user clicked Ok, the dialog's Color property contains the selected color value.
|
|
' Select a color using the CommonDialog control.
Private Sub picCommonDialog_Click()
cdlgColor.CancelError = True
On Error Resume Next
cdlgColor.ShowColor
If Err.Number = cdlCancel Then
' The user canceled. Do nothing.
ElseIf Err.Number <> 0 Then
' Unexpected error.
MsgBox "Error " & Format$(Err.Number) & _
" selecting color." & vbCrLf & _
Err.Description, _
vbExclamation Or vbOKOnly, "Error"
Else
' Set the new color.
picCommonDialog.BackColor = cdlgColor.Color
End If
End Sub
|
|
To use the custom color selection dialog shown here, the main program should set the dialog's hscrRed, hscrGreen, and hscrBlue scroll bar controls' values between 0 and 255. It should then show the dialog modally.
When the dialog returns, its Canceled property indicates whether the user clicked the Cancel button.
If the user clicked Ok, read the selected color components from the hscrRed, hscrGreen, and hscrBlue scroll bar controls.
|
|
' Select a color using the color dialog.
Private Sub picScrollBars_Click()
Dim r As Integer
Dim g As Integer
Dim b As Integer
' Initialize the dialog.
UnRGB picScrollBars.BackColor, r, g, b
dlgColor.hscrRed.Value = r
dlgColor.hscrGreen.Value = g
dlgColor.hscrBlue.Value = b
' Display the dialog.
dlgColor.Show vbModal
' See if the user clicked Ok.
If Not dlgColor.Canceled Then
' Apply the change.
picScrollBars.BackColor = _
dlgColor.picSample.BackColor
End If
Unload dlgColor
End Sub
|
|
The custom dialog's code is more interesting. When the value of the hscrRed scroll bar changes, either because the user moved it or because the main program set its value, the control's Change event handler executes. This code begins by checking the m_IgnoreEvents variable. If m_IgnoreEvents is True, then the txtRed TextBox's event handler code (which you'll see in a moment) changed the scroll bar's value. In that case, txtRed already shows the correct value and is about to display it in the sample PictureBox so the hscrRed_Change event handler doesn't need to do anything.
If m_IgnoreEvents is False, the event handler sets m_IgnoreEvents to True so when it sets the value of txtRed, that routine's event handler doesn't do anything. The routine then makes txtRed display the scroll bar's new value and it displays the new color in the picSample PictureBox.
The hscrGreen and hscrBlue controls have similar Change event handlers.
|
|
Private Sub hscrRed_Change()
' Do not recurse.
If m_IgnoreEvents Then Exit Sub
' Display the color value in the scroll bar
' and sample picture.
m_IgnoreEvents = True
txtRed.Text = Format$(hscrRed.Value)
picSample.BackColor = RGB(hscrRed.Value, _
hscrGreen.Value, hscrBlue.Value)
m_IgnoreEvents = False
End Sub
|
|
When the txtRed TextBox control's value is changed (by the user typing a new value), the control's Change event handler executes. This routine also checks the m_IgnoreEvents variable to prevent recursive behavior. It then sets the hscrRed scroll bar's value to the new text value. The code uses an On Error statement to protect itself if the user enters an invalid number such as one containing letters or one that is completely blank.
The txtGreen and txtBlue controls have similar event handlers.
|
|
Private Sub txtRed_Change()
' Do not recurse.
If m_IgnoreEvents Then Exit Sub
' Display the color value in the scroll bar
' and sample picture.
m_IgnoreEvents = True
On Error Resume Next
hscrRed.Value = CInt(txtRed.Text)
If Err.Number <> 0 Then hscrRed.Value = 0
On Error GoTo 0
picSample.BackColor = RGB(hscrRed.Value, _
hscrGreen.Value, hscrBlue.Value)
m_IgnoreEvents = False
End Sub
|
|
For lots more information on graphics in Visual Basic, see my book Visual Basic Graphics Programming.
|
|
|
|
|
|