• 大小: 520KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: 其他
  • 标签: 电子时钟  gtk  linux  

资源简介

这是一个基于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


评论

共有 条评论