ezpresso
ezpresso

Reputation: 8126

A Color Picker Control for Visual Basic 6 (VB6)

I am looking for a free color picker control for Visual Basic 6. Something like this or this. Is there any?

Update. Here is what I found so far:

  1. A Photoshop-style Color Picker
  2. Color Picker Control with Sample Code

Upvotes: 0

Views: 18353

Answers (3)

Bruce Munck
Bruce Munck

Reputation: 31

I'm running VB6 Enterprise Edition, so I don't know if this applies to Pro and Standard. When I click on the View menu in the main VB6 IDE I have a choice for "Color Palette" which opens up a color picker that allows custom colors to be mixed and used. It works great as it will accept rgb values or you can just use the slider to mix colors on the fly.

Upvotes: 0

rskar
rskar

Reputation: 4657

Have a look at: https://web.archive.org/web/20111001144343/http://www.devx.com/vb2themax/Tip/19257

It's supposed to show the standard color dialog (ChooseColor API in comdlg32.dll).


For convenience, here's the code:

Private Type ChooseColorStruct
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    rgbResult As Long
    lpCustColors As Long
    flags As Long
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" _
    (lpChoosecolor As ChooseColorStruct) As Long
Private Declare Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor _
    As Long, ByVal lHPalette As Long, lColorRef As Long) As Long
    
Private Const CC_RGBINIT = &H1&
Private Const CC_FULLOPEN = &H2&
Private Const CC_PREVENTFULLOPEN = &H4&
Private Const CC_SHOWHELP = &H8&
Private Const CC_ENABLEHOOK = &H10&
Private Const CC_ENABLETEMPLATE = &H20&
Private Const CC_ENABLETEMPLATEHANDLE = &H40&
Private Const CC_SOLIDCOLOR = &H80&
Private Const CC_ANYCOLOR = &H100&
Private Const CLR_INVALID = &HFFFF


' Show the common dialog for choosing a color.
' Return the chosen color, or -1 if the dialog is canceled
'
' hParent is the handle of the parent form
' bFullOpen specifies whether the dialog will be open with the Full style 
' (allows to choose many more colors)
' InitColor is the color initially selected when the dialog is open

' Example:
'    Dim oleNewColor As OLE_COLOR
'    oleNewColor = ShowColorsDialog(Me.hwnd, True, vbRed)
'    If oleNewColor <> -1 Then Me.BackColor = oleNewColor

Function ShowColorDialog(Optional ByVal hParent As Long, _
    Optional ByVal bFullOpen As Boolean, Optional ByVal InitColor As OLE_COLOR) _
    As Long
    Dim CC As ChooseColorStruct
    Dim aColorRef(15) As Long
    Dim lInitColor As Long
  
    ' translate the initial OLE color to a long value
    If InitColor <> 0 Then
        If OleTranslateColor(InitColor, 0, lInitColor) Then
            lInitColor = CLR_INVALID
        End If
    End If
    
    'fill the ChooseColorStruct struct
    With CC
        .lStructSize = Len(CC)
        .hwndOwner = hParent
        .lpCustColors = VarPtr(aColorRef(0))
        .rgbResult = lInitColor
        .flags = CC_SOLIDCOLOR Or CC_ANYCOLOR Or CC_RGBINIT Or IIf(bFullOpen, _
            CC_FULLOPEN, 0)
    End With
    
    ' Show the dialog
    If ChooseColor(CC) Then
        'if not canceled, return the color
        ShowColorDialog = CC.rgbResult
    Else
        'else return -1
        ShowColorDialog = -1
    End If
End Function

Upvotes: 7

MarkJ
MarkJ

Reputation: 30398

There is a color-picker built into VB6. The common dialog control can be used as a color picker.

Here's the code example from the VB6 manual

Private Sub Command1_Click()
  ' Set Cancel to True
  CommonDialog1.CancelError = True
  On Error GoTo ErrHandler
  'Set the Flags property
  CommonDialog1.Flags = cdlCCRGBInit
  ' Display the Color Dialog box
  CommonDialog1.ShowColor
  ' Set the form's background color to selected color 
  Form1.BackColor = CommonDialog1.Color
  Exit Sub

ErrHandler:
  ' User pressed the Cancel button
End Sub

Upvotes: 2

Related Questions