The MASM Forum

General => The Campus => Topic started by: cman on March 25, 2014, 07:54:25 AM

Title: ChooseColor Dialog
Post by: cman on March 25, 2014, 07:54:25 AM
When using a ChooseColor generated dialog , the Windows Api takes as a parameter a "CHOOSECOLOR" struct:


typedef struct ;from win32.hlp
{   // cc 
    DWORD        lStructSize;
    HWND         hwndOwner;
    HWND         hInstance;
    COLORREF     rgbResult;
    COLORREF*    lpCustColors;
    DWORD        Flags;
    LPARAM       lCustData;
    LPCCHOOKPROC lpfnHook;
    LPCTSTR      lpTemplateName;
} CHOOSECOLOR;



The color that was chosen by the user ( as I understand ) is stored in the struct field "rgbResult". Is this value ( rgbResult ) the chosen color in the format : "RRRRRRRRGGGGGGGGGBBBBBBBB"  , where R is a bit of the red component of the color and so on? Thanks for any information...
Title: Re: ChooseColor Dialog
Post by: x64Core on March 25, 2014, 09:58:32 AM
Correct, it's a RGB color format, You can take a look at the MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646830(v=vs.85).aspx

Title: Re: ChooseColor Dialog
Post by: MichaelW on March 25, 2014, 11:35:36 AM
Your format string shows 9 green bits, instead of 8.

COLORREF (http://msdn.microsoft.com/en-us/library/windows/desktop/dd183449(v=vs.85).aspx)

And in case you're curious about the RGB and Get?Value macros, from WinGDI.h:

#define RGB(r,g,b)          ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
#define GetRValue(rgb)      (LOBYTE(rgb))
#define GetGValue(rgb)      (LOBYTE(((WORD)(rgb)) >> 8))
#define GetBValue(rgb)      (LOBYTE((rgb)>>16))

Title: Re: ChooseColor Dialog
Post by: cman on March 26, 2014, 01:58:43 AM
Ok , thanks for the information!

Quote
Your format string shows 9 green bits, instead of 8.

I think my vision is going down hill a little bit ( my laptop screen is somewhat small ! ).

Is there a way to center the "Color Chooser" dialog on the screen? When I call the api the dialog jumps to the upper left-hand corner of my screen ( this is annoying ).
Title: Re: ChooseColor Dialog
Post by: jj2007 on March 26, 2014, 05:01:58 AM
Quote from: cman on March 26, 2014, 01:58:43 AM
Is there a way to center the "Color Chooser" dialog on the screen?
Yes, you can specify a callback:

QuotelpfnHook: Pointer to a CCHookProc hook procedure that can process messages intended for the dialog box
Title: Re: ChooseColor Dialog
Post by: MichaelW on March 26, 2014, 11:00:44 AM
I didn't have time to get my hook code working, but if you set the CC dialog owner window, at least under Windows XP SP3 the CC dialog will be positioned just below the owner window title bar, shifted slightly to the right, a good choice IMO.

;===============================================================================
include \masm32\include\masm32rt.inc
;===============================================================================
.data
    hInst dd ?
    cust  COLORREF 16 dup(0)
    cc    CHOOSECOLOR <>
.code
;===============================================================================
DialogProc proc  hwndDlg:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    SWITCH uMsg
        CASE WM_COMMAND

            .IF wParam == 1000

                invoke RtlZeroMemory, ADDR cc, SIZEOF cc
                mov cc.lStructSize, SIZEOF CHOOSECOLOR
                push hwndDlg
                pop cc.hwndOwner

                ;-------------------------------------------
                ; Leaving this off will cause the CC dialog
                ; to trigger an access violation exception.
                ;-------------------------------------------

                mov cc.lpCustColors, OFFSET cust

                invoke ChooseColor, ADDR cc

            .ENDIF

        CASE WM_CLOSE

            invoke EndDialog, hwndDlg, 0

    ENDSW
    return 0
DialogProc endp
;===============================================================================
start:
;===============================================================================
    mov hInst, rv(GetModuleHandle,NULL)
    Dialog "Test","MS Sans Serif",8,WS_OVERLAPPED or WS_SYSMENU or DS_CENTER, \
            1,0,0,100,75,1024
    DlgButton "Choose Color",0,21,20,50,12,1000
    CallModalDialog hInst, 0, DialogProc, 0
    exit
end start