⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk_clock_without_cairo.c

📁 一个用gtk编写的时钟, 没有用cairo进行渲染, 效果可能差一点, 但将就用吧!
💻 C
字号:
#include <gtk/gtk.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gdk/gdk.h>
#include <linux/kd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
int beep(int sec)
{ 
 int frequency=1000+sec*25;
 int duration=160;
 if(sec==60)
 {
  frequency=1500;
  duration=320;
 }
 int fd=open("/dev/tty0",O_RDWR|O_NDELAY);

 if (fd<0) 
 {
  perror("/dev/tty0");
  return 1;
 }
  /* 125 = 125 milliseconds, or 1/8th of a second;
      0x637 is the number of clock cycles in the standard kernel beep */ 
 if (ioctl(fd,KDMKTONE,(duration<<16)+1193180/frequency) == -1) 
 {
  perror("/dev/tty0: ioctl");
  return 1;
 }

 close(fd);
 return 0;
}
static gboolean on_expose_event(gpointer data)
{
 GtkWidget *clock=GTK_WIDGET(data);
/*
 gcarc1--The filled arc's gc
 gcarc2---The arc edge's gc
 gcquar---The quarter's gc 
*/
 GdkGC *gcarc1,*gcarc2,*gcquar;
/*
 gch--The hour hand's gc
 gcm--The minute hand's gc
 gcs--The second hand's gc
*/
 GdkGC *gch,*gcm,*gcs;
 GdkColor color;
 int  i;
 float radius=200;
 gcarc1 = gdk_gc_new (clock->window);
  color.red = 65535;
  color.green = 65535;
  color.blue = 65535;
 gdk_gc_set_rgb_fg_color (gcarc1, &color);
 gdk_draw_arc(clock->window,gcarc1,TRUE,0,0,radius*2,radius*2,0,360*64);

 gcarc2= gdk_gc_new (clock->window);
  color.red = 0;
  color.green = 0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcarc1, &color);
 gdk_draw_arc(clock->window,gcarc1,FALSE,0,0,radius*2,radius*2,0,360*64);

 gcquar = gdk_gc_new (clock->window);
  color.red = 0;
  color.green =0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcquar, &color);

 for(i=0;i<60;i++)
 {
  int inset;
  if (i % 15 == 0)
  {
    inset = 0.3 * radius;
  }
  else
  {
    if(i % 5 ==0)
     inset = 0.15 *radius;
    else inset = 0.05 * radius;
  }
 gdk_draw_line(clock->window,gcquar,
  radius+radius*sin(2*M_PI/60*i),
  radius-radius*cos(2*M_PI/60*i),
  radius+(radius-inset)*sin(2*M_PI/60*i),
  radius-(radius-inset)*cos(2*M_PI/60*i)
  );
 }

 gch = gdk_gc_new (clock->window);
  color.red = 65535;
  color.green = 0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gch, &color);

 gcm = gdk_gc_new (clock->window);
  color.red = 0;
  color.green = 65535;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcm, &color);

 gcs = gdk_gc_new (clock->window);
  color.red = 0;
  color.green = 0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcs, &color);
 
 time_t timet;
 struct tm tm;
 time(&timet);
 localtime_r(&timet,&tm);

 //draw sec hand
 gdk_draw_line(clock->window,gcs,radius,radius,
  radius+(radius*4/5)*sin(2*M_PI*tm.tm_sec/60),
  radius-(radius*4/5)*cos(2*M_PI*tm.tm_sec/60)
  );

 //draw min hand

 gdk_draw_line(clock->window,gcm,radius,radius,
  radius+(radius*3/5)*sin(2*M_PI*tm.tm_min/60+(2*M_PI/60)*tm.tm_sec/60),
  radius-(radius*3/5)*cos(2*M_PI*tm.tm_min/60+(2*M_PI/60)*tm.tm_sec/60)
            );

 //draw hour hand
 gdk_draw_line(clock->window,gch,radius,radius,
  radius+(radius*2/5)*sin(2*M_PI*tm.tm_hour/12+(2*M_PI/12)*tm.tm_min/60),
  radius-(radius*2/5)*cos(2*M_PI*tm.tm_hour/12+(2*M_PI/12)*tm.tm_min/60)
            );

 if(tm.tm_min==59)
 {
  if(tm.tm_sec==0)
   beep(60);
  else 
  {
   if(tm.tm_sec>55)
    beep(tm.tm_sec);
  }
 }
 g_object_unref(gch);
 g_object_unref(gcm);
 g_object_unref(gcs);
 g_object_unref(gcarc1);
 g_object_unref(gcarc2);
 g_object_unref(gcquar);
 return TRUE;
}
static gboolean updatetime(gpointer data)
{
 
 return  on_expose_event(data);
}
 int   main (int argc, char **argv)
{
 gtk_init(&argc,&argv);
 GtkWidget *window;
 GtkWidget *clock;

 gtk_init (&argc, &argv);
 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_MOUSE);
 
 GtkWidget *fixed=gtk_fixed_new();
 gtk_container_add (GTK_CONTAINER (window), fixed);
 gtk_widget_show(fixed);

 clock = gtk_drawing_area_new();
 gtk_fixed_put(GTK_FIXED(fixed),clock,0,0);
 gtk_widget_set_size_request(clock,600,480);
 g_signal_connect (window, "destroy",G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (clock, "expose_event",
   G_CALLBACK (on_expose_event), NULL);

 g_timeout_add(1000,updatetime,clock);
  gtk_widget_show_all (window);
 gtk_main ();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -