2009-06-17 128 views
16

如何將此按鈕添加到WPF中的標題欄中,通過它在很多應用程序中如此使用,我認爲它將內置於其中或其他東西,但看起來不是這樣。無論如何,讓我知道你是否知道這件事。幫助「?」按鈕

謝謝。

編輯:

是不是有什麼事情等同於this

基本上,要有?圖標在勝利形式,所有你需要做的是這樣的:

public Form1() 
{ 
    InitializeComponent(); 

    this.HelpButton = true; 
    this.MaximizeBox = false; 
    this.MinimizeBox = false; 
} 

不是WPF有這樣的事情嗎?

+0

爲什麼標題欄?我看到的大多數應用都將它放在菜單欄中。 – 2009-06-17 23:48:39

+1

事情是我們將主要在對話框窗口上使用它,它通常沒有菜單欄。點擊它將爲該窗口提供上下文幫助。就像在MS Word 2007中的字體對話框窗口中一樣。 – Carlo 2009-06-18 00:17:36

回答

30

這很簡單,只需將此代碼插入到Window類中即可。

此代碼使用interop刪除WS_MINIMIZEBOX和WS_MAXIMIZEBOX樣式並添加WS_EX_CONTEXTHELP擴展樣式(只有在刪除最小化和最大化按鈕時纔會顯示問號)。

編輯:在幫助按鈕上添加了單擊檢測,這是通過使用HwndSource.AddHook掛接到WndProc並使用wParam的SC_CONTEXTHELP偵聽WM_SYSCOMMAND消息來完成的。

當檢測到單擊時,此代碼將顯示一個消息框,將其更改爲事件,路由事件或命令(對於MVVM應用程序)作爲練習留給讀者。

private const uint WS_EX_CONTEXTHELP = 0x00000400; 
private const uint WS_MINIMIZEBOX = 0x00020000; 
private const uint WS_MAXIMIZEBOX = 0x00010000; 
private const int GWL_STYLE = -16; 
private const int GWL_EXSTYLE = -20; 
private const int SWP_NOSIZE = 0x0001; 
private const int SWP_NOMOVE = 0x0002; 
private const int SWP_NOZORDER = 0x0004; 
private const int SWP_FRAMECHANGED = 0x0020; 
private const int WM_SYSCOMMAND = 0x0112; 
private const int SC_CONTEXTHELP = 0xF180; 


[DllImport("user32.dll")] 
private static extern uint GetWindowLong(IntPtr hwnd, int index); 

[DllImport("user32.dll")] 
private static extern int SetWindowLong(IntPtr hwnd, int index, uint newStyle); 

[DllImport("user32.dll")] 
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags); 


protected override void OnSourceInitialized(EventArgs e) 
{ 
    base.OnSourceInitialized(e); 
    IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle; 
    uint styles = GetWindowLong(hwnd, GWL_STYLE); 
    styles &= 0xFFFFFFFF^(WS_MINIMIZEBOX | WS_MAXIMIZEBOX); 
    SetWindowLong(hwnd, GWL_STYLE, styles); 
    styles = GetWindowLong(hwnd, GWL_EXSTYLE); 
    styles |= WS_EX_CONTEXTHELP; 
    SetWindowLong(hwnd, GWL_EXSTYLE, styles); 
    SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); 
    ((HwndSource)PresentationSource.FromVisual(this)).AddHook(HelpHook); 
} 

private IntPtr HelpHook(IntPtr hwnd, 
     int msg, 
     IntPtr wParam, 
     IntPtr lParam, 
     ref bool handled) 
{ 
    if (msg == WM_SYSCOMMAND && 
      ((int)wParam & 0xFFF0) == SC_CONTEXTHELP) 
    { 
     MessageBox.Show("help"); 
     handled = true; 
    } 
    return IntPtr.Zero; 
} 
0

WPF沒有幫助按鈕。但是不應該推動自己推出自己的產品。

+0

到標題欄?好像你需要改變Window chrome,這是記錄的,但不是微不足道的。 – micahtan 2009-06-17 23:27:38

0

如果您打算將按鈕添加到非客戶端區域,請參閱this article