c# - 我如何使用 Mono C# 获取有关关注 Linux 的窗口的信息

标签 c# linux mono

我已经使用 Windows 用户组件 (user32.dll) 开发了一个 C# .NET WindowsForms 应用程序,它会在每次用户更改焦点时保存事件窗口(具有焦点的窗口)的标题。

现在我打算在 Linux 上使用 Mono C# 来做同样的事情。可能吗?

如果是,我在找什么?

最佳答案

我决定看一下 gnome-screenshot 的源代码,它有这样的功能(只截取事件窗口的屏幕截图):

static GdkWindow *
screenshot_find_active_window (void)
{
  GdkWindow *window;
  GdkScreen *default_screen;

  default_screen = gdk_screen_get_default ();
  window = gdk_screen_get_active_window (default_screen);

  return window;
}

当上面没有返回任何内容时,它有一些逻辑可以回退到“鼠标指针下的窗口”:

GdkWindow *
do_find_current_window (void)
{
  GdkWindow *current_window;
  GdkDeviceManager *manager;
  GdkDevice *device;

  current_window = screenshot_find_active_window ();
  manager = gdk_display_get_device_manager (gdk_display_get_default ());
  device = gdk_device_manager_get_client_pointer (manager);

  /* If there's no active window, we fall back to returning the
   * window that the cursor is in.
   */
  if (!current_window)
    current_window = gdk_device_get_window_at_position (device, NULL, NULL);

  if (current_window)
    {
      if (screenshot_window_is_desktop (current_window))
    /* if the current window is the desktop (e.g. nautilus), we
     * return NULL, as getting the whole screen makes more sense.
         */
        return NULL;

      /* Once we have a window, we take the toplevel ancestor. */
      current_window = gdk_window_get_toplevel (current_window);
    }

  return current_window;
}

据我所知,以上所有内容都依赖于 libgdk-pixbuf。如果那不是一个选项,您总是可以在 Gdk 的源代码中查看这些函数的实现。

关于c# - 我如何使用 Mono C# 获取有关关注 Linux 的窗口的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134124/

相关文章:

linux - 谁在等待 shell 后台进程?

c# - 在 Linux 上运行 .NET Core——什么都不写

linux - 如何使用 GtkSharp 的 Chromium 嵌入式框架?

c# - Windows 8自动翻转图片

linux - 如何将用户配额设置为零?

linux - 如何确保正在运行的进程是我期望正在运行的进程?

c# - 如何在不引用 X11 的情况下以单声道绘制 png 图像

c# - 自定义 JSON 转换为 C# 对象

c# - 在对象初始值设定项中使用后清除变量是否也会清除对象的属性?

c# - 数据类冲突有什么解决方法吗?