How do I set the focus to the Desktop from within my C# application

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

Winforms App. .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).

Can someone please show me how to do this with 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.

回答1:

You can add this COM object in your project:

Microsoft Shell Controls And Automation

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).



回答2:

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);      }  }  }  

Get Next Window

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. After doing that you'll want to call SwitchToThisWindow passing in the pointer you received from GetWindow.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!