How to change the name of ColorDialog?

I deploy the component ColorDialogin WinForms so that the user can select a specific background color and foreground color of the control chart. Both configuration parameters are on the same page of the configuration dialog, so I want to set the title of the background color dialog box when the dialog is raised so that change the background of the chart, and Grid Color to change the grid color. This will provide a useful UX where the user can see the title of the chart if they are not sure whether they have chosen a background color or a grid.

Unfortunately, there is no mention in the docs of any way to control the title ColorDialog. Can this be changed? If so, how?

+3
source share
3 answers

Unfortunately, changing the name of the general color selection dialog is not possible. A possible solution is to find or create a color selection control for placement in a special form, which, of course, you could assign an appropriate title to. Or you can accept the Office style color picker as a combo box.

EDIT

Inspired by Rob, I found the following solution. It includes inheritance from ColorDialog , snatching HWND from HookProc and calling SetWindowText via 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);
    }
}
+6
source

, . WM_INITDIALOG ( Tormod)

/// <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 shown.
    /// </summary>
    [Browsable(true)]
    [Category("Appearance")]
    [Description("The title that will be displayed on the dialog when it shown.")]
    public string Title
    {
        get;
        set;
    }

    /// <summary>
    /// The hook into the dialog WndProc that we can leverage to set the
    /// window 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);
    }
}
+2

, ColorDialog hWnd ( Visual Studio ), Win32 SetWindowText API. PInvoke PInvoke.net.

+1

Source: https://habr.com/ru/post/1706617/


All Articles