救命 ”?”按键

时间:2009-06-17 23:12:36

标签: wpf

如何将这个按钮添加到WPF中的标题栏,因为它在许多应用程序中使用,我认为它将被内置或者其他东西,但看起来并非如此。无论如何,如果您对此有任何了解,请告诉我。

感谢。

编辑:

是否有任何等同于this的内容?

基本上,有?赢取表格中的图标,您需要做的就是:

public Form1()
{
    InitializeComponent();

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

WPF有没有这样的东西?

3 个答案:

答案 0 :(得分:33)

这很简单,只需将此代码插入到Window类中即可。

此代码使用interop删除WS_MINIMIZEBOX和WS_MAXIMIZEBOX样式并添加WS_EX_CONTEXTHELP扩展样式(只有删除最小化和最大化按钮时才会显示问号)。

编辑:在帮助按钮上添加了点击检测,这是通过使用HwndSource.AddHook挂钩到WndProc并使用SC_CONTEXTHELP的wParam监听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;
}

答案 1 :(得分:0)

WPF没有开箱即用的帮助按钮。但是,不应该推动自己的推动。

答案 2 :(得分:0)

如果您打算将按钮添加到非客户区域,请查看this article