1

我在一个应用程序中有一个窗口,并且Resize模式设置为NoResize,但是如果单击窗口的左上角,“最大化”、“最小化”和“大小”的菜单项仍然启用。这只发生在没有图标但仍有关闭按钮的窗口上。这意味着这个问题不会发生在我的有图标的窗口上。

这是删除图标但仍保留红色关闭按钮的代码:

protected override void OnSourceInitialized(EventArgs e) {
    IconHelper.RemoveIcon(this);
}

public static class IconHelper {
    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hwnd, int index);

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

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

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);

    const int GWL_EXSTYLE = -20;
    const int WS_EX_DLGMODALFRAME = 0x0001;
    const int SWP_NOSIZE = 0x0001;
    const int SWP_NOMOVE = 0x0002;
    const int SWP_NOZORDER = 0x0004;
    const int SWP_FRAMECHANGED = 0x0020;
    const uint WM_SETICON = 0x0080;

    public static void RemoveIcon(Window window) {
        IntPtr hwnd = new WindowInteropHelper(window).Handle;
        int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
        SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
        SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
    }
}

在此处输入图像描述

我想知道为什么会发生这种情况,如果有人能解决这个问题,我将不胜感激。我正在使用 WPF 和 C#。

4

0 回答 0