GTK+ 2.x初学者指南.第一章

GTK+ 2.x Tutorial for Beginners

GTK+ 2.x 初学者指南

作者: Jan Bodnar

原文出自: http://zetcode.com/tutorials/gtktutorial/firstprograms/   

 

This tutorial is for beginners describing some of the most common widgets written by Jan Bodnar.

Jan Bodnar 编写的这本初学者指南描述了一些常用的构件。

First programs in GTK+

第一个 GTK+ 程序

In this part of the GTK+ programming tutorial, we will create our first programs in GTK+

在《 GTK 初学者指南》的本章中,我将使用 GTK+ 开发我们的第一个程序。

Simple example

简单例子

We start with a very simple example. We will show a basic window.

我们由一个非常简单的例子开始,此例显示出一个窗体。

#include <gtk/gtk.h>

int main( int argc, char *argv[])

{

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_widget_show(window);

  gtk_main();

  return 0;

}

This example will show a basic window on screen.

这个例子将在屏幕上显示出一个基本窗体。

  gcc -o simple simple.c `pkg-config --libs --cflags gtk+-2.0`

This is how we compile the example.

使用上面这个命令行来编译前面的例子。

  gtk_init(&argc, &argv);

Here we initiate the GTK+ library.

初始化 GTK+ 库。

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

We create a GtkWindow widget. The window type is GTK_WINDOW_TOPLEVEL . Toplevel windows have a titlebar and a border. They are managed by the window manager.

我们创建一个 GtkWindow 构件,窗体的类型是 GTK_WINDOW_TOPLEVEL 顶层窗体中附带一个标题栏和一个边框,由窗口管理器负责管理

  gtk_widget_show(window);

After we have created a widget, we must show it.

当创建完一个构件后,应将它显示出来。

  gtk_main();

This code enters the GTK+ main loop. From this point, the application sits and waits for events to happen.

这个代码的作用是使程序进入 GTK+ 主循环。由此看出,程序将停止并等待即将发生的事件。

 

Figure: Simple

 

Centering the window

置中窗体

If we do not position the window ourselves, the window manager will position it for us. In the next example, we will center the window.

如果我们不给窗体设置位置,窗口管理器会为窗体进行置位处理。在接下来的例子中,我们将置中一个窗体。

#include <gtk/gtk.h>

int main( int argc, char *argv[])

{

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title(GTK_WINDOW(window), "Center");

  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

  gtk_widget_show(window);

 

  g_signal_connect_swapped(G_OBJECT(window), "destroy",

      G_CALLBACK(gtk_main_quit), NULL);

  gtk_main();

  return 0;

}

In our example, we center the window, set a title and size for the window.

在这个例子中,我们置中了一个窗体,并为其设置标题和窗体大小。

  gtk_window_set_title(GTK_WINDOW(window), "Center");

The gtk_window_set_title() function will set a window title. If we do not set a title ourselves, the GTK+ will use a name of a source file as a title.

gtk_window_set_title() 函数用于给窗体设置标题。如果没给窗体设置标题, GTK+ 会使用源程序名作为窗体标题。

  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);

This code sets the size of the window to 230x150 pixels. Note, that we are talking about the client area, excluding the decorations provided by the window manager.

这句代码的作用是将窗体的大小设置为 230x150 像素,也就是我们说的窗体客户区,包括窗口管理器提供的装饰构件。

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

This code centers the window.

这句代码是将创建的窗体置于屏幕的中间位置。

  g_signal_connect_swapped(G_OBJECT(window), "destroy",

     G_CALLBACK(gtk_main_quit), NULL);

In the previous example, the window was not completely destroyed, when we clicked on the x button. We can see it, if we lauch the example from the command line. The window does not react to the destroy signal by default. We must explicitely terminate the application by connecting the destroy signal to the gtk_main_quit() function.

在当前例子中,当我们单击关闭按钮时,窗体不能被完全销毁。如果我们从命令行启动这个程序,就能看见窗体。但窗体并不响应默认的销毁信号,我们必须明确地使用 gtk_main_quit() 函数接管 destroy 信号来终止应用程序。

The application icon

应用程序图标

In the next example, we show the application icon. Most window managers display the icon in the left corner of the titlebar and also on the taskbar.

在下面这个例子中,我们将显示应用程序图标。大多数窗口管理器在标题栏的左角处显示图标,也可在任务栏上显示。

#include <gtk/gtk.h>

GdkPixbuf *create_pixbuf(const gchar * filename)

{

   GdkPixbuf *pixbuf;

   GError *error = NULL;

   pixbuf = gdk_pixbuf_new_from_file(filename, &error);

   if(!pixbuf) {

      fprintf(stderr, "%s/n", error->message);

      g_error_free(error);

   }

 

   return pixbuf;

}

 

int main( int argc, char *argv[])

{

  GtkWidget *window;

  gtk_init(&argc, &argv);

 

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title(GTK_WINDOW(window), "icon");

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值