c# - 如何获取不属于我的应用程序的事件窗口?

标签 c# .net pinvoke

如何获取用户当前关注的窗口标题? 我正在制作一个与另一个窗口一起运行的程序,如果用户没有关注那个窗口,我发现我的程序没有理由继续更新。

那么我怎样才能确定用户关注的是哪个窗口呢?

我确实尝试调查过

[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();

但我似乎只能在 Window 是我的应用程序的一部分时才能使用它,但事实并非如此。

最佳答案

检查这段代码:

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();


[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

private string GetActiveWindowTitle()
{
    const int nChars = 256;
    StringBuilder Buff = new StringBuilder(nChars);
    IntPtr handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
     return Buff.ToString();
    }
  return null;
}

关于c# - 如何获取不属于我的应用程序的事件窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32965310/

相关文章:

c# - 添加动态评级控件时出现奇怪的编译问题

c# - 如何在 Silverlight 中的 Border 元素上制作虚线边框?

c# - 如何使用 Windows 10 创建特定于设备系列的项目,例如在 W8.1/WP8.1 上?

c# - 在 C# 中使用与指针相关的 C 方法

c# - 正在分发 dll : What to do with . lib 文件吗?

c# - 打开 Excel 错误 : System. Runtime.InteropServices.COMException (0x80080005):正在检索具有 CLSID 的组件的 COM 类工厂

c# - 读取文件中的第一行和最后一行

c# - 显示和捕获 C# 异常

c# - C++ 函数到 C#

c# - P/Invoke Struct with Pointers, C++ 从 C#