@Anonymous wrote:
Nice it work with "Application" soluce 🙂
I supposed i must do the same with :
Me.BackColor = cc.rgbResult
And replace by :
Application.BackColor = cc.rgbResult
Nope, you'd have to change
Me.BackColor = cc.rgbResult
to
Me.BackColor = CLng(cc.rgbResult)
But my understanding from your last post is that most of the code you showed is not of your actual interest while you only need a way to have the user pick a color and then use it in some way.
So, no need for using that color and change form controls background color (maybe even those Option1, Option2, Option3, Check1 and Command1 controls are not actual controls of your form?)
hence, here's the updated code with some chosen color usage examples:
1) place the following at the very top of any module (hence, NOT in any UserForm code pane)
Option Explicit
Public Declare PtrSafe Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (lpcc As CHOOSECOLORSTRUCT) As LongPtr
Public Type CHOOSECOLORSTRUCT
lStructSize As LongPtr
hwndOwner As LongPtr
hInstance As LongPtr
rgbResult As LongPtr
lpCustColors As String
flags As LongPtr
lCustData As LongPtr
lpfnHook As LongPtr
lpTemplateName As String
End Type
2) place the following in your UserForm code pane
Option Explicit
Private dwCustClrs(0 To 15) As Byte
'ChooseColor structure flag constants
Private Const CC_RGBINIT As Long = &H1
Private Const CC_FULLOPEN As Long = &H2
Private Const CC_PREVENTFULLOPEN As Long = &H4
Private Const CC_SOLIDCOLOR As Long = &H80
Private Const CC_ANYCOLOR As Long = &H100
Public Sub UserForm_Initialize()
Dim cnt As Long
With Me '<--| skip this block if your form doesn't actually have these controls
.Option1.Caption = "Display normally"
.Option1.Value = True
.Option2.Caption = "Display with Define Custom Colors open"
.Option3.Caption = "Disable Define Custom Colors button"
.Check1.Caption = "Specify initial colour is form BackColor"
.Command1.Caption = "Choose Color"
End With
End Sub
Public Sub Command1_Click()
Dim cc As CHOOSECOLORSTRUCT
Dim r As Long
Dim g As Long
Dim b As Long
Me.hide
With cc
'set the flags based on the check and option buttons <--| skip this section if your form actually hasn't these controls
.flags = CC_ANYCOLOR
If Me.Option2.Value = True Then .flags = .flags Or CC_FULLOPEN
If Me.Option3.Value = True Then .flags = .flags Or CC_PREVENTFULLOPEN
If Me.Check1.Value = 1 Then .flags = .flags Or CC_RGBINIT
'size of structure
.lStructSize = Len(cc)
'owner of the dialog
.hwndOwner = Application.HWND
'assign the custom colour selections
.lpCustColors = StrConv(dwCustClrs, vbUnicode)
End With
If CHOOSECOLOR(cc) = 1 Then
'get chosen color RGB values
GetRBGFromCLRREF CLng(cc.rgbResult), r, g, b
' some chosen color usage examples
'--------------------------------
' example 1: show RGB values
MsgBox "chosen color RGB values are" & vbcrlf & vbcrlf & "red: " & vbTab & r & vbCrLf & "green: " & vbTab & g & vbCrLf & "blue: " & vbTab & b
'--------------------------------
'--------------------------------
' example 2: color a user selected entity
' instance an AcCmColor object and set its RGB values to the chosen color ones
Dim color As AcadAcCmColor
Set color = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.19")
color.SetRGB r, g, b
'make the user choose an entity
Dim ent As AcadEntity
Dim basePnt As Variant
ThisDrawing.Utility.GetEntity ent, basePnt, "Select an object"
'set selected entity color to the chosen one
ent.TrueColor = color
'--------------------------------
End If
End Sub
Public Sub GetRBGFromCLRREF(ByVal clrref As Long, r As Long, g As Long, b As Long)
'pass a hex colour, return the rgb components
b = (clrref \ 65536) And &HFF
g = (clrref \ 256) And &HFF
r = clrref And &HFF
End Sub