具體而言,不只是通過刪除按鈕,但完全禁用最大化。這意味着在Windows 7中雙擊標題欄或將標題欄拖動到屏幕頂部將不起作用。儘管如此,我仍然希望這個窗口很大。我可以防止WPF中的窗口最大化嗎?
回答
看看CanMinimize字段。
這不符合我的要求。我想禁用最大化,但允許調整大小。 CanMinimize可以防止調整大小。 – 2010-08-11 14:53:05
...並且在沒有錯誤時改變人們的拼寫是不禮貌的。我們不是所有美國人,你知道;) – 2010-08-11 14:56:57
對不起,儘管它的價值,「最大化」匹配的MSDN文檔:) – 2010-08-11 15:31:35
從系統菜單中刪除最大化應該足夠了。我不知道這是否會爲Win7「對接」工作,讓我知道如果它。
這裏有一個輔助類的文章,修改一個窗口的系統菜單: http://www.codeguru.com/csharp/csharp/cs_misc/userinterface/article.php/c9327/
它假設的WinForms,但只因爲你需要一個窗口句柄。在WPF中,這可以通過WindowInteropHelper獲得。
更新
此代碼是UI框架不可知
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
}
- 1. 如何最大化我的主窗口並防止在QT中調整大小
- 2. WPF最大化窗口比屏幕大
- 3. 我可以防止NSDocument保存窗口位置嗎?
- 4. 防止WPF窗口最小化(Winkey + D,主要是)
- 5. WPF窗口最大化完全
- 6. 不能始終最大化WPF窗口
- 7. 防止ShowDialog關閉在WPF的窗口
- 8. Tkinter窗口,以防止PermissionError
- 9. 窗口最大化最大化
- 10. WPF窗口全屏與窗口最大化
- 11. 對於窗口定義,我可以使用WPF窗口中的XamlReader.Load或InitializeFromXaml嗎?
- 12. 最大化窗口我的形式
- 13. WPF窗口沒有最小化/最大化
- 14. 防止窗口...
- 15. WPF中最小化的窗口位置
- 16. 最小化WPF中的窗口?
- 17. 當主窗口最小化時,WPF聚焦可見窗口
- 18. Ext-GWT可以使窗口最小化事件動畫化嗎?
- 19. 可停靠窗口 - 不能最大化
- 20. 獲取最大化最小化窗口的窗口位置
- 21. Rollapply&xts。我可以在窗口中輸出最大值的時間嗎?
- 22. 如何從最大化的WPF窗口中刪除邊框?
- 23. WPF Ribbon - 我們可以隱藏Ribbon ApplicationMenu的輔助窗口嗎?
- 24. 禁用窗口最大化/最小化
- 25. 如何最大化窗口?
- 26. Caliburn.Micro最大化Silverlight窗口
- 27. PHP Webdriver - 最大化窗口
- 28. JFrame最大化窗口
- 29. 如何渲染窗口關閉/最大化/最小化wpf中的按鈕?
- 30. 是否可以最大化Flash投影儀的窗口?
所以它的可調整大小?這並不意味着我可以手動將它拖動到屏幕的全尺寸,並基本上最大化? 使窗口不能最大化的重點是什麼?關於你的窗口有那麼重要,我可以將它拉長到全屏,但不能簡單地使用我永遠用來做同樣的按鈕? – 2010-08-11 16:02:10
爲什麼人們在StackOverflow上一直這樣做?與其回答一個問題,他們開始質疑爲什麼這是必需的。這沒有幫助。 只要接受它作爲軟件的一個要求,並且如果你知道如何保持它的獨立性,就可以回答它。在這種情況下,原因是因爲客戶想要支付我幾千歐元來做到這一點。 – 2010-08-13 16:25:47