Display Color Picker (1 Viewer)

Teggun

Registered User.
Local time
Tomorrow, 00:12
Joined
Aug 30, 2019
Messages
33
Hi guys, I'm trying to create a simulator of a specific product I use in my project which depends, on the colour it has.
So in a form, I want to change different parts of this product's color (I'm using rectangles) by just clicking on the control and display the Access color picker to choose it, nd of course after doing so, capture the color code and change the control's backcolor.

I cound not find any command to display this picker, only a web where they show a sample to do so, where they want to make me pay to download it...
I'm looking for something like this, just to display the color picker.

¿Any idea how this command or macro would look like? I want to just place it in the On Click event.

Thanks for your time.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:12
Joined
Oct 29, 2018
Messages
21,447
Don't know but maybe take a look here?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:12
Joined
May 21, 2018
Messages
8,519
There is one and other stuff in here.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:12
Joined
May 21, 2018
Messages
8,519
Does this help?
Probably not. Isladogs had to do quite a few conversions to handle 64 bit beyond simple ptrsafe.

Code:
Option Compare Database
Option Explicit

#If VBA7 Or Win64 Then
    Private Type ChooseColorStruct
        lStructSize As Long
        hwndOwner As LongPtr
        hInstance As LongPtr
        rgbResult As Long
        lpCustColors As LongPtr
        flags As Long
        lCustData As LongPtr
        lpfnHook As LongPtr
        lpTemplateName As String
    End Type

   Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As ChooseColorStruct) As LongPtr
  
   Private Declare PtrSafe Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor _
                                As Long, ByVal lHPalette As Long, lColorRef As Long) As Long
    
#Else
    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
    
#End If


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
#If Win64 Then
CC.lStructSize = LenB(CC)
#Else
CC.lStructSize = Len(CC)
#End If
    'fill the ChooseColorStruct struct
    With CC
    #If Win64 Then
        .lStructSize = LenB(CC)
    #Else
        .lStructSize = Len(CC)
    #End If
        .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
 

isladogs

MVP / VIP
Local time
Today, 23:12
Joined
Jan 14, 2017
Messages
18,209
As already mentioned by @Minty and @MajP, my colour converter/selector app includes a colour picker … and its free 😁
Its also available in my website where you can see a screenshot

@MajP was right. Getting it to work in 64-bit was more complex than most conversions.
 
Last edited:

Teggun

Registered User.
Local time
Tomorrow, 00:12
Joined
Aug 30, 2019
Messages
33
Thanks guys for your answers, I did manage to implement it in my project even me having low knowledge on this advanced coding.
¡Special thanks to @isladogs for this amazing function!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:12
Joined
May 7, 2009
Messages
19,227
surely, there must be a simpler Color Picker without much coding.
 

Attachments

  • agpColorPicker.zip
    26.6 KB · Views: 317

Minty

AWF VIP
Local time
Today, 23:12
Joined
Jul 26, 2013
Messages
10,367
Hmm, If I'm getting paid by the line, I'm not going to be using that. :p ;)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:12
Joined
May 21, 2018
Messages
8,519
I am not an API writer, but correct me if I am wrong. The second example is mainly shorter because it strips out much of the features of the original code like initial colors, full or partial style, ability to associate with given hwnd, handling the cancel event, etc. I do not know if those other features are useful, that is a different argument, but in writing shorter code that does less should not be revelation or a surprise to anyone. Maybe I am missing something.

Sorry for the rant, but I guess this is my personal Peev. In my opinion i do not think coders should care if a module, class, or function that works well and is designed well could be simpler. You do not get "paid by the line" in most cases. You should only care that the code is usable, flexible, and stable. Code modules and classes should be black boxes that you need to understand the interfaces not the inner workings. I need to understand how to use the watch not how it was built. I routinely use code that I either do not have the time or expertise to understand, but can use it. If I need to resize a form I use @isladogs form resizer. Never even tried to look at the code (like I said not my area of expertise), just the details on how to use it.
In both cases the function is called by a single line of code, so they are equally as easy to use.

I will routinely post a class modules here that do all kinds of things and take 100s to 1000s of lines of code. However, it only takes one line of code to instantiate all the functionality every time it is used. Users will say "that looks too complicated, I can do that in just this 30 lines of code". So yes they can do 1/10th of the functionality and rewrite the 30 lines of code each time. I will just rewrite a single line of code for every instantiation in the future.

Sure if the code is poorly written, clunky, inefficent then that is a different story. I do not think that is what we are talking.
 

isladogs

MVP / VIP
Local time
Today, 23:12
Joined
Jan 14, 2017
Messages
18,209
@MajP makes some important points above.

I had seen the code used by @arnelgp before on the MSDN website.
It opens to the concise colour picker but the custom colours are available on a button click
Cancel does still work in that code

I chose to use the longer version in various apps as it can be opened direct to either the concise or custom colour picker by setting an argument true or false. It can indeed work on any form using hWnd whereas the shorter version would I believe need adapting.

However for most purposes, the simpler code would probably suffice.
 

Users who are viewing this thread

Top Bottom