[英]How do I set the focus to the Desktop from within my C# application
Winforms App. Winforms应用程序。 .Net 3.5. .Net 3.5。
I need to set the focus from my C# application to the user's desktop (almost like simulating a mouse click on the desktop). 我需要将焦点从我的C#应用程序设置到用户的桌面(几乎就像在桌面上模拟鼠标单击一样)。
Can someone please show me how to do this with C#? 有人可以告诉我如何使用C#吗? I just want to set focus on the desktop so the focus is no longer on my application but I want to do this from within my application. 我只想将焦点放在桌面上,因此焦点不再放在我的应用程序上,而是要在我的应用程序内执行此操作。
Edit: An answer below works by setting the focus to the desktop, but it minimizes all the open windows on the user's desktop. 编辑:下面的答案通过将焦点设置在桌面上而起作用,但是它最小化了用户桌面上所有打开的窗口。
Is there a way I can maybe set the focus to the next open window on the desktop instead? 有什么方法可以将焦点设置为桌面上的下一个打开的窗口吗? I just want to get the focus off of my application (without minimizing my application or hiding it). 我只想将重点转移到我的应用程序上(而不是最小化或隐藏应用程序)。 I just want to move focus to somewhere else. 我只想将重点转移到其他地方。 Maybe the desktop was not the best choice if it will minimize all the user's open windows/applications. 如果它将最小化所有用户打开的窗口/应用程序,则桌面不是最佳选择。
This should do it for you. 这应该为您做。
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1 {
class Program {
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
static void Main(string[] args) {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
System.Threading.Thread.Sleep(2000);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
}
}
}
I don't have a code example ready for these two but I'm going to give you the links to both. 我没有针对这两个代码的示例,但我将为您提供两者的链接。 The first think you need to do is call GetWindow
. 您首先需要做的就是调用GetWindow
。 After doing that you'll want to call SwitchToThisWindow
passing in the pointer you received from GetWindow
. 完成之后,您将要调用SwitchToThisWindow
并传入从GetWindow
收到的指针。
You can add this COM object in your project: 您可以在项目中添加此COM对象:
Microsoft Shell Controls And Automation Microsoft Shell控件和自动化
And then just call: 然后只需调用:
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
This will minimize all the windows and then focus the desktop. 这将最小化所有窗口,然后将焦点放在桌面上。 Otherwise, if you have your window non-full screen then you can simulate the mouse click using: 否则,如果窗口非全屏显示,则可以使用以下方法模拟鼠标单击:
//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);
}
You can calculate coordinates by looking at your window startup location plus height/width and select a available space (that will be the desktop indeed). 您可以通过查看窗口的启动位置加上高度/宽度并选择一个可用空间(实际上就是桌面)来计算坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.