C# 如何在c中基于鼠标点击获取活动窗口名#
我试图让一个应用程序获得鼠标点击的位置和用户点击的窗口的标题(名称)。 例如,若我单击任务栏中打开的outlook,那个么我需要在数据库中将应用程序名存储为“Microsoft outlook” 提前感谢….检查此代码: 名称空间:C# 如何在c中基于鼠标点击获取活动窗口名#,c#,C#,我试图让一个应用程序获得鼠标点击的位置和用户点击的窗口的标题(名称)。 例如,若我单击任务栏中打开的outlook,那个么我需要在数据库中将应用程序名存储为“Microsoft outlook” 提前感谢….检查此代码: 名称空间: using System; using System.Runtime.InteropServices; using System.Text; using System.Threading; 方法: [DllImport("user32.dll")] private
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
方法:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
电话:
// get handle
IntPtr handle = GetForegroundWindow();
// get title
const int count = 512;
var text = new StringBuilder(count);
if (GetWindowText(handle, text, count) > 0)
{
MessageBox.Show(text.ToString());
}
我用过几次这个代码。非常容易使用。你可以设置一个计时器,每10毫秒启动一次。保存2个变量。一个是活动窗口,另一个是上次聚焦的窗口。在伪代码中说:如果newWindow!=oldWindow->listView.Add(窗口)
可能是这样的:
public partial class Form1 : Form
{
// DECLARE GLOBALS //
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public static string oldWindow = "";
public static string currentWindow = "";
// TIMER EVENT //
private void timerCheckFocus_Tick(object sender, EventArgs e)
{
// get handle
IntPtr handle = GetForegroundWindow();
// get title
const int count = 512;
var currentWindow = new StringBuilder(count);
if (currentWindow.ToString() != oldWindow)
{
// add your window to a listView //
oldWindow = currentWindow.ToString();
}
}
您可以导入user32.dll
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetForegroundWindow();
然后,例如,使用计时器查找活动应用程序。process.MainWindowTitle提供当前窗口/应用程序的名称
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
IntPtr activeWindow = GetForegroundWindow();
List<String> strListProcesses = new List<string>();
foreach (Process process in Process.GetProcesses())
{
try
{
if (activeWindow == process.MainWindowHandle)
{
newApplication = process.MainWindowTitle;
}
}
catch (System.ComponentModel.Win32Exception ex)
{
}
}
}
private void dispatchermer_Tick(对象发送方,事件参数e)
{
IntPtr activeWindow=GetForegroundWindow();
List strlistprocess=newlist();
foreach(Process.getprocesss()中的进程)
{
尝试
{
if(activeWindow==process.MainWindowHandle)
{
newApplication=process.MainWindowTitle;
}
}
捕获(System.ComponentModel.Win32Exception ex)
{
}
}
}
有商业工具可用于监视您的用户。如果您想自己创建一个,请阅读WinApi手册。你可以从六点开始。这个问题太广泛了。我的代码和上面一样,我得到了应用程序文本,但没有得到它的名称。例如,对于VisualStudio,我得到“ApplicationMonitoring(Running)-MicrosoftVisualStudio”,但我只需要“MicrosoftVisualStudio”,您需要“窗口名”。“ApplicationMonitoring(Running)-Microsoft Visual Studio”是窗口名称。看起来您正在查找应用程序名称;)。我的代码和上面一样,我得到应用程序文本,但没有得到它的名称。例如,对于VisualStudio,我得到“ApplicationMonitoring(Running)-MicrosoftVisualStudio”,但我只需要“MicrosoftVisualStudio”,这样您就不会搜索窗口名称。您要查找的是应用程序名称。编辑你的问题!我现在在工作。我稍后在家时会尝试编辑我的代码。和平。