使用 C# 将鼠标单击发送到任务栏中的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:00:48  来源:igfitidea点击:

Sending a mouse click to a button in the taskbar using C#

提问by adeel825

In an application that I am currently working on, a requirement is to bring a window of an external application to the foreground. Making Win32 API calls such as BringWindowToTop and SetForeground window do not work all the time. This is due to some restrictions within Windows XP. What I would like to do instead is send simulate a mouse click the window's button on the taskbar which I am hoping will bring the window to the front. Does anyone know how this is possible?

在我目前正在处理的应用程序中,需要将外部应用程序的窗口置于前台。使 Win32 API 调用(例如,BringWindowToTop 和 SetForeground 窗口)始终不起作用。这是由于 Windows XP 中的一些限制造成的。我想做的是发送模拟鼠标单击任务栏上的窗口按钮,我希望这会将窗口带到前面。有谁知道这怎么可能?

采纳答案by Joel Lucsy

Check out the section "How to steal focus on 2K/XP" at http://www.codeproject.com/KB/dialog/dlgboxtricks.aspx, as this is exactly what you need. I wouldn't go the taskbar route as the taskbar could be hidden or simply not there.

查看http://www.codeproject.com/KB/dialog/dlgboxtricks.aspx上的“如何窃取对 2K/XP 的关注”部分,因为这正是您所需要的。我不会走任务栏路线,因为任务栏可能隐藏或根本不在那里。

回答by TheSmurf

It's possible. But it's extremely sketchy. Your application may also break with the next version of Windows, since it's undocumented. What you need to do is find the window handle of the taskbar, then find the window handle of the child window representing the button, then send it a WM_MOUSEDOWN (I think) message.

这是可能的。但它非常粗略。您的应用程序也可能会在下一版本的 Windows 中中断,因为它没有记录。你需要做的是找到任务栏的窗口句柄,然后找到代表按钮的子窗口的窗口句柄,然后给它发送一个WM_MOUSEDOWN(我认为)消息。

Here's a bit on finding the window handle of the taskbar:

这里有一点关于找到任务栏的窗口句柄:

http://www.codeproject.com/

http://www.codeproject.com/

FWIW, the restrictions on BringWindowToTop/SetForeground are there because it's irritating when a window steals focus. That may not matter if you're working on a corporate environment. Just keep it in mind. :)

FWIW,对BringWindowToTop/SetForeground 的限制是存在的,因为当窗口窃取焦点时它很烦人。如果您在公司环境中工作,这可能无关紧要。只要记住它。:)

回答by Nelson Miranda

I used this in a program where I needed to simulate clicks and mouse movements;

我在需要模拟点击和鼠标移动的程序中使用了它;

Global Mouse and Keyboard Library

全局鼠标和键盘库

回答by Ryan Farley

To be honest I've never had an issue bringing a window to the foreground on XP/Vista/2003/2000.

老实说,我从来没有遇到过在 XP/Vista/2003/2000 上将窗口置于前台的问题。

You need to make sure you do the following:

您需要确保执行以下操作:

  1. Check if IsIconic (minimized)
  2. If #1 results in true then call ShowWindow passing SW_RESTORE
  3. Then call SetForegroundWindow
  1. 检查是否 IsIconic(最小化)
  2. 如果 #1 结果为真,则调用 ShowWindow 传递 SW_RESTORE
  3. 然后调用 SetForegroundWindow

I've never had problems that I can think of doing it with those steps.

我从来没有遇到过我能想到用这些步骤来做的问题。