救命 "?" 按键

Car*_*rlo 18 wpf

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

谢谢.

编辑:

是不是有什么等同于这个

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

public Form1()
{
    InitializeComponent();

    this.HelpButton = true;
    this.MaximizeBox = false;
    this.MinimizeBox = false;
}
Run Code Online (Sandbox Code Playgroud)

WPF有没有这样的东西?

Nir*_*Nir 34

这很简单,只需将此代码插入到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;
}
Run Code Online (Sandbox Code Playgroud)

  • 极好的!如果可以的话,我会给你不止一个......但是,我真的不能说我同意它和你说的一样“简单”;-)谢谢! (2认同)