C語言實(shí)現(xiàn)生成新春福字的示例詳解
更新時(shí)間:2022年01月24日 08:27:20 作者:從善若水
這篇文章主要介紹了如何利用C語言實(shí)現(xiàn)生成各個(gè)字體的新春福字,再也不用擔(dān)心支付寶掃福找不到圖片了,感興趣的同學(xué)可以跟隨小編學(xué)習(xí)一下
快新年了,支付寶掃?;顒?dòng)又開始了,每次都要百度找福,這次不想找了,自己寫一個(gè)程序生成各種字體的福字。
主要代碼
字面量以及數(shù)據(jù)結(jié)構(gòu)
#define FONT_DISPLAY "福" // g_fu_label中的每一個(gè)控件都是一個(gè)福字 static GtkWidget *g_fu_label[3][3]; // 記錄所有的字體family typedef struct { int n_faces; PangoFontFace **faces; }custom_PangoFontFace_t; //字體family 的總數(shù) static int n_families=0; //當(dāng)前正在使用的family 字體的索引 static int family_id=0; // 記錄每個(gè)字體family中的face(字模) static custom_PangoFontFace_t *custom_PangoFontFace;
定義一個(gè)回調(diào)函數(shù),刷新福字
static void change_fu_cb(GtkButton *button , gpointer user_data) { char *markup_fu_str = NULL; // 3*3 的矩陣中一共有9個(gè)福字,分別設(shè)置每個(gè)福字的字體 for(int i=0;i<3;++i) { for(int j=0;j<3;++j) { // 獲取當(dāng)前family中的下一個(gè)字模,如果當(dāng)前family中的字模都是用了,自動(dòng)更新family_id 指向下一個(gè)family if(family_id >= n_families) family_id=0; if(++face_id >= custom_PangoFontFace[family_id].n_faces){ ++family_id; if(family_id >= n_families) family_id=0; face_id = 0; } markup_fu_str = g_markup_printf_escaped("<span font-weight=\"Bold\" font_desc=\"%s 99\">%s</span>", pango_font_description_to_string(pango_font_face_describe(custom_PangoFontFace[family_id].faces[0])), FONT_DISPLAY); gtk_label_set_markup(GTK_LABEL(g_fu_label[i][j]),markup_fu_str); g_free(markup_fu_str); } } }
應(yīng)用初始化程序
static void activate (GtkApplication* app, gpointer user_data) { GtkWidget *window; GtkWidget *mainGrid; GtkWidget *overlay; GtkWidget *picture; GtkCssProvider *cssProvider; const char *cssMainGrid = "grid {background-color:#ed3c43;}"; const char *cssButton = "* {background-color:#FF0000;}"; PangoFontMap *pangoFontMap; GtkWidget *box; GtkWidget *button; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), ""); gtk_window_set_default_size (GTK_WINDOW (window), 900, 900); mainGrid = gtk_grid_new(); gtk_widget_set_hexpand(mainGrid,TRUE); gtk_widget_set_vexpand(mainGrid,TRUE); box = gtk_box_new(GTK_ORIENTATION_VERTICAL,0); gtk_widget_set_hexpand(box,TRUE); gtk_widget_set_vexpand(box,TRUE); gtk_box_append(GTK_BOX(box),mainGrid); button = gtk_button_new_with_label("換一批"); gtk_widget_set_hexpand(button,TRUE); gtk_widget_set_vexpand(button,TRUE); cssProvider = gtk_css_provider_new(); gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(cssProvider), cssButton, -1); gtk_style_context_add_provider(gtk_widget_get_style_context(button), GTK_STYLE_PROVIDER(cssProvider), GTK_STYLE_PROVIDER_PRIORITY_USER); gtk_style_context_add_provider(gtk_widget_get_style_context(gtk_widget_get_first_child(button)), GTK_STYLE_PROVIDER(cssProvider), GTK_STYLE_PROVIDER_PRIORITY_USER); g_object_unref(cssProvider); g_signal_connect(button,"clicked",G_CALLBACK(change_fu_cb),NULL); gtk_box_append(GTK_BOX(box),button); gtk_window_set_child(GTK_WINDOW(window),box); cssProvider = gtk_css_provider_new(); gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(cssProvider), cssMainGrid, -1); gtk_style_context_add_provider(gtk_widget_get_style_context(mainGrid), GTK_STYLE_PROVIDER(cssProvider), GTK_STYLE_PROVIDER_PRIORITY_USER); g_object_unref(cssProvider); //獲取當(dāng)前系統(tǒng)中可用的字體 family PangoFontFamily **families; pangoFontMap = pango_cairo_font_map_get_default(); pango_font_map_list_families(pangoFontMap,&families,&n_families); custom_PangoFontFace = (custom_PangoFontFace_t *)g_malloc0(n_families*sizeof(custom_PangoFontFace_t)); for(int i=0;i<n_families;++i) { //獲取每個(gè)字體 family 中可用的face pango_font_family_list_faces(families[i], &custom_PangoFontFace[i].faces, &custom_PangoFontFace[i].n_faces); } g_free(families); // 初始化每個(gè)福字控件 for(int i=0;i<3;++i) { for(int j=0;j<3;++j) { overlay = gtk_overlay_new(); gtk_widget_set_hexpand(overlay,TRUE); gtk_widget_set_vexpand(overlay,TRUE); // 福字的背景圖,需要放在執(zhí)行程序所在的目錄 picture = gtk_picture_new_for_filename("./fu.png"); gtk_widget_set_hexpand(picture,TRUE); gtk_widget_set_vexpand(picture,TRUE); g_fu_label[i][j] = gtk_label_new(""); gtk_widget_set_hexpand(g_fu_label[i][j],TRUE); gtk_widget_set_vexpand(g_fu_label[i][j],TRUE); gtk_overlay_set_child(GTK_OVERLAY(overlay),picture); gtk_overlay_add_overlay(GTK_OVERLAY(overlay),g_fu_label[i][j]); gtk_grid_attach(GTK_GRID(mainGrid),overlay,i,j,1,1); } } //初始化福字 change_fu_cb(GTK_BUTTON(button) , NULL); gtk_window_set_resizable(GTK_WINDOW(window),FALSE); gtk_widget_show (window); }
主程序
int main(int argc, char *argv[]) { GtkApplication *app; int status; app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); //釋放資源 for(int i=0;i<n_families;++i){ g_free(custom_PangoFontFace[i].faces); } g_free(custom_PangoFontFace); return status; }
效果展示
到此這篇關(guān)于C語言實(shí)現(xiàn)生成新春福字的示例詳解的文章就介紹到這了,更多相關(guān)C語言生成福字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)簡易網(wǎng)絡(luò)聊天室
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡易網(wǎng)絡(luò)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C/C++?string.h庫中memcpy()和memmove()的使用
memcpy與memmove的目的都是將N個(gè)字節(jié)的源內(nèi)存地址的內(nèi)容拷貝到目標(biāo)內(nèi)存地址中,本文主要介紹了C/C++?string.h庫中memcpy()和memmove()的使用,感興趣的可以了解一下2023-12-12