开发者

Can I prevent maximising of a window in WPF?

Specifically, not just by removing the buttons but completely disable maximising. This would mean that double-clicking the title bar or dragging the title bar to the top of the screen in Windows 7 would not work开发者_StackOverflow. I still want the window to be sizable though.


Have a look at the CanMinimize field .


Removing Maximize from the system menu should be sufficient. I don't know if that will work for the Win7 "docking", let me know if it does.

Here's an article with a helper class for modifying the system menu of a window: http://www.codeguru.com/csharp/csharp/cs_misc/userinterface/article.php/c9327/

It assumes WinForms, but only because you need a window handle. In WPF this can be obtained with a WindowInteropHelper.

Updated

This code is UI Framework agnostic

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

[Flags]
public enum SystemMenuFlags
{
    Unchecked = 0x0000,
    String = 0x0000,
    Disabled = 0x0002,
    Grayed = 0x0001,
    Checked = 0x0008,
    Popup = 0x0010,
    BarBreak = 0x0020,
    Break = 0x0040,
    ByPosition = 0x0400,
    ByCommand = 0x0000,
    Separator = 0x0800,
}

public enum SystemMenuCommand
{
    Size = 0xF000,
    Move = 0xF010,
    Minimize = 0xF020,
    Maximize = 0xF030,
    Close = 0xF060,
    Restore = 0xF120,
}

public class SystemMenu
{
    private IntPtr _menuHandle = IntPtr.Zero;
    public IntPtr MenuHandle { get { return _menuHandle; } }
    private readonly IntPtr _windowHandle;
    public IntPtr WindowHandle { get { return _windowHandle; } }


    public SystemMenu(IntPtr windowHandle)
    {
        if (windowHandle == IntPtr.Zero)
            throw new InvalidOperationException("The handle must point to a real window. Create only after your window object has created a real window.");
        _windowHandle = windowHandle;
        IntPtr menuHandle = GetSystemMenu(windowHandle, 0);
        if (menuHandle == IntPtr.Zero)
            throw new InvalidOperationException("The specified window does not have a system menu.");
        _menuHandle = menuHandle;
    }

    private SystemMenu(IntPtr windowHandle, IntPtr menuHandle)
    {
        _windowHandle = windowHandle;
        _menuHandle = menuHandle;
    }

    public static SystemMenu FromHandle(IntPtr windowHandle)
    {
        if (windowHandle == IntPtr.Zero)
            throw new InvalidOperationException("The handle must point to a real window. Call FromHandle only after your window object has created a real window.");
        IntPtr menuHandle = GetSystemMenu(windowHandle, 0);
        if (menuHandle == IntPtr.Zero)
            return null;
        return new SystemMenu(windowHandle, menuHandle);
    }

    public int Count
    {
        get
        {
            int count = GetMenuItemCount(MenuHandle);
            if (count < 0)
                throw new Win32Exception(Marshal.GetLastWin32Error());
            return count;
        }
    }

    private int GetItemPosition(SystemMenuCommand command)
    {
        int count = Count;
        for (int position = 0; position < count; position++)
        {
            int id = GetMenuItemID(MenuHandle, position);
            if ((SystemMenuCommand)id == command)
                return position;
        }

        return -1;
    }

    public void InsertItem(int position, int id, string text)
    {
        if (String.IsNullOrEmpty(text))
            throw new ArgumentNullException("text");
        if (!IsValidMenuIdValue(id))
            throw new ArgumentOutOfRangeException("id", "Valid range for menu id is [0 .. 0xF000]");
        if (InsertMenu(MenuHandle, position, (Int32)(SystemMenuFlags.ByPosition | SystemMenuFlags.String), id, text) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void InsertSeparator(int position)
    {
        if (InsertMenu(MenuHandle, position, (Int32)(SystemMenuFlags.ByPosition | SystemMenuFlags.Separator), 0, String.Empty) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void InsertItemBefore(SystemMenuCommand command, int id, string text)
    {
        if (String.IsNullOrEmpty(text))
            throw new ArgumentNullException("text");
        if (!IsValidMenuIdValue(id))
            throw new ArgumentOutOfRangeException("id", "Valid range for menu id is [0 .. 0xF000]");
        if (InsertMenu(MenuHandle, (int)command, (Int32)(SystemMenuFlags.ByCommand | SystemMenuFlags.String), id, text) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void InsertSeparatorBefore(SystemMenuCommand command)
    {
        if (InsertMenu(MenuHandle, (int)command, (Int32)(SystemMenuFlags.ByCommand | SystemMenuFlags.Separator), 0, String.Empty) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void InsertItemAfter(SystemMenuCommand command, int id, string text)
    {
        if (String.IsNullOrEmpty(text))
            throw new ArgumentNullException("text");
        if (!IsValidMenuIdValue(id))
            throw new ArgumentOutOfRangeException("id", "Valid range for menu id is [0 .. 0xF000]");
        int position = GetItemPosition(command);
        InsertItem(position + 1, id, text);
    }

    public void InsertSeparatorAfter(SystemMenuCommand command)
    {
        int position = GetItemPosition(command);
        InsertSeparator(position + 1);
    }

    public void AppendSeparator()
    {
        if (AppendMenu(MenuHandle, (int)SystemMenuFlags.Separator, 0, String.Empty) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void AppendItem(int id, string text)
    {
        if (String.IsNullOrEmpty(text))
            throw new ArgumentNullException("text");
        if (!IsValidMenuIdValue(id))
            throw new ArgumentOutOfRangeException("id", "Valid range for menu id is [0 .. 0xF000]");
        if (AppendMenu(MenuHandle, (int)SystemMenuFlags.String, id, text) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void RemoveItem(int position)
    {
        if (RemoveMenu(MenuHandle, position, (int)SystemMenuFlags.ByPosition) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void RemoveItem(SystemMenuCommand command)
    {
        if (RemoveMenu(MenuHandle, (int)command, (int)SystemMenuFlags.ByCommand) == 0)
            throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void Reset()
    {
        GetSystemMenu(WindowHandle, 1);
        _menuHandle = GetSystemMenu(WindowHandle, 0);
    }

    public static bool IsValidMenuIdValue(int id)
    {
        return id > 0 && id < 0xF000;
    }

    #region p/Invoke

    [DllImport("User32")]
    extern static IntPtr GetSystemMenu(IntPtr hWnd, int bRevert);
    [DllImport("User32", CharSet = CharSet.Auto, SetLastError = true)]
    extern static int AppendMenu(IntPtr hMenu, int uFlags, int uIDNewItem, string lpNewItem);
    [DllImport("User32", CharSet = CharSet.Auto, SetLastError = true)]
    extern static int InsertMenu(IntPtr hMenu, int uPosition, int uFlags, int uIDNewItem, string lpNewItem);
    [DllImport("User32", SetLastError = true)]
    extern static int RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);
    [DllImport("User32", SetLastError = true)]
    extern static int GetMenuItemCount(IntPtr hMenu);
    [DllImport("User32")]
    extern static int GetMenuItemID(IntPtr hMenu, int nPos);

    #endregion
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜