我一直在到处寻找,以了解如何使用转义键关闭我的程序。我认为没有多少人使用 GTK+。另外,如果要求不高,能否请一些人告诉我如何使用 Gnome 开发者网站,以便我自己找到这些东西?谢谢。所以,用转义键关闭窗口,这是我的代码:
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <ctype.h>
#include <curses.h>
#define KEY_ESC '\033'
static GtkWidget *entry;
static gboolean kill_window(GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_main_quit();
return FALSE;
}
static void button_press(GtkWidget *widget, gpointer data)
{
const char *text = gtk_entry_get_text(GTK_ENTRY(entry));
//system("cd" text);
//printf("%s\n", text);
const char *text2 = "&";
char *concatenation;
concatenation = malloc(strlen(text)+2);
strcpy(concatenation, text);
strcat(concatenation, text2);
system(concatenation);
gtk_main_quit();
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *button1;
GtkWidget *hbox;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label("Run");
button1 = gtk_button_new_with_label("Cancel");
entry = gtk_entry_new();
hbox = gtk_vbox_new(FALSE, 2);
gtk_window_set_title(GTK_WINDOW(window), "Run..");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
g_signal_connect(window, "delete_event", G_CALLBACK(kill_window), NULL);
g_signal_connect(button, "clicked", G_CALLBACK(button_press), NULL);
g_signal_connect(button1, "clicked", G_CALLBACK(kill_window), NULL);
g_signal_connect(entry, "activate", G_CALLBACK(button_press), NULL);
g_signal_connect(button1, "delete_event", G_CALLBACK(kill_window), NULL);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_window_set_default_size(GTK_WINDOW(window), 250, 100);
gtk_window_set_decorated(GTK_WINDOW(window), TRUE);
gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(hbox), button1, FALSE, FALSE, 2);
gtk_container_add(GTK_CONTAINER(window), hbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}