[DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int RegisterWindowMessage(string msg);
//取得Shell窗口句柄函数
[DllImport("user32.dll")]
public static extern IntPtr GetShellWindow();
//取得桌面窗口句柄函数
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
//取得窗口大小函数
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;
public RECT rc;
public IntPtr lParam;
}
public enum ABMsg : int
{
ABM_NEW = 0,
ABM_REMOVE,
ABM_QUERYPOS,
ABM_SETPOS,
ABM_GETSTATE,
ABM_GETTASKBARPOS,
ABM_ACTIVATE,
ABM_GETAUTOHIDEBAR,
ABM_SETAUTOHIDEBAR,
ABM_WINDOWPOSCHANGED,
ABM_SETSTATE
}
bool RunningFullScreenApp;
private IntPtr desktopHandle;
private IntPtr shellHandle;
int uCallBackMsg;
public void RegisterAppBar(bool registered)
{
APPBARDATA abd = new APPBARDATA();
abd.cbSize = Marshal.SizeOf(abd);
abd.hWnd = this.Handle;
desktopHandle = MyScreenSaver.GetDesktopWindow();
shellHandle = MyScreenSaver.GetShellWindow();
if (!registered)
{
//register
uCallBackMsg = MyScreenSaver.RegisterWindowMessage("APPBARMSG_优快云_HELPER");
abd.uCallbackMessage = uCallBackMsg;
uint ret = MyScreenSaver.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);
}
else
{
MyScreenSaver.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);
}
}
//重载窗口消息处理函数
protected override void WndProc(ref System.Windows.Forms.Message m)
{
//屏幕大小
Rectangle rect = Screen.PrimaryScreen.Bounds;
RunningFullScreenApp = false;
foreach (Process p in Process.GetProcesses())
{
//找到想要的进程
/*if (p.ProcessName == "POWERPNT")
{
}*/
GetWindowRect(p.MainWindowHandle, out RECT appBounds);
//比较
if ((appBounds.right - appBounds.left) >= rect.Width && (appBounds.bottom - appBounds.top) >= rect.Height)
{
//to do,这种情况肯定已经是全屏了
RunningFullScreenApp = true;
}
}
base.WndProc(ref m);
}
用的时候调用
RegisterAppBar(false)
不用的时候调用
RegisterAppBar(true)
参考:https://blog.youkuaiyun.com/jingzhongrong/article/details/5385951