以某特定窗口的截图与C / GTK [英] Taking a screenshot of a particular window with c/GTK

查看:160
本文介绍了以某特定窗口的截图与C / GTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一张code的它创建了一个窗口:

This is a piece of code which creates a window:

#include <gtk/gtk.h>

static GtkWidget* createWindow()
{
    GtkWidget *window;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
    gtk_widget_set_name(window, "GtkLauncher");
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
    G_CALLBACK(gtk_main_quit), NULL);
    return window;
}

int main(int argc, char* argv[])
{
    GtkWidget *main_window;
    gtk_init(&argc, &argv);
    if (!g_thread_supported())
        g_thread_init(NULL);
    main_window = createWindow();
    gtk_widget_grab_focus(main_window);
    gtk_widget_show_all(main_window); 
    gtk_main();
    return 0;
}

和这里:转换GTK的python脚本到C ,我得到了如何采取截图。

And in here: Convert a GTK python script to C , i got how to take a screenshot.

gdk_get_default_root_window()会给我的桌面截图。

gdk_screen_get_active_window(gdk_screen_get_default())会给我任何活动窗口的截图。

gdk_screen_get_active_window (gdk_screen_get_default()) will give me the screenshot of any active window.

有什么办法把窗口的截图在code以上??

Is there any way to take the screenshot of the window being created in the code above??

推荐答案

我觉得这个应该这样做,虽然你可能需要遍历显示窗口后,主循环得到它正确绘制,在这种情况下,你会需要更多的code(我没有测试过这一点)

I think this should do it, although you may need to iterate the main loop after showing the window to get it to paint properly, in which case you'll need some more code (I haven't tested this)

#include <unistd.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <cairo.h>


static GtkWidget* createWindow()
{
    GtkWidget *window;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
    gtk_widget_set_name(window, "GtkLauncher");
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
    G_CALLBACK(gtk_main_quit), NULL);
    return window;
}


int main(int argc, char **argv)
{
    gdk_init(&argc, &argv);

    GtkWidget *main_window = createWindow();
    gtk_widget_show_all(main_window);

    // may not need this, it also may not be enough either
    while (gtk_events_pending ()) gtk_main_iteration (); 

    GdkWindow *w = GDK_WINDOW(main_window);


    gint width, height;
    gdk_drawable_get_size(GDK_DRAWABLE(w), &width, &height);

    GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(NULL, 
                       GDK_DRAWABLE(w), 
                       NULL, 
                       0,0,0,0,width,height);

    if(pb != NULL) {
        gdk_pixbuf_save(pb, "screenshot.png", "png", NULL);
        g_print("Screenshot saved to screenshot.png.\n");
    } else {
        g_print("Unable to get the screenshot.\n");
    }
    return 0;
}

如果它不工作,你必须移动的截图考虑到连接到某些事件的事件处理程序(我不知道这可能窗口状态事件,那么你必须看事件弄清楚何时采取截图)

If it doesn't work you'll have to move the screenshot taking into an event handler that connects to some event (I'm not sure which probably window-state-event then you have to look at the event to figure out when to take the screenshot)

这篇关于以某特定窗口的截图与C / GTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆