Gtk+-3.0实例1.带点击事件的简单按钮实现-创新互联

1. 实现说明
  • 实现环境window10+msys2+gtk3+cb20.03+gcc12
  • 源码来自于《GTK2编程范例》1.3节的Hello程序
  • 解决了源代码中计数器不能自动加1的问题
  • 解决了中文乱码的问题,增加了gbk2utf8编码转换函数
2. C源代码
#include//中文字符串显示时的编码转换:GBK-to-UTF-8
char* gbk2utf8(char* c){return (g_locale_to_utf8(c,-1,0,0,0));
}

gint count=0;

void on_button_clicked(GtkWidget *bt,gpointer data){g_print(gbk2utf8("Button点击了"));
    g_print(" %d ",count);
    g_print(gbk2utf8("次!\n"));
    count += 1;
}

int main(int argc, char *argv[]) {GtkWidget *window;
  GtkWidget *button;

  gtk_init(&argc, &argv);

  g_print("<----About GTK+-3.0---->\n");
  g_print("  GTK+ version: %d.%d.%d\n", gtk_major_version,
        gtk_minor_version, gtk_micro_version);
  g_print("  Glib version: %d.%d.%d\n", glib_major_version,
        glib_minor_version, glib_micro_version);
  g_print("<---------------------->\n");

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), gbk2utf8("按钮示例"));
  gtk_window_set_default_size(GTK_WINDOW(window), 260, 50);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width(GTK_CONTAINER(window),10);
  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

  button = gtk_button_new_with_label(gbk2utf8("点我试试!"));
  g_signal_connect(G_OBJECT(button),"clicked", G_CALLBACK(on_button_clicked),NULL);

  gtk_container_add(GTK_CONTAINER(window),button);

  gtk_widget_show(button);
  gtk_widget_show(window);

  gtk_main();

  return 0;
}
3. 运行结果

在这里插入图片描述

目前累计服务客户超过千家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供网站设计、成都做网站、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。创新互联公司始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。4. C++版

函数gbk2utf8的类型和参数类型与C版有所不同,主要由char *改为了char const *,原因是 ISO C++ 标准禁止a string constantchar*的转换 !运行结果如上图。

#include#includeusing namespace std;

//中文字符串显示时的编码转换:GBK-to-UTF-8
char const * gbk2utf8(char const * c){return (g_locale_to_utf8(c,-1,0,0,0));
}

gint count=0;

void on_button_clicked(   ){g_print(gbk2utf8("Button点击了"));
    g_print(" %d ",count);
    g_print(gbk2utf8("次!\n"));

    count += 1;
}

int main(int argc, char *argv[]) {GtkWidget *window;
  GtkWidget *button;

  gtk_init(&argc, &argv);

  cout<< "<----About GTK+-3.0---->\n";
  cout<< "  GTK+ version: "<< gtk_major_version<< "."<< gtk_minor_version<< "."<< gtk_micro_version<\n";

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), gbk2utf8("按钮示例"));
  gtk_window_set_default_size(GTK_WINDOW(window), 260, 50);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width(GTK_CONTAINER(window),10);
  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

  button = gtk_button_new_with_label(gbk2utf8("点我试试!"));
  g_signal_connect(G_OBJECT(button),"clicked", G_CALLBACK(on_button_clicked),NULL);

  gtk_container_add(GTK_CONTAINER(window),button);
  gtk_widget_show(button);
  gtk_widget_show(window);

  gtk_main();

  return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


标题名称:Gtk+-3.0实例1.带点击事件的简单按钮实现-创新互联
标题网址:http://scyanting.com/article/djieeo.html