Reputation: 44096
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?
Upvotes: 5
Views: 4292
Reputation: 44096
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);
}
}
Upvotes: 3
Reputation: 6002
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);
}
}
Upvotes: 7
Reputation: 45799
Assuming the ColorDialog exposes its hWnd (I don't have Visual Studio on this machine to check), you could use the Win32 SetWindowText API. The PInvoke signature can be found at PInvoke.net.
Upvotes: 1