您可以在项目中添加此 COM 对象:
Microsoft Shell 控件和自动化
然后只需调用:
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
这将最小化所有窗口,然后聚焦桌面。否则,如果您的窗口非全屏,则可以使用以下命令模拟鼠标单击:
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
您可以通过查看窗口启动位置加上高度/宽度来计算坐标,然后选择一个可用空间(确实是桌面)。