How do I change the title of ColorDialog?

后端 未结 4 719
北荒
北荒 2021-01-19 14:21

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 configuratio

相关标签:
4条回答
  • 2021-01-19 14:41

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-19 14:45

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-19 14:49

    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.

    0 讨论(0)
  • 2021-01-19 14:59

    I'm posting this in case others come across this in years to come.

    Using the SetWindowText() function in the OnShow() event works fine. I'm not sure which version of Delphi introduced TColorDialog.OnShow but it goes at least back to XE2.

    procedure TForm1.ColorDialog1Show(Sender: TObject);
      begin
      SetWindowText(ColorDialog1.Handle, MyTitle);
      end;
    

    For my purposes I use a global variable for MyTitle and assign the appropriate text before calling ColorDialog1.Execute.

    0 讨论(0)
提交回复
热议问题