一派護法 十九級 |
【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; }
|