The Exit App (C)
Linux tutorials and code.
4 posts
Page 1 of 1
NOTE: Make sure to do all your programming in a text editor, and NOT a Word Processor!!!
In this tutorial, I showed you guys how you can create a simple window in C. Well today all we're going to do is add a button to that window.
The Coding:
All the code below does is open up a window, with the title "Exit App" for the programs name, with of 200px, and height of 10px, the app is positioned via centered.
Now lets add the following codes under gtk_container_set_border_width (GTK_CONTAINER (window), 10);.
The line in the middle tells us that when we click the button it's gonna close the app.
and
The line on the bottom tells us the button will be visible.
Now make sure to add the following code before gtk_widget_show (window);
Now our full code for our program should look like this.
Compile:
Now we need to open our Terminal, and navigate to the desktop directory.
In this tutorial, I showed you guys how you can create a simple window in C. Well today all we're going to do is add a button to that window.
The Coding:
All the code below does is open up a window, with the title "Exit App" for the programs name, with of 200px, and height of 10px, the app is positioned via centered.
Code: Select all
So now we need to declare a call for our button so under GtkWidget *window; put down GtkWidget *button;. Pretty simple huh?, but we're not done yet.#include <gtk/gtk.h>
int main (int argc, char **argv) {
GtkWidget *window;
gtk_init (&argc, &argv);
//////The Window//////
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Exit App");
g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
gtk_window_resize(GTK_WINDOW (window),200,10);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show (window);
gtk_main ();
return 0;
}
Now lets add the following codes under gtk_container_set_border_width (GTK_CONTAINER (window), 10);.
Code: Select all
The line on the top tells us the button's gonna say "Exit!".button = gtk_button_new_with_label ("Exit!");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (window), button);
The line in the middle tells us that when we click the button it's gonna close the app.
and
The line on the bottom tells us the button will be visible.
Now make sure to add the following code before gtk_widget_show (window);
Code: Select all
NOTE: If you wanna give the whole window a border, you'd put the following code below, gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);.
gtk_widget_show (button);
Code: Select all
However we're not going to be adding a border in our program, I just thought I'd mention that encase someone would like to.gtk_container_set_border_width (GTK_CONTAINER (window), 10);
Now our full code for our program should look like this.
Code: Select all
Now lets save this file to our desktop as exitapp.c.#include <gtk/gtk.h>
int main (int argc, char **argv) {
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
//////The Window//////
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Exit App");
g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
gtk_window_resize(GTK_WINDOW (window),200,10);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
//////The Button//////
button = gtk_button_new_with_label ("Exit!");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
Compile:
Now we need to open our Terminal, and navigate to the desktop directory.
Code: Select all
Now compile the program we just created with the following code.
cd Desktop
Code: Select all
You should get no errors, and your application should look like this.
You can download the app below as well as the source, if you wanna take a look, try it out, or may need further help.gcc -Wall -g -o Exit exitapp.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
You do not have the required permissions to view the files attached to this post.
Last edited by mikethedj4 on Sat Jul 16, 2011 6:08 pm, edited 2 times in total.
Hey Mike,
I wrote this in CMD:
Probable because I use windows, but I don't know :?
I wrote this in CMD:
Code: Select all
And I just get 'gcc' is not recognized as an internal or external command, operable program or batch file.gcc -Wall -g -o Exit exitapp.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
Probable because I use windows, but I don't know :?
You're crazy!
I'm not crazy, my mother had me tested. ~Sheldon Cooper
I'm not crazy, my mother had me tested. ~Sheldon Cooper
Did you run the command from the directory where the GCC compiler is installed?
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023