代码之家  ›  专栏  ›  技术社区  ›  Greg D

如何更改ColorDialog的标题?

  •  5
  • Greg D  · 技术社区  · 17 年前

    我在旋转 ColorDialog WinForms中的组件,允许用户选择特定自定义控件的图表背景色和前景色。这两个配置选项都在配置对话框的同一页面上,因此当打开对话框以更改图表的背景时,我想将颜色对话框的标题设置为“背景颜色”,将网格颜色设置为“网格颜色”以更改网格的颜色。这将提供一个有用的用户体验,如果用户不确定是选择更改背景还是网格颜色,他们可以查看图表的标题。

    不幸的是,文档似乎没有提到任何操纵 对话框 的标题。有可能做出这样的改变吗?如果是这样,怎么办?

    3 回复  |  直到 17 年前
        1
  •  7
  •   Tormod Fjeldskår    17 年前

    遗憾的是,无法更改通用颜色选择器对话框的标题。一种可能的解决方案是找到或创建一个颜色选择器控件,以专用形式承载,当然,您可以分配适当的标题。或者你可以 adopt the Office style of color picking

    编辑

    受到Rob回答的启发,我找到了以下解决方案。它涉及继承 ColorDialog ,把HWND从 HookProc 方法和调用 SetWindowText 通过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);
        }
    }
    
        2
  •  3
  •   Greg D    17 年前

    /// <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);
        }
    }
    
        3
  •  1
  •   Rob    17 年前
        4
  •  0
  •   mait    7 年前
    推荐文章