当前位置: 技术问答>linux和unix
GTK编程中文显示的问题
来源: 互联网 发布时间:2015-10-01
本文导语: 程序很简单,只是要它显示中文的"关闭窗口"四个字 #include #include int main (int argc, char *argv[]) { GtkWidget *window; GtkWidget *vbox; GtkWidget *entry; GtkWidget *text; GtkWidget *button; gtk_set_locale(); gtk_rc_add_defa...
程序很简单,只是要它显示中文的"关闭窗口"四个字
#include
#include
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *entry;
GtkWidget *text;
GtkWidget *button;
gtk_set_locale();
gtk_rc_add_default_file("./gtkrc.zh");
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
// gtk_widget_set_usize( GTK_WIDGET (window), 200, 500);
gtk_window_set_title(GTK_WINDOW (window), "GTK Entry");
gtk_signal_connect(GTK_OBJECT (window), "delete_event", (GtkSignalFunc) gtk_exit, NULL);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
entry = gtk_entry_new_with_max_length (60);
gtk_entry_select_region (GTK_ENTRY (entry), 0, GTK_ENTRY(entry)->text_length);
gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
gtk_widget_show (entry);
text = gtk_text_new (NULL, NULL); gtk_text_set_editable (GTK_TEXT (text), TRUE);
gtk_box_pack_start (GTK_BOX (vbox), text, TRUE, TRUE, 0);
gtk_widget_show(text);
button = gtk_button_new_with_label ("关闭窗口");
gtk_signal_connect_object (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC(gtk_exit), GTK_OBJECT (window));
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
gtk_widget_grab_default (button);
gtk_widget_show (button);
gtk_widget_show(window);
gtk_main();
return(0);
}
该目录下的gtkrc.zh内容为
style "gtk-default-zh-cn" {
fontset = "-adobe-helvetica-medium-r-normal--14-*-*-*-*-*-iso8859-1,
-misc-simsun-medium-r-normal--14-*-*-*-*-*-gbk-0,*-r-*"
}
class "GtkWidget" style "gtk-default-zh-cn"
可是显示的是乱码,请各位指点一下你们是怎么实现中文显示的.我看了网上的一下资料,修改了好多次gtkrc.zh,都是有警告,显示的都是乱码.
谢谢各位了,都搞了好几天了.还是没有搞定!
#include
#include
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *entry;
GtkWidget *text;
GtkWidget *button;
gtk_set_locale();
gtk_rc_add_default_file("./gtkrc.zh");
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
// gtk_widget_set_usize( GTK_WIDGET (window), 200, 500);
gtk_window_set_title(GTK_WINDOW (window), "GTK Entry");
gtk_signal_connect(GTK_OBJECT (window), "delete_event", (GtkSignalFunc) gtk_exit, NULL);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
entry = gtk_entry_new_with_max_length (60);
gtk_entry_select_region (GTK_ENTRY (entry), 0, GTK_ENTRY(entry)->text_length);
gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
gtk_widget_show (entry);
text = gtk_text_new (NULL, NULL); gtk_text_set_editable (GTK_TEXT (text), TRUE);
gtk_box_pack_start (GTK_BOX (vbox), text, TRUE, TRUE, 0);
gtk_widget_show(text);
button = gtk_button_new_with_label ("关闭窗口");
gtk_signal_connect_object (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC(gtk_exit), GTK_OBJECT (window));
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
gtk_widget_grab_default (button);
gtk_widget_show (button);
gtk_widget_show(window);
gtk_main();
return(0);
}
该目录下的gtkrc.zh内容为
style "gtk-default-zh-cn" {
fontset = "-adobe-helvetica-medium-r-normal--14-*-*-*-*-*-iso8859-1,
-misc-simsun-medium-r-normal--14-*-*-*-*-*-gbk-0,*-r-*"
}
class "GtkWidget" style "gtk-default-zh-cn"
可是显示的是乱码,请各位指点一下你们是怎么实现中文显示的.我看了网上的一下资料,修改了好多次gtkrc.zh,都是有警告,显示的都是乱码.
谢谢各位了,都搞了好几天了.还是没有搞定!
|
用 g_locale_to_utf8 线转换为UTF-8码 例如:
gtk_button_new_with_label(g_locale_to_utf8("关闭窗口",-1,NULL,NULL,NULL));
gtk_button_new_with_label(g_locale_to_utf8("关闭窗口",-1,NULL,NULL,NULL));
|
试试看button = gtk_button_new_with_label (_("关闭窗口"));