c# 里面隐藏了form 的边框,任务栏就没有了系统菜单。如何显示呢,看下文。
调用方式 在form 构造函数 或者load中 TaskMenu.ShowSYSMENU(this);
[DllImport("user32.dll" , EntryPoint = "GetWindowLong" , CharSet = CharSet.Auto)]
private static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll" , EntryPoint = "SetWindowLong" , CharSet = CharSet.Auto)]
private static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
private const int WS_SYSMENU = 0x00080000 ;
public static void ShowSYSMENU(Form form)
{
int windowLong = (GetWindowLong(new HandleRef(form, form.Handle), -16 ));
SetWindowLong(new HandleRef(form, form.Handle), -16 , windowLong | WS_SYSMENU | 0x20000 | 0x40000 );
int menu = Win32.GetSystemMenu(form.Handle.ToInt32(), 0 );
if (!form.ControlBox)
{
Win32.DeleteMenu(menu, Win32.SC_CLOSE, 0x0 );//关闭
Win32.DeleteMenu(menu, Win32.SC_MINIMIZE, 0x0 );//最小化
Win32.DeleteMenu(menu, Win32.SC_MAXIMIZE, 0x0 );//最大化
}
else
{
if (!form.MinimizeBox)
{
Win32.DeleteMenu(menu, Win32.SC_MINIMIZE, 0x0 );//最小化
}
if (!form.MaximizeBox)
{
Win32.DeleteMenu(menu, Win32.SC_MAXIMIZE, 0x0 );//最大化
}
}
}
private static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll" , EntryPoint = "SetWindowLong" , CharSet = CharSet.Auto)]
private static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
private const int WS_SYSMENU = 0x00080000 ;
public static void ShowSYSMENU(Form form)
{
int windowLong = (GetWindowLong(new HandleRef(form, form.Handle), -16 ));
SetWindowLong(new HandleRef(form, form.Handle), -16 , windowLong | WS_SYSMENU | 0x20000 | 0x40000 );
int menu = Win32.GetSystemMenu(form.Handle.ToInt32(), 0 );
if (!form.ControlBox)
{
Win32.DeleteMenu(menu, Win32.SC_CLOSE, 0x0 );//关闭
Win32.DeleteMenu(menu, Win32.SC_MINIMIZE, 0x0 );//最小化
Win32.DeleteMenu(menu, Win32.SC_MAXIMIZE, 0x0 );//最大化
}
else
{
if (!form.MinimizeBox)
{
Win32.DeleteMenu(menu, Win32.SC_MINIMIZE, 0x0 );//最小化
}
if (!form.MaximizeBox)
{
Win32.DeleteMenu(menu, Win32.SC_MAXIMIZE, 0x0 );//最大化
}
}
}