.netwinformscolordialog

How do I change the title of ColorDialog?


I'm spinning up a ColorDialog component in WinForms to let the user select a particular custom control's chart's background color and foreground color. Both configuration options are on the same page of the configuration dialog, so I want to set the title of the color dialog to "Background Color" when the dialog is brought up to change the chart's background, and "Grid Color" to change the color of the grid. This will provide a useful UX where the user will be able to look at the title of the chart if they're not sure whether they chose to change the background or grid color.

Unfortunately, the docs don't seem to mention any way to manipulate the ColorDialog's title. Is it possible to make this change? If so, how?


Solution

  • Unfortunately, changing the title of the common color picker dialog is not possible. A possible solution is to find or create a color picker control to host in a dedicated form which, of course, you could assign the appropriate title. Or you could adopt the Office style of color picking in the form of a combo box.

    EDIT

    Inspired by Rob's answer, I found the following solution. It involves inheriting from ColorDialog, snatching the HWND from the HookProc method and calling SetWindowText through P/Invoke:

    public class MyColorDialog : ColorDialog
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowText(IntPtr hWnd, string lpString);
    
        private string title = string.Empty;
        private bool titleSet = false;
    
        public string Title
        {
            get { return title; }
            set
            {
                if (value != null && value != title)
                {
                    title = value;
                    titleSet = false;
                }
            }
        }
    
        protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
        {
            if (!titleSet)
            {
                SetWindowText(hWnd, title);
                titleSet = true;
            }
    
            return base.HookProc(hWnd, msg, wparam, lparam);
        }
    }