在第一次单击鼠标时获取活动应用程序的名称
本文关键字:应用程序 活动 获取 第一次 单击 鼠标 | 更新日期: 2023-09-27 18:10:34
我试图获得当前活动应用程序的名称,仅在该应用程序中的第一次鼠标单击。
我的意思是:当我总是在同一个应用程序上时,我不想每次用户点击应用程序时都获得应用程序的名称。我的代码确实运行得很好,但它在每次鼠标点击时都写入了应用程序名称。
如果我想只在用户单击另一个应用程序时获得应用程序的名称,我该如何更改它?
class InterceptMouse
{
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private const int WM_LBUTTONDOWN = 0x0201;
private const int WH_MOUSE_LL = 14;
public static void Main()
{
_hookID = SetMouseHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetMouseHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN)
{
IntPtr hwnd2 = GetForegroundWindow();
StringBuilder windowtitle = new StringBuilder(256);
if (GetWindowText(hwnd2, windowtitle, windowtitle.Capacity) > 0)
Console.WriteLine(windowtitle);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
int nMaxCount);
}
您至少有两个明显的选择。一个是保留你最后看到的标题,如果下一个标题等于最后看到的标题,就不要写。
static string lastseen = null;
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN)
{
IntPtr hwnd2 = GetForegroundWindow();
StringBuilder windowtitle = new StringBuilder(256);
if (GetWindowText(hwnd2, windowtitle, windowtitle.Capacity) > 0)
{
var title = windowtitle.ToString();
if (!String.Equals(lastseen, title)
{
lastseen = title;
Console.WriteLine(lastseen);
}
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
第二种选择是保留一个包含已见标题的HashSet,向HashSet中添加新标题,如果HashSet
中已经找到一个标题,则忽略该标题。static seen = new HashSet<string>();
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN)
{
IntPtr hwnd2 = GetForegroundWindow();
StringBuilder windowtitle = new StringBuilder(256);
if (GetWindowText(hwnd2, windowtitle, windowtitle.Capacity) > 0)
{
var title = windowtitle.ToString();
if (!seen.Contains(title))
{
seen.Add(title);
Console.WriteLine(title);
}
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}