How do I change the title of ColorDialog?

后端 未结 4 721
北荒
北荒 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);
        }
    }
    

提交回复
热议问题