快新年了,支付宝扫福活动又开始了,每次都要百度找福,这次不想找了,自己写一个程序生成各种字体的福字。
#define font_display "福" // g_fu_label中的每一个控件都是一个福字static gtkwidget *g_fu_label[3][3];// 记录所有的字体familytypedef struct { int n_faces; pangofontface **faces;}四级考试技巧custom_pangofontface_t;//字体family 的总数static int n_families=0;//当前正在使用的family 字体的索引static int family_id=0;// 记录每个字体family中的face(字模)static custom_pangofontface_t *custom_pangofontface;
static voidchange_fu_cb(gtkbutton *button , gpointer ur_data){ char *markup_fu_str = null; // 3*3 的矩阵中一共有9个福字,分别设置每个福字的字体 for(int i=0;i<3;++i) { for(int j=0;j<3;++j) { // 获取当前family中的下一个字模,如果当前family中的字模都是用了,自动更新family_id 指向下一个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_t_markup(gtk_label(g_fu_label[i][j]),markup_fu_str); g_free(markup_fu_str); } }}
static voidactivate (gtkapplication* app, gpointer ur_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_t_title (gtk_window (window), ""); gtk_window_t_default_size (gtk_window (window), 900, 900); maingrid = gtk_grid_new(); gtk_widget_t_hexpand(maingrid,true); gtk_widget_t_vexpand(maingrid,true); box = gtk_box_new(gtk_orientation_vertical,0); gtk_widget_t_hexpand(box,true); gtk_widget_t_vexpand(box,true); gtk_box_append(gtk_box(box),maingrid); button = gtk_button_new_with_label("换一批"); gtk_widget_t_hexpand(button,true); gtk_widget_t_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_ur); gtk_style_context_add_provider(gtk_widget_get_style_context(gtk_widget_get_first_child(button)), gtk_style_provider(cssprovider), gtk_style_provider_priority_ur); g_object_unref(cssprovider); g_signal_connect(button,"clicked",g_callback(change_fu_cb),null); gtk_box_append(gtk_box(box),button); gtk_window_t_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_ur); g_object_unref(cssprovider); //获取当前系统中可用的字体 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) { //获取每个字体 family 中可用的face pango_font_family_list_faces(families[i], &custom_pangofontface[i].faces, &custom_pangofontface[i].n_faces); } g_free(families); // 初始化每个福字控件 for(int i=0;i<3;++i) { for(int j=0;j<3;++j) { overlay = gtk_overlay_new(); gtk_widget_t_hexpand(overlay,true); gtk_widget_t_vexpand(overlay,true); // 福字的背景图,需要放在执行程序所在的目录 picture = gtk_picture_new_for_filename("./fu.png"); gtk_widget_t_hexpand(picture,true); gtk_widget_t_vexpand(picture,true); g_fu_label[i][j] = gtk_label_new(""); gtk_widget_t_hexpand(g_fu_label[i][j],true); gtk_widget_t_vexpand(g_fu_label[i][英语音标怎么读j],true); gtk_overlay_t_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_t_resizable(gtk_window(window),fal); gtk_widget_show (window);}
intmain(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_f马六甲海峡位于amilies;++i){ g_free(custom_pangofontface[i].faces); } g_free(custom_pangofontface); return status;}
到此这篇关于c语言实现生成新春福字的示例详解的文章就介绍到这了,更多相关c语言生成福字内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 18:03:23,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7ca7e464cb81bc36c42cc9f42a91bf73.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C语言实现生成新春福字的示例详解.doc
本文 PDF 下载地址:C语言实现生成新春福字的示例详解.pdf
留言与评论(共有 0 条评论) |