Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Customize Color DialogBox

0.00/5 (No votes)
8 Jun 2007 1  
Customize Color dialog Box by overriding Window Procedure method

Introduction

Color dialog box is one of the pre-defined dialog in C#.NET which we can use to select and change the color of a control at runtime. It allows us to do some of the alterations with the help of the limited number of properties and event that are available for the dialog box.

As for any other window form it doesn,t have a Paint event or a property like BackColor to alter according to our requirement.In many of out application we need to change the appearence of the dialog box, which is not directly possible. In this code i have override the WinProc() method thus tracing the window messages and incorporating my own way to paint the dialog box. Using the code anyone can change the appearence according to his/her requirement.

Screenshot - color.jpg

Background

With the basic knowledge of programming, one should have a good knowledge of Window Procedure, window messages and Message handling. We can incorporate our own method for painting the color dialog box by catching the WM_Paint message, i have used a gradient brush to paint the background of the dialog box here.


Using the code

You can use the code given in the zip file to change the appearance of the color dialog box. One can change the appearance of any other dialog box by following the same steps.

One can use the WinSpy++ which is being shiped with Visual studio to get the Window handle and control Id of different controls that are present on the dialog box.

Below is the list of messages that can be used for altering the appearence of a dialog box:

private const int WM_DESTROY = 0x0002;
private const int WM_PAINT = 0x000F;
private const int WM_INITDIALOG = 0x0110;
private const int WM_COMMAND = 0x0111;
private const int BN_CLICKED = 0;
private const int BN_PAINT = 0;
private const int WM_CTLCOLORBTN = 0x0135;
private const int WM_CTLCOLORSTATIC = 0x0138;
private const int WS_CHILD = 0x40000000;
private const int WM_CTLCOLORDLG = 0x0136;
private const int MSG_MOVETOTOPMOST = 0x00F2;
private const int WM_CTLCOLOREDIT = 0x0133;
private const int WM_DRAWITEM = 0x002B;
private const UInt32 WM_NCPAINT = 0x0085;
private const int WM_ERASEBKGND = 0x0014;
private const int WM_WINDOWPOSCHANGED = 0x0047;

List of Control IDs for different controls present on a dialog box:

private const int ID_OK = 1;
private const int ID_CANCEL = 2;
private const int DLG_COLOR = 10;
private const int NUM_BASIC_COLORS = 48;
private const int COLOR_SOLID_LEFT = 730;
private const int COLOR_SOLID_RIGHT = 731;
private const int COLOR_HUE = 703; // Edit
private const int COLOR_SAT = 704; // Edit
private const int COLOR_LUM = 705; // Edit
private const int COLOR_RED = 706; // Edit
private const int COLOR_GREEN = 707; // Edit
private const int COLOR_BLUE = 708; // Edit
private const int COLOR_CURRENT = 709;
private const int COLOR_RAINBOW = 710; //Color shade wheel
private const int COLOR_ELEMENT = 716;
private const int COLOR_ADD = 712; // Button: "&Add Custom Colors"
private const int COLOR_MIX = 719; // Button: "&Define Colors >>"
private const int COLOR_BOX1 = 720; // Static: basic colors
private const int COLOR_CUSTOM1 = 721; // Static: custom colors
private const int COLOR_HUEACCEL = 723; // Label Edit
private const int COLOR_SATACCEL = 724; // Label Edit
private const int COLOR_LUMACCEL = 725; // Label Edit
private const int COLOR_REDACCEL = 726; // Label Edit
private const int COLOR_GREENACCEL = 727; // Label Edit
private const int COLOR_BLUEACCEL = 728; // Label Edit

Controls are classified as "Static", "Edit","Buttons" etc. there is a Color shade wheel with control ID as Color_RAINBOW,this is the wheel where we can input the RGB values or the Hue, saturation and Luminosity to alter the color.

With the Window procedure we can also change the size of the color dialog box and can actually add some of our required control to show up on the dialog box.All this can be done very easily by tracking the Window messages for the required alteration.

Apart from the window messages and Control Ids, one should also import the required functions from the User32.dll to actually give the window handle and to work on the dialog box. Some example are a s follows:

private static extern bool GetWindowRect(int hWnd, ref Rectangle lpRect);

[DllImport("user32.dll")]
private static extern int GetParent(int hWnd);

[DllImport("user32.dll")]
private static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

[DllImport("user32.dll")]
static extern uint GetDlgItemInt(IntPtr hDlg, int nIDDlgItem, IntPtr lpTranslated, bool bSigned);

[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
private static extern int GetDlgCtrlID(IntPtr hWnd);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool repaint);

[DllImport("user32.dll")]
private static extern bool UpdateWindow(IntPtr hWnd);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int InvalidateRect(IntPtr hWnd, IntPtr rc, int bErase);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SetWindowPos(IntPtr hWnd,IntPtr hWndInsertAfter, int x, int y, int Width, int Height, SetWindowPosFlags flags);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool LockWindowUpdate(IntPtr hWndLock);

[DllImport("gdi32.dll")]
static extern uint SetBkColor(IntPtr hdc, int crColor);

[DllImport("gdi32.dll")]
static extern int SetBkMode(IntPtr hdc, int iBkMode);

//[DllImport("user32.dll", CharSet = CharSet.Auto)]
//static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);

[DllImport("gdi32.dll")]
static extern IntPtr GetStockObject(int fnObject);

//rotected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)

        {
            switch (msg)
            {
                case WM_ERASEBKGND:
                    Graphics gfr = Graphics.FromHwnd(hWnd);
                    LinearGradientBrush lnrfr = new LinearGradientBrush(new Point(0, 0), new Point(400, 400), Color.CornflowerBlue, Color.White);
                    gfr.FillRegion(lnrfr, gfr.Clip);
                    break;
            
              
            }

            return base.HookProc(hWnd, msg, wparam, lparam);
        
        }
    }
}

//


Points of Interest

There is a lot of information that is provide don MSDN for alteration of the dialog box but none of them actually helped this time, though MSDN is very helpful in other cases.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here