|
-
Aug 12th, 2000, 09:37 AM
#1
Thread Starter
Hyperactive Member
Hi,
Code below allows to call the ShowColor API wihtout using MS COMDLG.OCX that comes with VB.
It's a SUB.
As you can see, it sets a backcolour of a form in which the sub resides.
Can anyone tell me how convert it to a function, so I can pass any control as a parameter and set this control's backcolour without being restricted to just the form.
' Code:
'----------------------------------------------------
' Form module
'----------------------------------------------------
Private Sub ChooseColour_Click()
Dim CustomColours() As Byte
' Define array for custom colours.
ReDim CustomColours(0 To 15) As Byte
' Resize the array to hold the elements.
Dim tChooseColour As CHOOSECOLOR
' Declare a user-defined variable for the ChooseColour
' type structure.
With tChooseColour
.hwndOwner = Me.hwnd
' Set the handle for the owner of the window.
.lpCustColors = StrConv(CustomColours, vbUnicode)
' Pass the custom colours array after converting
' it to Unicode using the StrConv function.
.flags = 0&
' For this sample, we do not need to use this.
.lStructSize = Len(tChooseColour)
' Set the size of the type structure.
End With
If ShowColour(tChooseColour) = 0 Then MsgBox _
"You clicked on the 'Cancel' button", _
vbExclamation: Exit Sub
' Call the API function and display the ChooseColour
' Common Dialog box, and if the users clicks on cancel
' then the function will return 0.
Me.BackColor = tChooseColour.rgbResult
' Set the back colour of the form with the colour
' that the user selected.
End Sub
'----------------------------------------------------
' Standard module
'----------------------------------------------------
Option Explicit
Public Type CHOOSECOLOR
lStructSize As Long
hwndOwner As Long
hInstance As Long
rgbResult As Long
lpCustColors As String
flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Declare Function ShowColour Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
'----------------------------------------------------
Thanks for your help.
-
Aug 12th, 2000, 09:49 AM
#2
Try this:
Code:
Function ChooseColour(ctl As Control)
Dim CustomColours() As Byte
ReDim CustomColours(0 To 15) As Byte
Dim tChooseColour As CHOOSECOLOR
With tChooseColour
.hwndOwner = Me.hwnd
.hInstance = App.hInstance
.lpCustColors = StrConv(CustomColours, vbUnicode)
.flags = 0&
.lStructSize = Len(tChooseColour)
End With
If ShowColour(tChooseColour) = 0 Then MsgBox "You clicked on the 'Cancel' button", vbExclamation: Exit Function
ctl.BackColor = tChooseColour.rgbResult
'Return a the colour
ChooseColour = tChooseColour.rgbResult
End Function
Usage
Code:
Private Sub Command1_Click()
'Call the function
retval = ChooseColour(Picture1)
End Sub
-
Aug 12th, 2000, 10:11 AM
#3
Thread Starter
Hyperactive Member
Thanks Megatron,
Is there a way to put this function in a standard module or make it Public so I can access Color Dialog from other Forms within my app without including the function in each form?
By the way, (in your replys), how do you display your code like in a VB editor (with indentations, colours, etc.)? This is much more readable.
Thanks
-
Aug 12th, 2000, 10:21 AM
#4
To make it public, place the function in a Module. But when you decide which control you want to change, you must also specify the Form, i.e:
Code:
retval = ChooseColour(Form1.Picture1)
To make your code like the VB IDE, enclose them in code tags.
[code]
MyCode Here
[/code]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|