Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 243
Greg D Avatar asked Apr 17 '09 20:04

Greg D


People also ask

What does ColorDialog represents?

The ColorDialog control class represents a common dialog box that displays available colors along with controls that enable the user to define custom colors. It lets the user select a color. The main property of the ColorDialog control is Color, which returns a Color object.

How to use ColorDialog in VB net?

Let's create a Color Dialog in the VB.NET Windows form by using the following steps. Step 1: Drag the Color Dialog from the toolbox and drop it to the Windows form, as shown below. Step 2: Once the Color Dialog is added to the form, we can set various properties of the Color by clicking on the Color Dialog.

Which option is correct for selecting color option from ColorDialog box control in window form?

To choose a color using the ColorDialog component Display the dialog box using the ShowDialog method. Use the DialogResult property to determine how the dialog box was closed. Use the Color property of the ColorDialog component to set the chosen color.

How do I use color dialog windows forms?

To create a ColorDialog control at design-time, you simply drag and drop a ColorDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a ColorDialog on a Form, the ColorDialog looks like Figure 2. Adding a ColorDialog to a Form adds following two lines of code.


2 Answers

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);
    }
}
like image 133
Tormod Fjeldskår Avatar answered Oct 27 '22 23:10

Tormod Fjeldskår


I'm posting this as a reference for how I did it. I used WM_INITDIALOG (as referenced in my comment on Tormod's answer)

/// <summary>
/// The standard ColorDialog dialog box with a title property.
/// </summary>
public class ColorDialogWithTitle : ColorDialog
{
    private const int InitDialogMessage = 0x0110; // WM_INITDIALOG

    /// <summary>
    /// Initializes a new instance of the ColorDialogWithTitle class.
    /// </summary>
    public ColorDialogWithTitle() :
        base()
    {
        this.Title = Resources.ColorDialogWithTitle_DefaultTitle;

        return;
    }

    /// <summary>
    /// Gets or sets the title that will be displayed on the dialog when it's shown.
    /// </summary>
    [Browsable(true)]
    [Category("Appearance")]
    [Description("The title that will be displayed on the dialog when it's shown.")]
    public string Title
    {
        get;
        set;
    }

    /// <summary>
    /// The hook into the dialog's WndProc that we can leverage to set the
    /// window's text.
    /// </summary>
    /// <param name="hWnd">The handle to the dialog box window.</param>
    /// <param name="msg">The message being received.</param>
    /// <param name="wparam">Additional information about the message.</param>
    /// <param name="lparam">More additional information about the message.</param>
    /// <returns>
    /// A zero value if the default dialog box procedure processes the
    /// message, a non-zero value if the default dialog box procedure 
    /// ignores the message.
    /// </returns>
    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
    {
        if (msg == InitDialogMessage)
        {
            // We'll ignore failure cases for now.  The default text isn't 
            // so bad and this isn't library code.
            SafeNativeMethods.SetWindowText(hWnd, this.Title);
        }

        return base.HookProc(hWnd, msg, wparam, lparam);
    }
}
like image 43
Greg D Avatar answered Oct 28 '22 00:10

Greg D