目前共有7篇帖子。 內容轉換:不轉換▼
 
點擊 回復
527 6
【程序】GTK+中的图形绘制与文本框操作
一派護法 十九級
1樓 發表于:2016-8-26 15:28
一派護法 十九級
2樓 發表于:2016-8-26 15:29
【main.c】
#include <gtk/gtk.h>
#include <string.h>

gboolean flag = FALSE; // 是否禁止重绘
GObject *label;

void change_title(GtkWindow *window)
{
    char str[20];
    static int n = 1;
    sprintf(str, "Title Changed (%d)", n);
    gtk_window_set_title(window, str); // 更改窗口标题
    n++;
}

void no_draw(GtkWidget *widget, gpointer data)
{
    flag = !flag;
    //gtk_button_set_label(GTK_BUTTON(widget), (flag) ? "Not Drawing" : "Drawing"); // 更改按钮文字
    gtk_window_set_title(GTK_WINDOW(data), (flag) ? "Not Drawing" : "Drawing");
}

gboolean draw(GtkWidget *widget, cairo_t *cr, gpointer data)
{
    // 获取绘图区域尺寸
    guint width = gtk_widget_get_allocated_width(widget);
    guint height = gtk_widget_get_allocated_height(widget);
    if (flag)
        return FALSE;
   
    // 将整个区域填充为白色
    cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); // 白色
    cairo_rectangle(cr, 0, 0, width, height);
    cairo_fill(cr);

    // 画线
    cairo_set_source_rgb(cr, 1.0, 0.0, 0.0); // 红色
    cairo_set_line_width(cr, 0.5);
    cairo_move_to(cr, 10, 10);
    cairo_line_to(cr, 140, 90);
    cairo_stroke(cr);
   
    cairo_set_source_rgb(cr, 0.0, 1.0, 0.0); // 绿色
    cairo_move_to(cr, 140, 90); // 不能省略!
    cairo_line_to(cr, 100, 10);
    cairo_line_to(cr, 10, 10);
    cairo_stroke(cr);
   
    // 画矩形
    cairo_set_source_rgb(cr, 0.0, 0.0, 1.0); // 蓝色
    cairo_rectangle(cr, 10, 60, 30, 30); // 后两个参数为宽度和高度,而非终点坐标
    cairo_fill(cr);
    return FALSE;
}

void change_text(GtkEntry *entry)
{
    char str[40];
    static int i = -1;
    GtkWidget *parent_window = gtk_widget_get_toplevel(GTK_WIDGET(entry)); // 获取文本框的父窗口
    i++;
    sprintf(str, "i=%d", i);
   
    if (i != 0 && i % 10 == 0)
    {
        gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE); // 将文本框设为只读
        strcat(str, ", not editable");
        gtk_label_set_text(GTK_LABEL(label), "Label"); // 更改标签文本
        //gtk_window_get_widget();
    }
    else
    {
        gtk_editable_set_editable(GTK_EDITABLE(entry), TRUE);
        gtk_label_set_text(GTK_LABEL(label), "Text Changed");
    }
    gtk_entry_set_text(entry, str); // 更改文本框内容
    gtk_window_set_focus(GTK_WINDOW(parent_window), GTK_WIDGET(entry)); // 将光标移动到文本框上
}

int main(int argc, char *argv[])
{
    GtkBuilder *builder;
    GObject *window, *button, *drawing_area, *text_entry;
   
    gtk_init(&argc, &argv);
    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "builder.ui", NULL);
   
    window = gtk_builder_get_object(builder, "window");
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
   
    button = gtk_builder_get_object(builder, "button1");
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(change_title), window);
   
    button = gtk_builder_get_object(builder, "button2");
    g_signal_connect(button, "clicked", G_CALLBACK(no_draw), window);
   
    button = gtk_builder_get_object(builder, "quit");
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
   
    drawing_area = gtk_builder_get_object(builder, "drawing");
    g_signal_connect(drawing_area, "draw", G_CALLBACK(draw), NULL);
   
    text_entry = gtk_builder_get_object(builder, "text");
    button = gtk_builder_get_object(builder, "btn");
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(change_text), text_entry);
   
    label = gtk_builder_get_object(builder, "label");
   
    gtk_main();
    return 0;
}
一派護法 十九級
3樓 發表于:2016-8-26 15:29
【builder.ui】
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <object class="GtkWindow" id="window">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="border_width">10</property>
    <property name="title">Grid Window</property>
    <property name="default_width">640</property>
    <property name="default_height">480</property>
    <child>
      <object class="GtkGrid" id="grid">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="halign">center</property>
        <property name="valign">center</property>
        <property name="row_spacing">10</property>
        <property name="column_spacing">10</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label">Button 1</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label">Button 2</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="quit">
            <property name="label">Quit</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkDrawingArea" id="drawing">
            <property name="width_request">200</property>
            <property name="height_request">100</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">2</property>
            <property name="width">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="text">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="text" translatable="yes">Default</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">3</property>
            <property name="width">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="btn">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">4</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="label" translatable="yes">Label</property>
            <property name="selectable">True</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">4</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
一派護法 十九級
4樓 發表于:2016-8-26 15:29
【Makefile】
main: main.c builder.ui
     gcc `pkg-config --cflags gtk+-3.0` main.c -o main `pkg-config --libs gtk+-3.0`
一派護法 十九級
5樓 發表于:2016-8-26 15:40
【显示文字】
cairo_select_font_face(cr, "Source Han Sans CN Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); // 该字体支持汉字
cairo_set_source_rgb(cr, 1.0, 0.0, 1.0); // 字体颜色
cairo_set_font_size(cr, 14); // 字体大小
cairo_move_to(cr, 52, 40);
cairo_show_text(cr, "简体中文ABC");
一派護法 十九級
6樓 發表于:2016-8-26 15:48
粗体字:CAIRO_FONT_WEIGHT_BOLD(参数4)
斜体字:CAIRO_FONT_SLANT_ITALIC(参数3)

Source Han Sans CN Regular字体支持粗体字,但不支持斜体字。
serif字体粗体斜体都支持,但是不能显示中文。

例如:
cairo_select_font_face(cr, "serif", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_source_rgb(cr, 1.0, 0.0, 1.0);
cairo_set_font_size(cr, 14);
cairo_move_to(cr, 52, 40);
cairo_show_text(cr, "English Only");
一派護法 十九級
7樓 發表于:2016-8-26 15:53
合在一起:
// 显示文字
cairo_select_font_face(cr, "serif", CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_source_rgb(cr, 1.0, 0.0, 1.0); // 字体颜色
cairo_set_font_size(cr, 14); // 字体大小
cairo_move_to(cr, 52, 40);
cairo_show_text(cr, "English Only");

cairo_select_font_face(cr, "Source Han Sans CN Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); // 该字体支持汉字
cairo_set_source_rgb(cr, 0.5, 0.4, 0.3);
cairo_move_to(cr, 82, 20);
cairo_show_text(cr, "简体中文ABC");

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:527 回複數:6
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-8-26 15:53
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用共享創意姓名標示-相同方式分享 3.0 Unported許可協議進行許可。