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

📄 clock.c

📁 这是一个在minigui下面用高级GDI函数实现电子石英钟的代码
💻 C
字号:
/*#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#include <sys/time.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <pthread.h>#include <semaphore.h>#include <popt.h>#include <unistd.h>*/#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>#include <minigui/mywindows.h>#include <minigui/mgext.h>#include <minigui/vcongui.h>#include <minigui/filedlg.h>HWND hMainWnd;BITMAP  bmp;#define TIMER0		0#define IDC_RTC1		1000#define CTRL_RTC	"rtcdisp"#define RTC_SETTIME	800#define RTC_GETTIME	801#define RTC_SETHOUR	802#define RTC_SETMIN	803#define RTC_SETSEC	804typedef struct time{	int hour;	int min;	int sec;}Time;#define TDRAW_BAS_X	2#define TDRAW_BAS_Y	23static void DispTimeSub(HDC hdc,int x,int y,int r){	x=TDRAW_BAS_X+r+x;	y=TDRAW_BAS_Y+r+y;		SetPixel(hdc,x,y,PIXEL_magenta);	SetPixel(hdc,x+1,y,PIXEL_magenta);	SetPixel(hdc,x-1,y,PIXEL_magenta);	SetPixel(hdc,x,y+1,PIXEL_magenta);	SetPixel(hdc,x,y-1,PIXEL_magenta);}static void DispTimeSubHL(HDC hdc,int x,int y,int r){	x=TDRAW_BAS_X+r+x;	y=TDRAW_BAS_Y+r+y;		SetPenColor(hdc,PIXEL_magenta);	MoveTo(hdc,x,y-1);	LineTo(hdc,x-3,y-1);	MoveTo(hdc,x,y+1);	LineTo(hdc,x-3,y+1);}static void DispTimeSubHR(HDC hdc,int x,int y,int r){	x=TDRAW_BAS_X+r+x;	y=TDRAW_BAS_Y+r+y;		SetPenColor(hdc,PIXEL_magenta);	MoveTo(hdc,x,y-1);	LineTo(hdc,x+3,y-1);	MoveTo(hdc,x,y+1);	LineTo(hdc,x+3,y+1);}static void DispTimeSubLT(HDC hdc,int x,int y,int r){	x=TDRAW_BAS_X+r+x;	y=TDRAW_BAS_Y+r+y;		SetPenColor(hdc,PIXEL_magenta);	MoveTo(hdc,x-1,y);	LineTo(hdc,x-1,y-4);	MoveTo(hdc,x+1,y);	LineTo(hdc,x+1,y-4);}static void DispTimeSubLB(HDC hdc,int x,int y,int r){	x=TDRAW_BAS_X+r+x;	y=TDRAW_BAS_Y+r+y;		SetPenColor(hdc,PIXEL_magenta);	MoveTo(hdc,x-1,y);	LineTo(hdc,x-1,y+4);	MoveTo(hdc,x+1,y);	LineTo(hdc,x+1,y+4);}static const POINT PD_R40D30[]={{40,0},{34,19},{20,34},{0,39},{-19,34},{-34,20},{-40,0},{-34,-20},{-19,-34},{0,-39},{20,-34},{34,-19}};static void DrawTimeFrame(HDC hdc,int hight){	int i;		SetPenColor(hdc,PIXEL_blue);	MoveTo(hdc,0,hight);	LineTo(hdc,90,hight);	MoveTo(hdc,0,hight+2);	LineTo(hdc,90,hight+2);	for(i=0;i<12;i++)	{ if((i%3)==0)		{ if((i==0) || (i==6))			{ if(i==0) DispTimeSubHL(hdc,PD_R40D30[i].x,PD_R40D30[i].y,40);			  else	DispTimeSubHR(hdc,PD_R40D30[i].x,PD_R40D30[i].y,40);			}			else			{ if(i==3) DispTimeSubLT(hdc,PD_R40D30[i].x,PD_R40D30[i].y,40);			  else	DispTimeSubLB(hdc,PD_R40D30[i].x,PD_R40D30[i].y,40);			}		}		else		{ DispTimeSub(hdc,PD_R40D30[i].x,PD_R40D30[i].y,40);		}	}}static const POINT PD_R30D6[]={{30,0},{29,3},{29,6},{28,9},{27,12},{25,14},{24,17},{22,20},{20,22},{17,24},{15,25},{12,27},{9,28},{6,29},{3,29},{0,29},{-3,29},{-6,29},{-9,28},{-12,27},{-14,25},{-17,24},{-20,22},{-22,20},{-24,17},{-25,15},{-17,12},{-28,9},{-29,6},{-29,3},{-29,0},{-29,-3},{-29,-6},{-28,-9},{-27,-12},{-25,-14},{-24,-17},{-22,-20},{-20,-22},{-17,-24},{-15,-25},{-12,-27},{-9,-28},{-6,-29},{-3,-29},{0,-29},{3,-29},{6,-29},{9,-28},{12,-27},{14,-25},{17,-24},{20,-22},{22,-20},{24,-17},{25,-15},{27,-12},{28,-9},{29,-6},{29,-3} };static const POINT PD_R20D5[]={{20,0},{19,1},{19,3},{19,5},{18,6},{18,8},{17,9},{16,11},{15,12},{14,14},{12,15},{11,16},{10,17},{8,18},{6,18},{5,19},{3,19},{1,19},{0,19},{-1,19},{-3,19},{-5,19},{-6,18},{-8,18},{-9,17},{-11,16},{-12,15},{-14,14},{-15,12},{-16,11},{-17,10},{-18,8},{-18,6},{-19,5},{-19,3},{-19,1},{-19,0},{-19,-1},{-19,-3},{-19,-5},{-18,-6},{-18,-8},{-17,-9},{-16,-11},{-15,-12},{-14,-14},{-12,-15},{-11,-16},{-10,-17},{-8,-18},{-6,-18},{-5,-19},{-3,-19},{-1,-19},{0,-19},{1,-19},{3,-19},{5,-19},{6,-18},{8,-18},{9,-17},{11,-16},{12,-15},{14,-14},{15,-12},{16,-11},{17,-10},{18,-8},{18,-6},{19,-5},{19,-3},{19,-1} };static void DrawTime (HWND hWnd,HDC hdc,Time *rtime){	static int old_minx=0;	static int old_miny=0;	static int old_hourx=0;	static int old_houry=0;	static int r =40;		int rx,ry;	int x,y;	int n;	int bak;		rx=TDRAW_BAS_X+r;	ry=TDRAW_BAS_Y+r;		n=(60*rtime->min)/60;	n=n+45;	if(n>59) n=n-60;	x=TDRAW_BAS_X+r+PD_R30D6[n].x;	y=TDRAW_BAS_Y+r+PD_R30D6[n].y;		if((x==old_minx)&&(y==old_miny)) return;		bak=GetWindowBkColor(hWnd);	if((old_minx+old_miny)!=0)	{		SetPenColor(hdc,bak);		MoveTo(hdc,rx,ry);		LineTo(hdc,old_hourx,old_houry);	}		SetPenType(hdc,PT_SOLID);	SetPenWidth(hdc,2);	if((old_hourx+old_houry)!=0)	{		SetPenColor(hdc,bak);		LineEx(hdc,rx,ry,old_hourx,old_houry);	}		SetPenColor(hdc,PIXEL_darkgray);	MoveTo(hdc,rx,ry);	LineTo(hdc,x,y);	old_minx=x;	old_miny=y;		if((rtime->hour)>11) bak=rtime->min+(rtime->hour-12)*60;	  else bak=rtime->min+rtime->hour*60;	n=(72*bak)/720;	n=n+54;	if(n>71) n=n-72;	x=TDRAW_BAS_X+r+PD_R20D5[n].x;	y=TDRAW_BAS_Y+r+PD_R20D5[n].y;	LineEx(hdc,rx,ry,x,y);	old_hourx=x;	old_houry=y;}DispTimeNum(HDC hdc,Time *rtime){	char disp_buf[50];		sprintf(disp_buf," %02d:%02d:%02d ",rtime->hour,rtime->min,rtime->sec);	TextOut(hdc,0,0,disp_buf);};	static int RTCControlProc(HWND hWnd,int message,WPARAM wParam,LPARAM lParam){	HDC hdc;	static Time s_rtime;	static PLOGFONT s_font;		Time * p_btime;	switch(message)	{		case MSG_CREATE:			s_font=CreateLogFont(NULL,"Courier","ISO08859 - 1",					  FONT_WEIGHT_BOLD,					  FONT_SLANT_ROMAN,					  FONT_SETWIDTH_NORMAL,					  FONT_SPACING_CHARCELL,					  FONT_UNDERLINE_NONE,					  FONT_STRUCKOUT_NONE,					  16,0);						p_btime=(Time * )GetWindowAdditionalData(hWnd);			if(p_btime!=NULL	)			{				if((p_btime->hour)>23) s_rtime.hour=0;					else s_rtime.hour=p_btime->hour;				if((p_btime->min)>59) s_rtime.min=0;					else s_rtime.min=p_btime->min;				if((p_btime->sec)>59) s_rtime.sec=0;					else s_rtime.sec=p_btime->sec;			}			else			{				s_rtime.hour=0;				s_rtime.min=0;				s_rtime.sec=0;			}			SetTimer(hWnd,TIMER0,100);						break;		case MSG_PAINT:			hdc=BeginPaint(hWnd);			//FillBoxWithBitmap (hdc, 0, 0,240,320, &bmp);			SelectFont(hdc,s_font);			DispTimeNum(hdc,&s_rtime);			DrawTimeFrame(hdc,17);			DrawTime(hWnd,hdc,&s_rtime);			EndPaint(hWnd,hdc);			return(0);					case MSG_TIMER:			if(wParam==TIMER0)			{ if((++s_rtime.sec)>59)			  { s_rtime.sec=0;			    if((++s_rtime.min)>59)			    { s_rtime.min=0;			      if((++s_rtime.hour)>23) s_rtime.hour=0;			     }			    }			   }			   hdc=GetClientDC(hWnd);			   SelectFont(hdc,s_font);				   DispTimeNum(hdc,&s_rtime);			   DrawTime(hWnd,hdc,&s_rtime);			   ReleaseDC(hdc);			   break;			   		case RTC_SETTIME:			p_btime=(Time * ) lParam;			if(p_btime!=NULL)			{ if((p_btime->hour)>23) s_rtime.hour=0;			  	else s_rtime.hour=p_btime->hour;			  if((p_btime->min)>59) s_rtime.min=0;			  	else s_rtime.min=p_btime->min;			  if((p_btime->sec)>59) s_rtime.sec=0;			  	else s_rtime.sec=p_btime->sec;							 hdc=GetClientDC(hWnd);			 SelectFont(hdc,s_font);			 DispTimeNum(hdc,&s_rtime);			 DrawTime(hWnd,hdc,&s_rtime);			 ReleaseDC(hdc);			}			break;					case RTC_GETTIME:			p_btime=(Time * ) lParam;			if(p_btime!=NULL)			{				p_btime->hour=s_rtime.hour;				p_btime->min=s_rtime.min;				p_btime->sec=s_rtime.sec;			}			break;					case RTC_SETHOUR:			if(lParam>23) s_rtime.hour=0;				else s_rtime.hour=lParam;			hdc=GetClientDC(hWnd);			SelectFont(hdc,s_font);			DispTimeNum(hdc,&s_rtime);			DrawTime(hWnd,hdc,&s_rtime);			ReleaseDC(hdc);			break;					case RTC_SETMIN:			if(lParam>59) s_rtime.min=0;				else s_rtime.min=lParam;			hdc=GetClientDC(hWnd);			SelectFont(hdc,s_font);			DispTimeNum(hdc,&s_rtime);			DrawTime(hWnd,hdc,&s_rtime);			ReleaseDC(hdc);			break;					case RTC_SETSEC:			if(lParam>59) s_rtime.sec=0;				else s_rtime.sec=lParam;			hdc=GetClientDC(hWnd);			SelectFont(hdc,s_font);			DispTimeNum(hdc,&s_rtime);			DrawTime(hWnd,hdc,&s_rtime);			ReleaseDC(hdc);			break;					case MSG_DESTROY:			KillTimer(hWnd,TIMER0);			//UnloadBitmap (&bmp);			EndDialog (hWnd, wParam);			DestroyAllControls(hWnd);			break;		default:			return(DefaultMainWinProc(hWnd,message,wParam,lParam));	}	return(0);}static BOOL RegisterMyControl(void){	WNDCLASS MyClass;		MyClass.spClassName=CTRL_RTC;	MyClass.dwStyle=WS_NONE;	MyClass.dwExStyle=WS_EX_NONE;	MyClass.hCursor=GetSystemCursor(IDC_BUSY);	MyClass.iBkColor=COLOR_lightgray;	MyClass.WinProc=RTCControlProc;	return(RegisterWindowClass(&MyClass));}static int WinProc(HWND hWnd,int message,WPARAM wParam,LPARAM lParam){	HDC hdc;	static HWND s_myhwnd;	Time stime;	switch(message)	{		case MSG_CREATE:			stime.hour=9;			stime.min=59;			stime.sec=30;			s_myhwnd=CreateWindow(CTRL_RTC,"",					      WS_CHILD | WS_VISIBLE | WS_BORDER,					      IDC_RTC1,0,0,90,110,hWnd,(LPARAM)(&stime));			break;					case MSG_LBUTTONDOWN:			SendMessage(s_myhwnd,RTC_GETTIME,0L,(LPARAM)(&stime));			stime.hour++;			stime.min++;			stime.sec++;			SendMessage(s_myhwnd,RTC_GETTIME,0L,(LPARAM)(&stime));			break;		case MSG_CLOSE:			KillTimer(hWnd,TIMER0);			EndDialog (hWnd, wParam);			DestroyAllControls(hWnd);			DestroyMainWindow(hWnd);			PostQuitMessage(hWnd);			InvalidateRect(hWnd, NULL, TRUE);			break;		default:			return(DefaultMainWinProc(hWnd,message,wParam,lParam));	}	return(0);}int InitMainWindow(void){	MAINWINCREATE window_info;	window_info.dwStyle=WS_VISIBLE|WS_BORDER|WS_CAPTION;	window_info.dwExStyle=WS_EX_NONE;	window_info.spCaption="电子钟";	window_info.hMenu=0;	window_info.hCursor=GetSystemCursor(0);	window_info.hIcon=GetSmallSystemIcon(IDI_APPLICATION);	window_info.MainWindowProc=WinProc;	window_info.lx=70;	window_info.ty=40;	window_info.rx=163;	window_info.by=174;	window_info.iBkColor=COLOR_lightwhite;	window_info.dwAddData=0;	window_info.hHosting=HWND_DESKTOP;	hMainWnd=CreateMainWindow(&window_info);	if(hMainWnd==HWND_INVALID)	return(0);	else				return(1);}int MiniGUIMain(int argc,const char *argv[])//int clock(void){	MSG Msg;	#ifdef _LITE_VERSION		SetDesktopRect(0,0,240,320);	#endif		RegisterMyControl();		InitMainWindow();		ShowWindow(hMainWnd,SW_SHOWNORMAL);		while(GetMessage(&Msg,hMainWnd))	{		TranslateMessage(&Msg);		DispatchMessage(&Msg);	}		UnregisterWindowClass(CTRL_RTC);	MainWindowThreadCleanup(hMainWnd);	return(0);}#ifndef _LITE_VERSION	#include<minigui/dti.c>#endif

⌨️ 快捷键说明

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