GTK 制作学习笔记(一)

通过这几天GTK的学习,网上关于gtk的资料少之又少!

分享几个执行成功的案例,纯用来感兴趣的学习使用。

下面是制作一个接盘界面,可以模拟鼠标点击,点击Quit可以退出。

一、效果图:

二、代码:

这个代码对gtk版本没有太大要求,2.-或者3.-或者4.-应该都可以,大家可以尝试后留言,我这个是gtk2版本。

#include <gtk/gtk.h>
/* 我们的回调。
* 传到这个函数的数据被打印到标准输出 */
void callback(GtkWidget* widget, gpointer data)
{
    g_print("%s", (char*)data);
}
/* 这个回调退出程序 */
gint delete_event(GtkWidget* widget, GdkEvent* event, gpointer data)
{
    gtk_main_quit();
    return FALSE;
}
int main(int argc, char* argv[])
{
    GtkWidget* window;
    GtkWidget* button;
    GtkWidget* table;
    gtk_init(&argc, &argv);
    /* 创建一个新窗口 */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    /* 设置窗口标题 */
    gtk_window_set_title(GTK_WINDOW(window), "Rong Tao's Keyboard");
    /* 为 delete_event 设置一个立即退出 GTK 的处理函数。 */
    g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
    /* 设置窗口的边框宽度。 */
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);
    /* 创建一个 2x2 的表 */
    table = gtk_table_new(4, 20, TRUE);
    /* 将表放进主窗口 */
    gtk_container_add(GTK_CONTAINER(window), table);
    /* 创建第一个按钮 */
    button = gtk_button_new_with_label("Q");
    /* 当这个按钮被点击时,我们调用 "callback" 函数,并将一个指向"button 1"的指针作为它的参数 */
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"q");
    /* 将 button 1 插入表的左上象限(quadrant) */
    gtk_table_attach_defaults(GTK_TABLE(table), button, 0, 2, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("W");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"w");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 2, 4, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("E");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"e");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 4, 6, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("R");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"r");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 6, 8, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("T");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"t");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 8, 10, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("Y");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"y");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 10, 12, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("U");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"u");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 12, 14, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("I");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"i");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 14, 16, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("O");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"o");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 16, 18, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("P");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"p");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 18, 20, 0, 1);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("A");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"a");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 1, 3, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("S");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"s");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 3, 5, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("D");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"d");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 5, 7, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("F");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"f");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 7, 9, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("G");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"g");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 9, 11, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("H");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"h");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 11, 13, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("J");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"j");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 13, 15, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("K");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"k");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 15, 17, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("L");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"l");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 17, 19, 1, 2);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("'");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"'");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 0, 2, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("Z");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"z");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 2, 4, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("X");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"x");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 4, 6, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("C");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"c");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 6, 8, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("V");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"v");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 8, 10, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("B");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"b");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 10, 12, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("N");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"n");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 12, 14, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("M");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"m");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 14, 16, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label(",");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)",");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 16, 18, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label(".");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)".");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 18, 20, 2, 3);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label("Enter");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"\n");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 2, 6, 3, 4);
    gtk_widget_show(button);
    /*********************/
    button = gtk_button_new_with_label(" ");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)" ");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 6, 16, 3, 4);
    gtk_widget_show(button);
    /* 创建"Quit"按钮 */
    button = gtk_button_new_with_label("Quit");
    /* 当这个按钮被点击时,我们调用 "delete_event" 函数接着程序就退出了 */
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(delete_event), NULL);
    /* 将退出按钮插入表的下面两个象限 */
    gtk_table_attach_defaults(GTK_TABLE(table), button, 16, 19, 3, 4);
    gtk_widget_show(button);

    gtk_widget_show(table);
    gtk_widget_show(window);
    gtk_main();
    return 0;
}
相关推荐
EterNity_TiMe_19 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
蓝田~20 分钟前
观察者模式和订阅模式
windows·观察者模式
sanguine__23 分钟前
java学习-集合
学习
lxlyhwl23 分钟前
【STK学习】part2-星座-目标可见性与覆盖性分析
学习
nbsaas-boot24 分钟前
如何利用ChatGPT加速开发与学习:以BPMN编辑器为例
学习·chatgpt·编辑器
CV学术叫叫兽1 小时前
一站式学习:害虫识别与分类图像分割
学习·分类·数据挖掘
我们的五年1 小时前
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
linux·c++·学习
一棵开花的树,枝芽无限靠近你2 小时前
【PPTist】添加PPT模版
前端·学习·编辑器·html
VertexGeek2 小时前
Rust学习(八):异常处理和宏编程:
学习·算法·rust
二进制_博客3 小时前
Flink学习连载文章4-flink中的各种转换操作
大数据·学习·flink