Results 1 to 11 of 11

Thread: (RESOLVED) Save custom colors

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved (RESOLVED) Save custom colors

    Hey All,

    I'm using the ChooseColor API to select a color. Is there any way to save the
    custom colors a user selects?

    Thanks in advance for any help,
    Ron



    VB Code:
    1. Option Explicit
    2.  
    3.    Private Type CHOOSECOLOR
    4.      lStructSize As Long
    5.      hwndOwner As Long
    6.      hInstance As Long
    7.      rgbResult As Long
    8.      lpCustColors As String
    9.      flags As Long
    10.      lCustData As Long
    11.      lpfnHook As Long
    12.      lpTemplateName As String
    13.    End Type
    14.  
    15.    Private Declare Function ChooseColorAPI Lib "comdlg32.dll" Alias _
    16.      "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
    17.  
    18.    Dim CustomColors() As Byte
    19.  
    20. Private Sub Form_Load()
    21.        ReDim CustomColors(0 To 16 * 4 - 1) As Byte
    22.        Dim i As Integer
    23.  
    24.        For i = LBound(CustomColors) To UBound(CustomColors)
    25.            CustomColors(i) = 0
    26.        Next i
    27. End Sub
    28.  
    29. Private Sub Command1_Click()
    30.        Dim cc As CHOOSECOLOR
    31.        Dim Custcolor(16) As Long
    32.        Dim lReturn As Long
    33.        cc.lStructSize = Len(cc)
    34.        cc.hwndOwner = Me.hWnd
    35.        cc.hInstance = 0
    36.        cc.lpCustColors = StrConv(CustomColors, vbUnicode)
    37.        cc.flags = 0
    38.        lReturn = ChooseColorAPI(cc)
    39.        If lReturn <> 0 Then
    40.            Text1.Text = cc.rgbResult
    41.            Label1.ForeColor = cc.rgbResult
    42.            CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
    43.        Else
    44.            'MsgBox "User chose the Cancel Button"
    45.        End If
    46.    End Sub
    Last edited by rdcody; Sep 15th, 2005 at 05:16 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Save custom colors

    I just use the Common Dialog Control to change the colors. I click on a label to change it's color, and save them to a file. When I load the program, it reads the file and loads the label colors. I also put the CDC control on a small form, and can move the location of the form before I show it.

    Here is how to use the CDC control. I added the Show Color today.

    EDIT: After 66 views, I'm updating to version 6 that asks if you want to overwrite in a save operation.
    Attached Files Attached Files
    Last edited by dglienna; Jan 31st, 2006 at 01:01 PM.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save custom colors

    Change the lpCustColors member to a Long instead of a string and set it to VarPtr(CustColors(0)) instead of using StrConv. You can then store these values somewhere and load them before the call.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Save custom colors

    Thanks dglienna, but I am/want to use the ChooseColor API.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Save custom colors

    Hey Joacim,

    So the code would be....

    VB Code:
    1. cc.lpCustColors = VarPtr(CustomColors(0))

    instead of...

    VB Code:
    1. cc.lpCustColors = StrConv(CustomColors, vbUnicode)

    Is this what your talking about?

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save custom colors

    Yes. But you must of course also change the type from String to Long in the CHOOSECOLOR structure.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Save custom colors

    I don't understand how to use this. I mean there are 16 custom color boxes,
    how do I save the color number for each box?

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save custom colors

    OK. I've made some slight changes to your code. I've changed the structure to use a Long for the lpCustColor member. I've also changed your CustomColors() array to be of the type Long instead of Byte. I removed the local Custcolor() array you had in the Command1_Click event since that doesn't do anything. Instead I use the CustomColors() array.

    The changes are in bold below.
    VB Code:
    1. Private Type CHOOSECOLOR
    2.     lStructSize As Long
    3.     hwndOwner As Long
    4.     hInstance As Long
    5.     rgbResult As Long
    6.     lpCustColors[b] As Long [/b]
    7.     flags As Long
    8.     lCustData As Long
    9.     lpfnHook As Long
    10.     lpTemplateName As String
    11. End Type
    12.  
    13. Private Declare Function ChooseColorAPI Lib "comdlg32.dll" Alias _
    14.   "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
    15.  
    16. Private CustomColors()[b] As Long [/b]
    17.  
    18. Private Sub Form_Load()
    19. [b]    ReDim CustomColors(15) As Long 'index 0 to 15 = 16 colors [/b]
    20.     Dim i As Integer
    21.    
    22.     For i = LBound(CustomColors) To UBound(CustomColors)
    23.         CustomColors(i) = 0
    24.     Next i
    25. End Sub
    26.  
    27. Private Sub Command1_Click()
    28.     Dim cc As CHOOSECOLOR
    29.     Dim lReturn As Long
    30.     cc.lStructSize = Len(cc)
    31.     cc.hwndOwner = Me.hWnd
    32.     cc.hInstance = 0
    33.     cc.lpCustColors =[b] VarPtr(CustomColors(0)) [/b]
    34.     cc.flags = 0
    35.     lReturn = ChooseColorAPI(cc)
    36.     If lReturn <> 0 Then
    37.         Text1.Text = cc.rgbResult
    38.         Label1.ForeColor = cc.rgbResult
    39. [b]        'The CustomColors() array now contains all the custom colors
    40.         'that the user has selected. You can save this array values
    41.         'to a file if you want and reload them in Form_Load.
    42.         'Next time you click this button these values are used again! [/b]
    43.     Else
    44.         'MsgBox "User chose the Cancel Button"
    45.     End If
    46. End Sub

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Save custom colors

    Hey thanks Joacim ...now if I can figure out to write/read that
    array to an *.ini file, I'll be set.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save custom colors

    Why save them to an INI file? Why not use a binary file.
    VB Code:
    1. Private Type CHOOSECOLOR
    2.     lStructSize As Long
    3.     hwndOwner As Long
    4.     hInstance As Long
    5.     rgbResult As Long
    6.     lpCustColors As Long
    7.     flags As Long
    8.     lCustData As Long
    9.     lpfnHook As Long
    10.     lpTemplateName As String
    11. End Type
    12.  
    13. Private Declare Function ChooseColorAPI Lib "comdlg32.dll" Alias _
    14.   "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
    15.  
    16. Private CustomColors() As Long
    17. [b]Private sFileName As String [/b]
    18.  
    19. Private Sub Form_Load()
    20. [b]    Dim hFile As Integer [/b]
    21.     ReDim CustomColors(15) As Long 'index 0 to 15 = 16 colors
    22. [b]    sFileName = App.Path & "\CustClrs.dat"
    23.     'Check if the file exists
    24.     If Len(Dir$(sFileName)) > 0 Then
    25.         'read the file
    26.         hFile = FreeFile
    27.         Open sFileName For Binary Access Read As #hFile
    28.             Get #hFile, , CustomColors
    29.         Close #hFile
    30.     End If [/b]
    31. End Sub
    32.  
    33. Private Sub Command1_Click()
    34.     Dim cc As CHOOSECOLOR
    35.     Dim lReturn As Long
    36. [b]    Dim hFile As Integer [/b]
    37.     cc.lStructSize = Len(cc)
    38.     cc.hwndOwner = Me.hWnd
    39.     cc.hInstance = 0
    40.     cc.lpCustColors = VarPtr(CustomColors(0))
    41.     cc.flags = 0
    42.     lReturn = ChooseColorAPI(cc)
    43.     If lReturn <> 0 Then
    44.         Text1.Text = cc.rgbResult
    45.         Label1.ForeColor = cc.rgbResult
    46. [b]        'Save the custom colors
    47.         hFile = FreeFile
    48.         Open sFileName For Binary Access Write As #hFile
    49.             Put #hFile, , CustomColors
    50.         Close #hFile [/b]
    51.     Else
    52.         'MsgBox "User chose the Cancel Button"
    53.     End If
    54. End Sub

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Save custom colors

    That's the answer! Thanks a lot
    Joacim. I don't know where my heads been today...I've had that brain fog
    all day. I'll be sure and give you an excellent rating! Thanks again and have
    a good one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width