Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Using the ChooseColor Common Dialog

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using the ChooseColor Common Dialog

    This small PB/DLL program demonstrates the use of the ChooseColor common dialog. If you change the two MSGBOX lines to PRINT and add a WAITKEY$ at the end, the program can also be compiled with PB/CC.

    Code:
    $DIM ALL
    $REGISTER NONE
    $COMPILE EXE
     
    $INCLUDE "WIN32API.INC"
    $INCLUDE "COMDLG32.INC"
     
    FUNCTION PBMain AS LONG
     
        DIM ColorSpec AS LOCAL CHOOSECOLORAPI
        DIM lResult   AS LOCAL LONG
        DIM lCounter  AS LOCAL LONG
     
        DIM lCustomColor(15) AS LOCAL LONG 'array
     
        ColorSpec.lStructSize  = LEN(ColorSpec)
        ColorSpec.hwndOwner    = 0     'Handle of owner window.  If 0, dialog appears at top/left.
        ColorSpec.lpCustColors = VARPTR(lCustomColor(0))
        ColorSpec.rgbResult    = 255   'set the default color to Red (255,0,0)...
     
        'try these options one by one, for different effects...
        'ColorSpec.Flags = ColorSpec.Flags OR %CC_RGBINIT  'tells control to start at default color
        'ColorSpec.Flags = ColorSpec.Flags OR %CC_FULLOPEN
        'ColorSpec.Flags = ColorSpec.Flags OR %CC_PREVENTFULLOPEN
      
        'create a nice selection of colors for the custom colors (OPTIONAL)...
        RANDOMIZE TIMER
        FOR lCounter = 0 TO 15
            lCustomColor(lCounter) = RND(0,16777215)  'or RGB(lCounter*16,0,(15-lCounter)*16)
        NEXT
        '(You could also load custom colors from a file, or hard-code them.)
     
        lResult = ChooseColor(ColorSpec)
     
        IF lResult = 0 THEN
            MSGBOX "You chose the Cancel button.",,"Color Selection Demo"
        ELSE
            MSGBOX "You chose color value &h"+HEX$(ColorSpec.rgbResult,6),,"Color Selection Demo"
        END IF
     
        'You can also check then values in the lCustomColor() array to see if they've been changed.
     
        FUNCTION = 1
     
    END FUNCTION
    -------------
    Perfect Sync: Perfect Sync Development Tools
    Email: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited March 01, 2000).]
    "Not my circus, not my monkeys."

  • #2
    Eric,
    Great routine. I'm trying to use it with PB/CC which has, as you are aware, only 16 colours.

    Any tips on restricing the RGB function to the basic 16

    TIA

    ------------------
    Nigel Thomas

    Comment


    • #3
      Hmmm... Not off the top of my head. The ChooseColor dialog is designed to allow the selection of any valid Windows color, so you probably couldn't restrict the display to 16 colors. So several of the standard color choices won't make sense in the context of a console app.

      Or are you asking how you could write a "best match" function that could convert a TrueColor RGB value into the closest valid console color? I've never tried it, but it doesn't sound like it would be too difficult. Has anybody out there written one?

      One interesting side note for PB/CC programmers... If your program will be run only on NT or 2000, it is possible to specify "custom colors" for each of the 16 standard COLOR values by editing the registry. But the settings will only take effect the next time your program is run, since Windows only reads the registry when it creates the console. But it can be a useful technique for giving your PB/CC apps a custom look.

      -- Eric


      ------------------
      Perfect Sync: Perfect Sync Development Tools
      Email: mailto:[email protected][email protected]</A>

      "Not my circus, not my monkeys."

      Comment


      • #4
        In vb16 bit;

        CC_Flags = CC_RGBINIT Or CC_PREVENTFULLOPEN



        ------------------

        Comment


        • #5
          This function finds "nearest" index for PB/CC COLOR
          (initially was done in VB to find "clean" colors and was based on QBColor statement)

          Code:
          Function QbColorId (RGBComp As Dword) As Long
             Local RGBTest As Dword, ss As Quad, s As Quad
             Local i As Long, j As Long, ii As Long
             Local b1 As Byte Ptr, b2 As Byte Ptr
             For i = 0 To 15
                Select Case i
                   Case  0: RgbTest = &H000000
                   Case  1: RgbTest = &H800000
                   Case  2: RgbTest = &H008000
                   Case  3: RgbTest = &H808000
                   Case  4: RgbTest = &H000080
                   Case  5: RgbTest = &H800080
                   Case  6: RgbTest = &H008080
                   Case  7: RgbTest = &HC0C0C0
                   Case  8: RgbTest = &H808080
                   Case  9: RgbTest = &HFF0000
                   Case 10: RgbTest = &H00FF00
                   Case 11: RgbTest = &HFFFF00
                   Case 12: RgbTest = &H0000FF
                   Case 13: RgbTest = &HFF00FF
                   Case 14: RgbTest = &H00FFFF
                   Case 15: RgbTest = &HFFFFFF
                End Select
                b1 = VarPtr(RGBTest): b2 = VarPtr(RGBComp): s = 0
                For j = 1 To 3
                   s = s + (@b1 - @b2) * (@b1 - @b2)
                   Incr b1: Incr b2
                Next
                If (i = 0) Or (ss > s) Then ss = s: ii = i
             Next
             Function = ii
          End Function
          Usage for Eric's sample:
          MsgBox Str$(QbColorId(ColorSpec.rgbResult)),,"PbCC Color"

          Interesting, how to find RGB values for 256-colors pallette ?

          ------------------

          Comment


          • #6
            Interesting, how to find RGB values for 256-colors palette ?
            The default palette does not fill all 256 entries. Usually only about 20 are defined (I seem to recall the exact behavior is driver dependant), and the remaining entries are set by application(s) that alter the system palette.


            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected].nz

            Comment


            • #7
              Originally posted by Eric Pearson View Post
              This small PB/DLL program demonstrates the use of the ChooseColor common dialog.
              I just used this as the basis for selecting colors in the halftone color converter
              example program.
              Nice. Thanks!
              The world is strange and wonderful.*
              I reserve the right to be horrifically wrong.
              Please maintain a safe following distance.
              *wonderful sold separately.

              Comment

              Working...
              X