2009-04-17 12 views
3

WinFormsでColorDialogコンポーネントをスピンアップして、ユーザーが特定のカスタムコントロールのチャートの背景色と前景色を選択できるようにします。両方の設定オプションは設定ダイアログの同じページにありますので、ダイアログをチャートの背景を変更するときに「背景色」に、色を変更するために「グリッドカラー」にカラーダイアログのタイトルを設定したいグリッドのこれにより、背景やグリッドの色を変更したかどうかわからない場合に、チャートのタイトルを見ることができる便利なUXが提供されます。ColorDialogのタイトルを変更するにはどうすればよいですか?

残念ながら、ドキュメントにはColorDialogのタイトルを操作する方法はありません。この変更は可能ですか?もしそうなら、どうですか?

答えて

6

残念ながら、一般的なカラーピッカーダイアログのタイトルを変更することはできません。可能な解決策は、適切なタイトルを割り当てることができる専用フォームでホストするカラーピッカーコントロールを見つけるか作成することです。または、adopt the Office style of color pickingをコンボボックスの形式で指定することもできます。ロブの答えに触発さ

EDIT

は、私は、次の解決策を見つけました。それはHookProc方法からHWNDをひったくり、ColorDialogから継承し、P /呼び出してSetWindowTextを呼び出す含まれます

ColorDialogはそのhWndは(私がチェックするために、このマシンにVisual Studioを持っていない)、あなたを公開すると仮定すると、
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

私は同じことをほぼ並行して発見しました。私はそれがあなたのtitleSet変数でこれを行うか、WM_INITDIALOG(0x0110)でしか設定しない方が良いかどうかはわかりません。 –

+1

私はウィンドウメッセージを考慮せずに、実際にそのコードをマッシュアップしていました。 WM_INITDIALOGに設定すると、あなたが私に尋ねるとさらに良い解決策になります。 –

2

私はこれをどのように行ったのかを参考にして転記しています。私は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'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); 
    } 
} 
関連する問題