原文 [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)

 [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
}; }
}

[WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)的更多相关文章

  1. [WPF疑难]如何禁用窗口上的关闭按钮

    原文 [WPF疑难]如何禁用窗口上的关闭按钮 [WPF疑难]如何禁用窗口上的关闭按钮                                           周银辉 哈哈,主要是调用Rem ...

  2. [WPF疑难] 继承自定义窗口

    原文 [WPF疑难] 继承自定义窗口 [WPF疑难] 继承自定义窗口 周银辉 项目中有不少的弹出窗口,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于Window自身的,但每个弹出框 ...

  3. [WPF疑难]避免窗口最大化时遮盖任务栏

    原文 [WPF疑难]避免窗口最大化时遮盖任务栏 [WPF疑难]避免窗口最大化时遮盖任务栏 周银辉 WPF窗口最大化时有个很不好的现象是:如果窗口的WindowStyle被直接或间接地设置为None后( ...

  4. [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口

    原文:[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 周银辉 现象: 大家可以试试下面这个很有趣但会带来Defect的现象:当我 ...

  5. 通过 AppSwitch 禁用 WPF 内置的触摸让 WPF 程序可以处理 Windows 触摸消息

    原文:通过 AppSwitch 禁用 WPF 内置的触摸让 WPF 程序可以处理 Windows 触摸消息 WPF 框架自己实现了一套触摸机制,但同一窗口只能支持一套触摸机制,于是这会禁用系统的触摸消 ...

  6. [WPF疑难]Hide me! not close

    原文 [WPF疑难]Hide me! not close [WPF疑难]Hide me! not close                               周银辉 有朋友遇到这样的一个问 ...

  7. WPF的消息机制(二)- WPF内部的5个窗口之隐藏消息窗口

    目录 WPF的消息机制(一)-让应用程序动起来 WPF的消息机制(二)-WPF内部的5个窗口 (1)隐藏消息窗口 (2)处理激活和关闭的消息的窗口和系统资源通知窗口 (3)用于用户交互的可见窗口 (4 ...

  8. WPF的消息机制(三)- WPF内部的5个窗口之处理激活和关闭的消息窗口以及系统资源通知窗口

    原文:WPF的消息机制(三)- WPF内部的5个窗口之处理激活和关闭的消息窗口以及系统资源通知窗口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/p ...

  9. 【WPF】运用MEF实现窗口的动态扩展

    若干年前,老周写了几篇有关MEF的烂文,简单地说,MEF是一种动态扩展技术,比如可以指定以某个程序集或某个目录为搜索范围,应用程序在运行时会自动搜索符合条件的类型,并自动完成导入,这样做的好处是,主程 ...

随机推荐

  1. MyGui 3.2.0(OpenGL平台)的编译

    MyGui是一个用来创建用户图形界面的库,用于游戏和3D应用程序.这个库的主要目标是达到:快速.灵活.易用. 1.下载准备: 源代码:http://svn.code.sf.net/p/my-gui/c ...

  2. 七日筑基——C#第一天(下)

    继续C#第一天的内容,昨天我们简单说了一下如何用C#代码来让学生做自我介绍,介绍的格式要求:“我叫威震天,今年20岁,我喜欢踢足球和上网,希望接下来的三年能跟大家一起成长.”威震天介绍完了,继续下一个 ...

  3. js调试若干

    主要是将 chrome调试工具   firebug的控制台对以下都有支持 consoleAPI https://developers.google.com/chrome-developer-tools ...

  4. CSS hank

    CSS hank CSS hank是为了让CSS代码兼容不同浏览器,也可以通过CSS hank为不同的浏览器设置不同的CSS样式. CSS hank的3种表现形式: 类内部hank IE6能识别下划线 ...

  5. MFC下DLL编程(图解)

    DLL(Dynamic Link Library,动态链接库)是微软公司为Windows和OS/2操作系统设计一种供应用程序在运行时调用的共享函数库.DLL是应用程序的一种扩展,也是软件共享和重用的传 ...

  6. 使2个div 在一行上显示

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  7. ajax+php如何获取部分请求的信息显示在对应的div中

    我该如何把需要显示的信息显示在文章列表中呢???

  8. python中3个帮助函数help、dir、type的使用

    1.help函数:查看模块.函数.变量的详细说明: 查看模块 help("modules") 查看包  help("json") 查看类 help(json.J ...

  9. [转载]Android 知识图谱

    from: http://blog.csdn.net/xyz_lmn/article/details/41411355

  10. BZOJ 1062

    program candy bzoj1062; ; maxm=; maxn=; var n,len,m,i,p,t,l,r,c,d,q:longint; s:..,..maxn,..maxm] of ...