简体   繁体   English

如何在C#应用程序中将焦点设置到桌面

[英]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); 
    } 
} 
} 

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 . 您首先需要做的就是调用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.

相关问题 当我将可见的正确设置为true时,如何防止我的C#winforms应用程序窃取焦点? - How to prevent my C# winforms application from stealing focus when I set the visible properly to true? 如何在C#中为桌面应用程序创建activeX组件 - How do I create an activeX component for a desktop application in C# 如何在C#Metro应用程序的AppBar中聚焦文本框? - How do I focus a TextBox in an AppBar in a C# Metro application? 如何从C#桌面应用程序以全屏模式打开默认浏览器? - How do I open default browser in fullscreen mode from a C# desktop application? 如何在 C# 中的 Outlook 365 桌面应用程序中启动新的电子邮件对话框? - How Do I Start a New E-mail Dialogue Box Within Outlook 365 Desktop Application in C#? 如何在C#桌面应用程序中使用FB API图 - How to Use FB API Graph within C# Desktop Application 如何设置C#应用程序以将程序文件用于应用程序,并将应用程序数据用于数据库? - how do I set up my C# application to use program files for the app and app data for the DB? MVC:如何在javascript块中设置C#静态变量? - MVC: How do I set a C# static variable from within a javascript block? 如何为C#桌面应用程序设置默认时区? - How to set Default Time zone for C# Desktop Application? C#如何暂停程序并等待WinForm中的键盘输入 - C# how do I pause my program and wait for keyboard input from within a WinForm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM