.386
.model flat, stdcall
option casemap :none

    include \masm32\include\windows.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\COMDLG32.inc            ;Contains the color common dialog
    includelib \masm32\lib\COMDLG32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    include \masm32\include\gdi32.inc               ;createsolidbrush function
    includelib \masm32\lib\gdi32.lib

    WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

.data
    hInstance   dd 0
    dlgname     db "MAIN",0
    szColor     db "Color Value: %li (dec)",0       ;Show our color value
.data?
    buffer      db 24 dup(?)                        ;buffer for wsprintf
    rgb         dd 16 dup(?)                        ;value for cc struct
    color       dd ?                                ;color holder
    hBrush      dd ?                                ;hold our brush so we can delete it
    hStatic     dd ?                                ;holder for the static we want to paint

.code

start:

    invoke GetModuleHandle, NULL
        mov hInstance, eax
    invoke DialogBoxParam,hInstance,ADDR dlgname,0,ADDR WndProc,0
        invoke ExitProcess,eax

WndProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD

LOCAL cc:CHOOSECOLOR

.if uMsg == WM_INITDIALOG   
    invoke GetDlgItem,hWin,1000                     ;get the big static box
        mov hStatic,eax                             ;save it
            xor eax, eax
                ret
.elseif uMsg == WM_CTLCOLORSTATIC
    mov eax,lParam
        .if eax == hStatic                          ;paint our static color box
            invoke CreateSolidBrush,color           ;create a solidbrush from the color
                mov hBrush,eax                      ;save the handle
                    ret                             ;return with that brush to paint the bk
        .endif
.elseif uMsg == WM_COMMAND
    .if wParam == 1001                              ;the command button
        mov cc.lStructSize,sizeof cc                ;size of the struct
        push hWin                                   ;our window is the owner of the color dialog
        pop cc.hwndOwner
        mov cc.rgbResult,255                        ;start the box of in red
        mov cc.Flags,CC_FULLOPEN+CC_RGBINIT         ;dialog is open all the way and starts with our predefined start
        mov cc.lpCustColors,offset rgb              ;holder for the rgb declared in the color dialog box
            invoke ChooseColor, addr cc             ;we only filled a little of the stuct but it is ebough to where we can call it
                .if eax == TRUE                     ;return true means they chose a color and did not hit cancel or alt+f4
                    push cc.rgbResult               ;get our color and save it
                    pop color
                        invoke InvalidateRect,hStatic,NULL,TRUE             ;Redraw the static
                        invoke wsprintf,addr buffer,addr szColor,color      ;place the color value into a string
                        invoke SetDlgItemText,hWin,2000,addr buffer         ;show the string in the second static control
                .endif
    .endif
        xor eax,eax
            ret
.elseif uMsg == WM_CLOSE
    invoke DeleteObject,hBrush                      ;delete our custom brush
        invoke EndDialog,hWin,NULL
            ret
.endif
xor eax, eax
    ret
WndProc endp
end start

