禁用,移除 WPF window窗体系统操作SystemMenu
2024-10-22 15:25:26
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, );
int cnt = GetMenuItemCount(hmenu); for (int i = cnt - ; i >= ; 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, ); //remove the menu item
RemoveMenu(hmenu, itemIndex, MF_DISABLED | MF_BYPOSITION); DrawMenuBar(windowHandle); //Redraw the menu bar
};
} [DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
public static IntPtr GetSysMenu(IntPtr hWnd, bool bRevert)
{
return GetSystemMenu(hWnd, bRevert);
} [DllImport("user32.dll")]
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
public static bool EnableMenu(IntPtr hMenu, uint uIDEnableItem, uint uEnable)
{
return EnableMenuItem(hMenu, uIDEnableItem, uEnable);
}
}
public class Win32ApiWrapper
{
[StructLayout(LayoutKind.Sequential)]
private struct Win32Point
{
public Int32 X;
public Int32 Y;
}; [DllImport("user32.dll")]
private static extern bool GetCursorPos(ref Win32Point pt); /// <summary>
/// 儅僂僗僇乕僜儖埵抲傪曉偡.<br/>
/// </summary>
/// <param name="relativeTo">The Visual to which the mouse coordinates will be relative.</param>
public static Point GetMousePosition(Visual relativeTo)
{
Win32Point mouse = new Win32Point();
GetCursorPos(ref mouse); return relativeTo.PointFromScreen(new Point((double)mouse.X, (double)mouse.Y));
} #region OCR Wrapper
[DllImport("SshSvcOCRHandle.dll")]
public static extern int CheckOCREnable();
#endregion // OCR Wrapper [DllImport("user32.dll")]
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
public static bool EnableMenu(IntPtr hMenu, uint uIDEnableItem, uint uEnable)
{
return EnableMenuItem(hMenu, uIDEnableItem, uEnable);
} [DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
public static IntPtr GetSysMenu(IntPtr hWnd, bool bRevert)
{
return GetSystemMenu(hWnd, bRevert);
} [DllImport("user32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
public static bool CheckWindow(string strWindowName)
{
IntPtr hwnd = FindWindow(strWindowName, null);
if (hwnd == IntPtr.Zero)
{
return false;
}
return true;
} [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, string lpName); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
}
使用:
private const uint MF_BYCOMMAND = 0x00000000;
private const uint MF_GRAYED = 0x00000001;
private const uint SC_RESTORE = 0xF120;
private const uint SC_MOVE = 0xF010;
private const uint SC_SIZE = 0xF000;
private const uint SC_MINIMIZE = 0xF020;
private const uint SC_MAXIMIZE = 0xF030;
private const uint SC_CLOSE = 0xF060; IntPtr handle = new WindowInteropHelper(metroWindow).Handle;
//xamlで、ResizeMode="NoResize"としても、メニューにマスクされないため明示的に行う。
var sysMenu = SystemMenuManager.GetSysMenu(handle, false);
SystemMenuManager.EnableMenu(sysMenu, SC_RESTORE, MF_BYCOMMAND | MF_GRAYED);
SystemMenuManager.EnableMenu(sysMenu, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
SystemMenuManager.EnableMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND | MF_GRAYED);
SystemMenuManager.EnableMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
最新文章
- SDCycleScrollView 滚动视图的使用(广告)
- Sqlserver 笔记 持续更新
- Service完全解析(转)
- 设计模式之原型模式(prototype)
- 【转】C++ 内存分配(new,operator new)详解
- 20151124 Jquery UI form 表单变成dialog
- Django - staticfiles,STATIC_ROOT, STATIC_URL,STATICFILES_DIRS
- Unity3d导出Recast geomset.txt
- 只有try和finally,没有catch
- 增加Myecllipse内存
- [iOS]深入理解GCD
- angularjs 异步请求无法更新数据
- Archive of all Android Studio releases / Eclipse 版本大全 / OpenJDK 各版本
- js 获取客户端mac地址
- F题:等差区间(RMQ||线段树)
- js图片跟随鼠标移动
- springboot对security的后端配置
- awk正则匹配nginx日志【原创】
- Domato学习
- 洛谷P2571 [SCOI2010]传送带 [三分]
热门文章
- 由 ToString()和Convert.ToString() 引发的问题
- C#委托内部使用局部的变量的问题
- dos编码格式 cmd编码 dos中文显示
- 关于 Android 状态栏的适配总结
- hadoop 自定义OutputFormat
- WC 个人项目 ( node.js 实现 )
- 每天一点产品思考(5):Web端链接跳转在当前页面刷新还是新标签页打开?
- nginx 的 proxy_cache 缓存配置
- 【error】C++:fatal error LNK1169: 找到一个或多个多重定义的符号
- XML配置报错