QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1034|回复: 1

Gtk+/Win32中文显示方案

[复制链接]
发表于 2004-3-17 15:31:35 | 显示全部楼层 |阅读模式
昨夜看到有人发贴问在windows下编译的gtk程序无法显示中文,该如何解决。──如果我没记错的话──可我现在实在找不到昨天看到的那个贴子了,呵呵,只好贴到这里。各位朋友知道那个贴子的给转一下,免得人家找不到答案苦恼懊悔郁闷。
今天偶然浏览到的:
[code:1]    Gtk+在UNIX平台下中文显示很正常,但同样的程序在GTK+/WIN32下汉字无法显示,原因为传入GTK+的字符串不能为GB2312码,必须全部为UTF-8码,否则无法正常显示。而UTF-8码在WINDOWS上无法正常输入,必须通过转换程序。
    libiconv就可以实现这样的功能,从网上下载编译后,可以生成iconv.exe,执行 iconv -f EUC-CN -t UTF-8 srcfile >desfile
就可以将文件中的GB码转化为UTF-8码。
    如果需要在程序中自动转换,则必须调用libiconv系列函数。同时GTK+中必须使用中文字体,才能正确显示中文。例程如下:

#include <locale.h>
#include <gtk/gtk.h>
static char *gtkrc_string = "\
        style \"default\"\
        {\
                fontset = \"-unknown-榛戜綋-normal-r-normal-*-*-140-*-*-p-*-
gb2312.1980-0\"\
        }\
        class \"GtkWidget\" style \"default\"";
/* This is a callback function. The data arguments are ignored
* in this example. More on callbacks below. */
void hello( GtkWidget *widget,
            gpointer   data )
{
    g_print ("Hello World\n");
}
gint delete_event( GtkWidget *widget,
                   GdkEvent  *event,
     gpointer   data )
{
    /* If you return FALSE in the "delete_event" signal handler,
     * GTK will emit the "destroy" signal. Returning TRUE means
     * you don't want the window to be destroyed.
     * This is useful for popping up 'are you sure you want to quit?'
     * type dialogs. */
    g_print ("delete event occurred\n");
    /* Change TRUE to FALSE and the main window will be destroyed with
     * a "delete_event". */
    return(TRUE);
}
/* Another callback */
void destroy( GtkWidget *widget,
              gpointer   data )
{
    gtk_main_quit();
}
int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *button;
    /* This is called in all GTK applications. Arguments are parsed
     * from the command line and are returned to the application. */
gtk_set_locale();
    gtk_init(&argc, &argv);
gtk_rc_parse_string(gtkrc_string);
    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    /* When the window is given the "delete_event" signal (this is given
     * by the window manager, usually by the "close" option, or on the
     * titlebar), we ask it to call the delete_event () function
     * as defined above. The data passed to the callback
     * function is NULL and is ignored in the callback function. */
    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
   GTK_SIGNAL_FUNC (delete_event), NULL);
    /* Here we connect the "destroy" event to a signal handler.
     * This event occurs when we call gtk_widget_destroy() on the window,
     * or if we return FALSE in the "delete_event" callback. */
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
   GTK_SIGNAL_FUNC (destroy), NULL);
    /* Sets the border width of the window. */
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    /* Creates a new button with the label "Hello World". */
    button = gtk_button_new_with_label ("浣犲ソ锛屼笘鐣岋紒 Hello World!");
    /* When the button receives the "clicked" signal, it will call the
     * function hello() passing it NULL as its argument.  The hello()
     * function is defined above. */
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
   GTK_SIGNAL_FUNC (hello), NULL);
    /* This will cause the window to be destroyed by calling
     * gtk_widget_destroy(window) when "clicked".  Again, the destroy
     * signal could come from here, or the window manager. */
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
          GTK_SIGNAL_FUNC (gtk_widget_destroy),

          GTK_OBJECT (window));
    /* This packs the button into the window (a gtk container). */
    gtk_container_add (GTK_CONTAINER (window), button);
    /* The final step is to display this newly created widget. */
    gtk_widget_show (button);
    /* and the window */
    gtk_widget_show (window);
    /* All GTK applications must have a gtk_main(). Control ends here
     * and waits for an event to occur (like a key press or
     * mouse event). */
    gtk_main ();
    return(0);
}
/* example-end */

[/code:1]
发表于 2004-3-17 19:55:36 | 显示全部楼层
谢谢!
小弟昨晚问的问题。最近不方便用linux,就开始在window下试试gtk,

谢谢楼主的热心肠!

用g_locale_to_utf8也可以,也是在linuxforum.net那里看到的,只是奇怪,window_title可以显示中文,而button_label就不可以,只是显示出“口口”。

不知道windows的gtkruntime中是怎么设置字体的。把c:\Program Files\Common Files\GTK\2.0\etc\gtk-2.0目录下的所有文件中字体设置都改成了"宋体 8"可还是没办法解决~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-8 18:51 , Processed in 0.054180 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表