资源简介
这是一个基于Linux环境下,用gtk技术开发的电子时钟,图形界面优美

代码片段和文件信息
#include // 绘图所需要的头文件
#include
#include
#include
#include
#include
#define WIN_W 800
#define WIN_H 480
typedef struct _Window
{
GtkWidget *main_window;
GtkWidget *table;
GtkWidget *hbutton_add;
GtkWidget *hbutton_sub;
GtkWidget *mbutton_add;
GtkWidget *mbutton_sub;
GtkWidget *sbutton_add;
GtkWidget *sbutton_sub;
GtkWidget *button_set;
GtkWidget *button_ok;
GtkWidget *button_cancel;
char *gs_bmp_name[20]; // 存放图片目录和文件名地址的指针数组
int gs_bmp_total; // 图片总数
int gs_index; // 当前图片标号
char time_buf[20]; // 时间buf
struct tm *local_time; // time to show or modify
guint timer_id; // 定时器id
}WINDOW;
// 设置系统时间
void settime(int tm_hour int tm_min int tm_sec)
{
struct tm *time_set = NULL;
struct timeval tv;
struct timezone tz;
/* 获取当前时间 */
gettimeofday(&tv &tz);
/* 获取当前时间 */
time_set = gmtime(&tv.tv_sec);
/* 设置当前时间结构体 */
time_set->tm_hour = tm_hour;
time_set->tm_min = tm_min;
time_set->tm_sec = tm_sec;
/* 获取用秒表示的时间 */
tv.tv_sec = mktime(time_set);
/* 设置当前时间 */
settimeofday(&tv &tz);
}
// 给创建好的image重新设计一张图片
void load_image(GtkWidget *image const char *file_path const int w const int h )
{
gtk_image_clear( GTK_IMAGE(image) ); // 清除图像
GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(file_path NULL); // 创建图片资源
GdkPixbuf *dest_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf w h GDK_INTERP_BILINEAR); // 指定大小
gtk_image_set_from_pixbuf(GTK_IMAGE(image) dest_pixbuf); // 图片控件重新设置一张图片(pixbuf)
g_object_unref(src_pixbuf); // 释放资源
g_object_unref(dest_pixbuf); // 释放资源
}
/* 功能: 根据图片路径创建一个新按钮,同时指定图片大小
* file_path: 图片路径
* w h: 图片的宽度和高度
*/
GtkWidget *create_button_from_file(const char *file_path const int w const int h)
{
GtkWidget *temp_image = gtk_image_new_from_pixbuf(NULL);
load_image(temp_image file_path w h);
GtkWidget *button = gtk_button_new(); // 先创建空按钮
gtk_button_set_image(GTK_BUTTON(button) temp_image); // 给按钮设置图标
gtk_button_set_relief(GTK_BUTTON(button) GTK_RELIEF_NONE); // 按钮背景色透明
return button;
}
/* 功能: 绘图事件 主要是通过绘图设置背景图,画时间
* event: 事件类型
* data: WINDOW变量的地址
*/
gboolean on_expose_event(GtkWidget *widget GdkEventExpose *event gpointer data)
{
WINDOW *p_temp = (WINDOW *)data;
cairo_t *cr = gdk_cairo_create(widget->window); // 创建cairo环境
// 获取图片
GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(p_temp->gs_bmp_name[p_temp->gs_index] NULL);
// 指定图片大小
GdkPixbuf* dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf WIN_W WIN_H GDK_INTERP_BILINEAR);
// dst_pixbuf作为cr环境的画图原材料,(0 0):画图的起点坐标
gdk_cairo_set_source_pixbuf(cr dst_pixbuf 0 0);
cairo_paint(cr); // 绘图
cairo_set_source_rgb(cr 0 0 0);
cairo_set_font_size(cr 120.0); // 设置字体大小
cairo_move_to(cr 60 230); // 将“画笔”移动到图像区域的(10.0 34.0)位置开始绘制文本
cairo_show_text(cr p_temp->time_buf); // 写字
cairo_destroy(cr); // 回收所有Cai
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 17875 2013-11-28 18:36 电子时钟\demo
文件 15129 2013-11-28 18:35 电子时钟\demo.c
文件 215287 2013-11-25 10:55 电子时钟\demo_show.png
文件 31353 2013-11-04 16:02 电子时钟\image\1.jpg
文件 40365 2013-11-04 16:02 电子时钟\image\2.jpg
文件 33936 2013-11-04 16:02 电子时钟\image\3.jpg
文件 27953 2013-11-04 16:14 电子时钟\image\4.jpg
文件 53298 2013-11-04 16:02 电子时钟\image\5.jpg
文件 76704 2013-11-04 16:02 电子时钟\image\6.jpg
文件 3484 2013-11-04 16:14 电子时钟\image\button_add.png
文件 4172 2013-11-04 16:14 电子时钟\image\button_cancel.png
文件 2962 2013-11-04 16:14 电子时钟\image\button_ok.png
文件 3857 2013-11-04 16:14 电子时钟\image\button_set.png
文件 6599 2013-11-04 16:14 电子时钟\image\button_skin.png
文件 4867 2013-11-04 16:14 电子时钟\image\button_sub.png
..A.SH. 33280 2013-11-29 08:02 电子时钟\image\Thumbs.db
文件 165 2013-11-04 16:14 电子时钟\Makefile
文件 42 2013-11-04 16:14 电子时钟\readme.txt
..A.SH. 5120 2013-11-04 16:14 电子时钟\Thumbs.db
目录 0 2014-05-07 08:30 电子时钟\image
目录 0 2014-05-07 08:30 电子时钟
----------- --------- ---------- ----- ----
576448 21
- 上一篇:esp8266红外例程
- 下一篇:Louis_IC卡.zip
相关资源
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- 单片机电子时钟 闹钟 日历
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- GTK实现数字表显示
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- SA1110处理器掌上电脑液晶显示器设计
评论
共有 条评论