1 / 23

Linux 系统应用与程序设计

Linux 系统应用与程序设计. 主讲:邝颖杰 电邮: kuangyingjie@163.com. gtk-demo pkg-config. GTK+ 程序示例. 登录程序 按钮. #include <gtk/gtk.h> #include <stdio.h> #include <string.h> const char * password="secret"; void closeApp(GtkWidget *window,gpointer data){ gtk_main_quit(); }

maddox
Download Presentation

Linux 系统应用与程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux 系统应用与程序设计 主讲:邝颖杰 电邮:kuangyingjie@163.com

  2. gtk-demo • pkg-config

  3. GTK+程序示例 • 登录程序 • 按钮

  4. #include <gtk/gtk.h> #include <stdio.h> #include <string.h> const char * password="secret"; void closeApp(GtkWidget *window,gpointer data){ gtk_main_quit(); } void button_clicked(GtkWidget *window,gpointer data){ const char *password_text=gtk_entry_get_text(GTK_ENTRY((GtkWidget *)data)); if (strcmp(password_text,password)==0) printf("Access granted!\n"); else printf("Access denied!\n"); }

  5. int main(int argc,char *argv[]){ GtkWidget *window; GtkWidget *username_label,*password_label; GtkWidget *username_entry,*password_entry; GtkWidget *ok_button; GtkWidget *hbox1,*hbox2; GtkWidget *vbox; gtk_init(&argc,&argv); window=gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),"GtkEntryBox"); gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window),200,200); g_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(closeApp),NULL);

  6. username_label=gtk_label_new("Login:"); password_label=gtk_label_new("Password:"); username_entry=gtk_entry_new(); password_entry=gtk_entry_new(); gtk_entry_set_visibility(GTK_ENTRY(password_entry),FALSE); ok_button=gtk_button_new_with_label("OK"); g_signal_connect(GTK_OBJECT(ok_button),"clicked",GTK_SIGNAL_FUNC(button_clicked),password_entry); hbox1=gtk_hbox_new(TRUE,5); hbox2=gtk_hbox_new(TRUE,5); vbox=gtk_vbox_new(FALSE,10); gtk_box_pack_start(GTK_BOX(hbox1),username_label,TRUE,FALSE,5); gtk_box_pack_start(GTK_BOX(hbox1),username_entry,TRUE,FALSE,5); gtk_box_pack_start(GTK_BOX(hbox2),password_label,TRUE,FALSE,5); gtk_box_pack_start(GTK_BOX(hbox2),password_entry,TRUE,FALSE,5); gtk_box_pack_start(GTK_BOX(vbox),hbox1,FALSE,FALSE,5); gtk_box_pack_start(GTK_BOX(vbox),hbox2,FALSE,FALSE,5); gtk_box_pack_start(GTK_BOX(vbox),ok_button,FALSE,FALSE,5); gtk_container_add(GTK_CONTAINER(window),vbox); gtk_widget_show_all(window); gtk_main(); return 0; }

  7. GtkButton • GtkButton • GtkToggleButton • GtkCheckButton • GtkRadioButton

  8. #include <gtk/gtk.h> • #include <stdio.h> • GtkWidget *togglebutton; • GtkWidget *checkbutton; • GtkWidget *radiobutton1,*radiobutton2; • void closeApp(GtkWidget *window,gpointer data) • { • gtk_main_quit(); • } • void add_widget_with_label(GtkContainer *box,gchar *caption,GtkWidget *widget) • { • GtkWidget *label=gtk_label_new(caption); • GtkWidget *hbox=gtk_hbox_new(TRUE,4); • gtk_container_add(GTK_CONTAINER(hbox),label); • gtk_container_add(GTK_CONTAINER(hbox),widget); • gtk_container_add(box,hbox); • }

  9. void print_active(char *button_name,GtkToggleButton *button) • { • gboolean active=gtk_toggle_button_get_active(button); • printf("%s is %s\n",button_name,active?"active":"not active"); • } • void button_clicked(GtkWidget *button,gpointer data) • { • print_active("Checkbutton",GTK_TOGGLE_BUTTON(checkbutton); • print_active("Togglebutton",GTK_TOGGLE_BUTTON(togglebutton); • print_active("Radiobutton1",GTK_TOGGLE_BUTTON(radiobutton1); • print_active("Radiobutton2",GTK_TOGGLE_BUTTON(radiobutton2); • printf("\n"); • }

  10. gint main(gint argc,gchar *argv[]) • { • GtkWidget *window; • GtkWidget *button; • GtkWidget *vbox; • gtk_init(&argc,&argv); • window=gtk_window_new(GTK_WINDOW_TOPLEVEL); • gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); • gtk_window_set_default_size(GTK_WINDOW(window),200,200); • g_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(closeApp),NULL); • button=gtk_button_new_with_label("OK"); • togglebutton=gtk_toggle_button_new_with_label("Toggle"); • checkbutton=gtk_check_button_new(); • radiobutton1=gtk_radio_button_new(NULL); • radiobutton2=gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(radiobutton1));

  11. vbox=gtk_vbox_new(TRUE,4); • add_widget_with_label(GTK_CONTAINER(vbox),"ToggleButton:",togglebutton); • add_widget_with_label(GTK_CONTAINER(vbox),"CheckButton:",checkbutton); • add_widget_with_label(GTK_CONTAINER(vbox),"Radio 1:",radiobutton1); • add_widget_with_label(GTK_CONTAINER(vbox),"Radio 2:",radiobutton2); • add_widget_with_label(GTK_CONTAINER(vbox),"Button:",button); • g_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(button_clicked),NULL); • gtk_container_add(GTK_CONTAINER(window),vbox); • gtk_widget_show_all(window); • gtk_main(); • return 0; • }

  12. 程序的参数处理 • Shell脚本程序使用$0,$1接收参数 • C程序: • int main(int argc,char *argv[]) • argc是程序参数的个数,argv是代表参数自身的字符串数组 • 如果不声明argc,argv,就不能使用

  13. Shell接收用户输入的命令行,将命令行分解成单词,然后把单词放入argv数组Shell接收用户输入的命令行,将命令行分解成单词,然后把单词放入argv数组 • 如 myapp left right ‘and center’ • argc:4 • argv:{“myapp”,”left”,”right”,”and center”} • 参数个数包括程序自身名称

  14. 命令行选项 • ls –l –s –t –r • df –m • 命令行开关都应以一个短横线开头,包含单个字母或数字

  15. C程序处理命令行选项 • 演示

  16. 利用getopt函数处理命令行选项 • getopt是linux的函数,它支持需要关联值和不需要关联值的选项 • Int getopt(int argc,char *const argv[],const char *optstring) • *optstring是选项指定字符串,告诉getopt哪些选项可用,以及每个选项是否有关联值 • optstring每个字符代表一个单字符选项,如果一个字符后面紧跟一个冒号,则表明选项有关联值

  17. getopt (argc ,argv,”if:lr”) • 该函数返回值是argv数组的下一个选项字符 • 如果选项有关联值,则外部变量optarg指向这个值 • 如果选项处理完毕,getopt返回-1,特殊参数- -使函数停止 • 如果遇到无法识别的选项,返回一个?号,并把它保存在外部变量optopt中 • 如果选项要求关联值,但未提供该值,则返回:

  18. 程序参数 • 演示

  19. 时间和日期 • 所有的unix系统都使用同一个时间和日期的起点:格林尼治时间(GMT)1970年1月1日0点,这是unix纪元的起点,linux也不例外 • Linux系统中所有的时间都以从那时起经过的秒数来衡量 • MS-DOS也是类似,只是它的纪元始于1980年

  20. 时间通过一个预定义的类型time_t处理,是长整数时间通过一个预定义的类型time_t处理,是长整数 • #include <time.h> • time_t time(time_t *tloc)

  21. #include <time.h> • #include <stdio.h> • #include <unistd.h> • int main() • { • int i; • time_t thetime; • for (i=1;i<=10;i++) • { • thetime=time((time_t *)0); • printf("the time is %ld \n",thetime); • sleep(2); • } • exit(0); • }

  22. gmtime函数 • struct tm *gmtime(const time_t timeval) • tm的结构 • Int tm_sec • Int tm_min • Int tm_hour • Int tm_mday • Int tm_mon • Int tm_year • Int tm_wday 星期几 • Int tm_yday • Int tm_isdst 是否夏令时

  23. #include <time.h> • #include <stdio.h> • int main() • { • struct tm *tm_ptr; • time_t thetime; • (void)time(&thetime); • tm_ptr=gmtime(&thetime); • printf("raw time is %ld \n",thetime); • printf("gmtime gives:\n"); • printf("date: %02d/%02d/%02d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday); • printf("time: %02d:%02d:%02d\n",tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec); • exit(0); • }

More Related