//Download by http://www.NewXing.com
/*--------------------------------------
CLOCK.C -- Analog Clock Program
--------------------------------------*/
//时钟的每整点的时间数字显示出来,并在时钟内显示出当前的年月日,
//同时分针,时针,秒针用不同画笔和画刷填充不同(颜色自己选定).
#include <windows.h>
#include <math.h>
#define ID_TIMER 1
#define TWOPI (2 * 3.14159)
void DisplayDigit (HDC hdc, int iNumber)
{
static BOOL fSevenSegment [10][7] = //0~9数字的排布
{
1, 1, 1, 0, 1, 1, 1, // 0
0, 0, 1, 0, 0, 1, 0, // 1
1, 0, 1, 1, 1, 0, 1, // 2
1, 0, 1, 1, 0, 1, 1, // 3
0, 1, 1, 1, 0, 1, 0, // 4
1, 1, 0, 1, 0, 1, 1, // 5
1, 1, 0, 1, 1, 1, 1, // 6
1, 0, 1, 0, 0, 1, 0, // 7
1, 1, 1, 1, 1, 1, 1, // 8
1, 1, 1, 1, 0, 1, 1 // 9
} ;
static POINT ptSegment [7][6] =
{
7, 6, 11, 2, 31, 2, 35, 6, 31, 10, 11, 10,
6, 7, 10, 11, 10, 31, 6, 35, 2, 31, 2, 11,
36, 7, 40, 11, 40, 31, 36, 35, 32, 31, 32, 11,
7, 36, 11, 32, 31, 32, 35, 36, 31, 40, 11, 40,
6, 37, 10, 41, 10, 61, 6, 65, 2, 61, 2, 41,
36, 37, 40, 41, 40, 61, 36, 65, 32, 61, 32, 41,
7, 66, 11, 62, 31, 62, 35, 66, 31, 70, 11, 70
} ;
int iSeg ;
for (iSeg = 0 ; iSeg < 7 ; iSeg++)
if (fSevenSegment [iNumber][iSeg])
Polygon (hdc, ptSegment [iSeg], 6) ;
}
void DisplayTwoDigits (HDC hdc, int iNumber) //输出2个数
{
DisplayDigit (hdc, iNumber/10) ;
OffsetWindowOrgEx (hdc, -50, 0, NULL) ;
DisplayDigit (hdc, iNumber % 10) ;
OffsetWindowOrgEx (hdc, -150, 0, NULL) ;
}
void DisplayyearTime(HDC hdc) //显示日期
{
SYSTEMTIME st ;
GetLocalTime (&st) ;
DisplayTwoDigits (hdc, (st.wYear%=10)?st.wYear:10) ;//显示年
DisplayTwoDigits (hdc, st.wMonth) ; //显示月
DisplayTwoDigits (hdc, st.wDay) ;//显示日
}
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Clock") ;
HWND hwnd;
MSG msg;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("时钟示例"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void SetIsotropic (HDC hdc, int cxClient, int cyClient)
{
SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExtEx (hdc, 1000, 1000, NULL) ;
SetViewportExtEx (hdc, cxClient / 2, -cyClient / 2, NULL) ;
SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
}
void RotatePoint (POINT pt[], int iNum, int iAngle)
{
int i ;
POINT ptTemp ;
for (i = 0 ; i < iNum ; i++)
{
ptTemp.x = (int) (pt[i].x * cos (TWOPI * iAngle / 360) +
pt[i].y * sin (TWOPI * iAngle / 360)) ;
ptTemp.y = (int) (pt[i].y * cos (TWOPI * iAngle / 360) -
pt[i].x * sin (TWOPI * iAngle / 360)) ;
pt[i] = ptTemp ;
}
}
void DrawClock (HDC hdc) //画表盘
{
int iAngle ;
POINT pt[3] ;
HBRUSH hb;
int tm[12]={12,1,2,3,4,5,6,7,8,9,10,11};
HFONT hf;
hb=CreateSolidBrush(RGB(250,0,0));
hf=CreateFont(80,80,0,0,0,0,0,0,0,0,0,0,0,0);
SelectObject(hdc,hb);
SelectObject(hdc,hf);
for (iAngle = 0 ; iAngle < 360 ; iAngle += 6)
{
pt[0].x = 0 ;
pt[0].y = 900 ;
RotatePoint (pt, 1, iAngle) ;
pt[2].x = pt[2].y = iAngle % 5 ? 33 : 100 ;
pt[0].x -= pt[2].x / 2 ;
pt[0].y -= pt[2].y / 2 ;
pt[1].x = pt[0].x + pt[2].x ;
pt[1].y = pt[0].y + pt[2].y ;
if (iAngle==0) //这里,我试着把1—12存在数组中再读出的方式输出表盘上的点数
{
TextOut(hdc,pt[0].x-50,pt[1].y,"1",1); //但是失败了,只有用笨办法一个字符一
TextOut(hdc,pt[0].x+30,pt[1].y,"2",1); //个字符的输出
}//输出12
else if (iAngle==30) TextOut(hdc,pt[0].x,pt[1].y,"1",1);//输出1
else if (iAngle==60) TextOut(hdc,pt[0].x,pt[1].y,"2",1);
else if (iAngle==90) TextOut(hdc,pt[0].x,pt[1].y,"3",1);
else if (iAngle==120) TextOut(hdc,pt[0].x,pt[1].y,"4",1);
else if (iAngle==150) TextOut(hdc,pt[0].x,pt[1].y,"5",1);
else if (iAngle==180) TextOut(hdc,pt[0].x,pt[1].y,"6",1);
else if (iAngle==210) TextOut(hdc,pt[0].x,pt[1].y,"7",1);
else if (iAngle==240) TextOut(hdc,pt[0].x,pt[1].y,"8",1);
else if (iAngle==270) TextOut(hdc,pt[0].x,pt[1].y,"9",1);
else if (iAngle==300)
{
TextOut(hdc,pt[0].x-50,pt[1].y,"1",1);//输出10
TextOut(hdc,pt[0].x+30,pt[1].y,"0",1);
}
else if (iAngle==330) //11
{
TextOut(hdc,pt[0].x-50,pt[1].y,"1",1);
TextOut(hdc,pt[0].x+30,pt[1].y,"1",1);
}
else Ellipse (hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y) ; //画点
}
DeleteObject(hf);
DeleteObject(hb);
}
void DrawHands (HDC hdc, SYSTEMTIME * pst, BOOL fChange) //画指针
{
static POINT pt[3][5] =
{
0, -150, 100, 0, 0, 600, -100, 0, 0, -150,
0, -200, 50, 0, 0, 800, -50, 0, 0, -200,
0, 0, 0, 0, 0, 0, 0, 0, 0, 800
} ;
int i, iAngle[3] ;
POINT ptTemp[3][5] ;
HBRUSH hb1,hb2;
HPEN hp;
hb1=CreateSolidBrush(RGB(0,250,0));
hb2=CreateSolidBrush(RGB(0,0,250));
hp=CreatePen(PS_SOLID,1,RGB(50,250,0));
iAngle[0] = (pst->wHour * 30) % 360 + pst->wMinute / 2 ;
iAngle[1] = pst->wMinute * 6 ;
iAngle[2] = pst->wSecond * 6 ;
memcpy (ptTemp, pt, sizeof (pt)) ;
for (i = fChange ? 0 : 2 ; i < 3 ; i++)
{
if(i==0)
{
SelectObject(hdc,hb1); //上色
SelectObject(hdc,hp);
}
if(i==1)
{
SelectObject(hdc,hb2);
SelectObject(hdc,hp);
}
RotatePoint (ptTemp[i], 5, iAngle[i]) ;
Polygon (hdc, ptTemp[i], 5) ;
}
DeleteObject(hb1);
DeleteObject(hb2);
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient ;
static SYSTEMTIME stPrevious ;
BOOL fChange ;
HDC hdc ;
PAINTSTRUCT ps ;
SYSTEMTIME st ;
HBRUSH hbr;
switch (message)
{
case WM_CREATE :
SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
GetLocalTime (&st) ;
stPrevious = st ;
return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_TIMER :
GetLocalTime (&st) ;
fChange = st.wHour != stPrevious.wHour ||
st.wMinute != stPrevious.wMinute ;
hdc = GetDC (hwnd) ;
SetIsotropic (hdc, cxClient, cyClient) ;
// SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
// DrawHands (hdc, &stPrevious, fChange) ;
SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
DrawHands (hdc, &st, TRUE) ;
ReleaseDC (hwnd, hdc) ;
stPrevious = st ;
InvalidateRect (hwnd, NULL, TRUE) ;//用了刷新就可以不用白色指针覆盖
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
SetIsotropic (hdc, cxClient, cyClient) ;
DrawClock (hdc) ;
DrawHands (hdc, &stPrevious, TRUE) ;
SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExtEx (hdc, 276*5, 72*3, NULL) ;
SetViewportExtEx (hdc, cxClient, cyClient, NULL
没有合适的资源?快使用搜索试试~ 我知道了~
vc++时钟代码在此下载 圆形
共6个文件
plg:1个
cpp:1个
ncb:1个
5星 · 超过95%的资源 需积分: 10 8 下载量 88 浏览量
2012-12-12
09:56:44
上传
评论
收藏 8KB RAR 举报
温馨提示
时钟代码 VC++ 课程设计不是问题 用她简单的改一下就可以得到一个精美的时钟勒
资源推荐
资源详情
资源评论
收起资源包目录
VC++ 圆形动态时钟程序源码.rar (6个子文件)
www.NewXing.com
clock
clock.dsp 4KB
clock.cpp 8KB
clock.dsw 535B
Debug
clock.plg 1KB
clock.ncb 41KB
clock.opt 53KB
共 6 条
- 1
资源评论
- weeyee_ye2013-12-02下下来后,可以参考,谢谢了。
ellen1992
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功