[转]如何禁用WPF窗口的系统菜单(SystemMenu)
2009-12-02 08:50
411 查看
[WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu) 周银辉 点击窗口左上角图标时弹出来的菜单也就是这里所说的系统菜单(SystemMenu),有时需要禁用(移除)其中的某些或全部菜单项。刚才也有网友问到了这一点,OK,贴代码: 要全部禁用(移除)菜单项请调用SystemMenuManager.RemoveWindowSystemMenu(Window window)方法,想部分禁用(移除)菜单项则调用SystemMenuManager.RemoveWindowSystemMenuItem(Window window, int itemIndex)方法。
值得注意的是禁用了其中的菜单项那么与之相关联的功能也会被禁用,比如将“关闭”从其中移除,那么窗口的右上角的关闭按钮也会被禁用,在任务栏的窗口图标上右击也不会出现相应的项目 public static class SystemMenuManager
{
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
private static extern IntPtr GetSystemMenu(IntPtr hwnd, int revert);
[DllImport("user32.dll", EntryPoint = "GetMenuItemCount")]
private static extern int GetMenuItemCount(IntPtr hmenu);
[DllImport("user32.dll", EntryPoint = "RemoveMenu")]
private static extern int RemoveMenu(IntPtr hmenu, int npos, int wflags);
[DllImport("user32.dll", EntryPoint = "DrawMenuBar")]
private static extern int DrawMenuBar(IntPtr hwnd);
private const int MF_BYPOSITION = 0x0400;
private const int MF_DISABLED = 0x0002;
public static void RemoveWindowSystemMenu(Window window)
{
if(window == null)
{
return;
}
window.SourceInitialized += window_SourceInitialized;
}
static void window_SourceInitialized(object sender, EventArgs e)
{
var window = (Window) sender;
var helper = new WindowInteropHelper(window);
IntPtr windowHandle = helper.Handle; //Get the handle of this window
IntPtr hmenu = GetSystemMenu(windowHandle, 0);
int cnt = GetMenuItemCount(hmenu);
for (int i = cnt - 1; i >= 0; i--)
{
RemoveMenu(hmenu, i, MF_DISABLED | MF_BYPOSITION);
}
}
public static void RemoveWindowSystemMenuItem(Window window, int itemIndex)
{
if (window == null)
{
return;
}
window.SourceInitialized += delegate
{
var helper = new WindowInteropHelper(window);
IntPtr windowHandle = helper.Handle; //Get the handle of this window
IntPtr hmenu = GetSystemMenu(windowHandle, 0);
//remove the menu item
RemoveMenu(hmenu, itemIndex, MF_DISABLED | MF_BYPOSITION);
DrawMenuBar(windowHandle); //Redraw the menu bar
};
}
}
注:
转载必须保留原作者信息和本文原始链接
本文原始链接:/article/4664986.html
值得注意的是禁用了其中的菜单项那么与之相关联的功能也会被禁用,比如将“关闭”从其中移除,那么窗口的右上角的关闭按钮也会被禁用,在任务栏的窗口图标上右击也不会出现相应的项目 public static class SystemMenuManager
{
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
private static extern IntPtr GetSystemMenu(IntPtr hwnd, int revert);
[DllImport("user32.dll", EntryPoint = "GetMenuItemCount")]
private static extern int GetMenuItemCount(IntPtr hmenu);
[DllImport("user32.dll", EntryPoint = "RemoveMenu")]
private static extern int RemoveMenu(IntPtr hmenu, int npos, int wflags);
[DllImport("user32.dll", EntryPoint = "DrawMenuBar")]
private static extern int DrawMenuBar(IntPtr hwnd);
private const int MF_BYPOSITION = 0x0400;
private const int MF_DISABLED = 0x0002;
public static void RemoveWindowSystemMenu(Window window)
{
if(window == null)
{
return;
}
window.SourceInitialized += window_SourceInitialized;
}
static void window_SourceInitialized(object sender, EventArgs e)
{
var window = (Window) sender;
var helper = new WindowInteropHelper(window);
IntPtr windowHandle = helper.Handle; //Get the handle of this window
IntPtr hmenu = GetSystemMenu(windowHandle, 0);
int cnt = GetMenuItemCount(hmenu);
for (int i = cnt - 1; i >= 0; i--)
{
RemoveMenu(hmenu, i, MF_DISABLED | MF_BYPOSITION);
}
}
public static void RemoveWindowSystemMenuItem(Window window, int itemIndex)
{
if (window == null)
{
return;
}
window.SourceInitialized += delegate
{
var helper = new WindowInteropHelper(window);
IntPtr windowHandle = helper.Handle; //Get the handle of this window
IntPtr hmenu = GetSystemMenu(windowHandle, 0);
//remove the menu item
RemoveMenu(hmenu, itemIndex, MF_DISABLED | MF_BYPOSITION);
DrawMenuBar(windowHandle); //Redraw the menu bar
};
}
}
注:
转载必须保留原作者信息和本文原始链接
本文原始链接:/article/4664986.html
相关文章推荐
- [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)
- [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)
- [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)
- [WPF疑难]如何禁用窗口上的关闭按钮
- WPF中如何禁用/去除窗口右上角的关闭按钮
- [WPF疑难]如何禁用窗口上的关闭按钮
- [WPF疑难]如何禁用窗口上的关闭按钮
- WPF中如何禁用/去除窗口右上角的关闭按钮
- 【WPF】如何让弹出的窗口"阻塞"
- wpf 如何让当前窗口隐藏
- WPF中,如何将Vista Aero效果扩展到整个窗口
- WPF学习笔记-如何按ESC关闭窗口
- WPF中禁用窗口的“alt+空格”功能菜单键
- WPF如何仿制QQ2013登录窗口的关闭效果
- wpf中如何将窗口设置为透明
- [WPF]如何实现设置弹出窗口的Owner属性功能?
- wpf中如何将窗口设置为透明
- 如何禁用窗口关闭按钮在一个 MFC 多文档界面 (MDI) 应用程序
- 如何实现一个不规则形状的WPF窗口
- [WPF]如何实现设置UserControl 弹出窗口的Owner属性功能