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

时间:2013-04-21 17:34:07

标签: c# linux mono

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

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

如果是的话,我正在寻找什么?

1 个答案:

答案 0 :(得分:2)

我决定查看具有此类功能的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源代码中这些函数的实现。