#include <windows.h>
#include "math.h"
//#include "../wcontrol.h"
#include "resource.h"
#define WATCH_CLSNAME "Watch - PC Assistant"
//定义使用中文语言字符。
#define CHINESE_LANGUAGE
void CenterWindows(HWND hWnd)
{
RECT rc;
GetWindowRect (hWnd,&rc);
int w=rc.right-rc.left;
int h=rc.bottom-rc.top;
int scrX=(int)GetSystemMetrics(SM_CXSCREEN);
int scrY=(int)GetSystemMetrics(SM_CYSCREEN);
MoveWindow(hWnd,(scrX-w)/2,(scrY-h)/2,w,h,TRUE);
}
void ResizeDlgClientSize(HWND hWnd,int w,int h)
{
RECT rc;
rc.left=rc.top=0;
rc.right=w;
rc.bottom=h;
AdjustWindowRectEx(&rc,GetWindowLong(hWnd, GWL_STYLE),GetMenu(hWnd)!=NULL,GetWindowLong(hWnd,GWL_EXSTYLE));
SetWindowPos(hWnd,NULL,0,0,rc.right-rc.left,rc.bottom-rc.top,SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE);
}
const RECT BaseRc[12] =
{
{61,0,65,4,},
{91,8,95,12,},
{114,30,118,34,},
{122,61,126,65,},
{114,92,118,96,},
{91,114,95,118,},
{61,122,65,126,},
{30,114,34,118,},
{8,91,12,95,},
{0,61,4,65,},
{8,30,12,34,},
{31,8,35,12,},
};
HINSTANCE g_hInstance = NULL;
class CWatch
{
public:
HWND m_hWnd;
HMENU m_hPopMenu;//右键弹出菜单的句柄。
HRGN m_hRgn;
HRGN m_hRgnBase;
HRGN m_hRgnHour;
HRGN m_hRgnMinute;
HRGN m_hRgnSecond;
POINT ptSecond[4];
POINT ptMinute[4];
POINT ptHour[4];
int m_WndInClientX,m_WndInClientY;
POINT m_MouseDownPt;
bool m_IfMoveWnd;
CWatch();
~CWatch();
void InitWatch(HWND hWnd);
void OnMouseMove(int flag,int x,int y);
void OnLButtonDown(int flag,int x,int y);
void OnLButtonUp(int flag,int x,int y);
void OnRButtonDown(int flag,int x,int y);
void Timer();
void OnEraseBkgnd(HWND hWnd,HDC hDc);
HPEN m_hPen1;
HPEN m_hPen2;
HBRUSH m_hBrush;
HPEN m_hPenPine[3][2];
HBRUSH m_hBrushPine[3][2];
void DrawPine(HDC hDc,POINT *pt,int index);
};
CWatch::CWatch()
{
m_IfMoveWnd = false;
m_hPopMenu = NULL;
m_hRgn = NULL;
m_hRgnBase = NULL;
m_hRgnHour = NULL;
m_hRgnMinute = NULL;
m_hRgnSecond = NULL;
m_hPen1 = CreatePen(PS_SOLID,0,RGB(0,255,255));
m_hPen2 = CreatePen(PS_SOLID,0,RGB(0,0,0));
m_hBrush = CreateSolidBrush( RGB(0,150,150) );
COLORREF c,color[3] = { RGB(230,60,60), RGB(163,103,60), RGB(60,128,50),};
for(int j=0;j<3;j++)
{
c = color[j];
m_hPenPine[j][0] = CreatePen(PS_SOLID,0,c);
m_hBrushPine[j][0] = CreateSolidBrush( c );
c = c-RGB(50,50,50);
m_hPenPine[j][1] = CreatePen(PS_SOLID,0,c);
m_hBrushPine[j][1] = CreateSolidBrush( c );
}
}
CWatch::~CWatch()
{
if( m_hPopMenu )DestroyMenu( m_hPopMenu );
if( m_hRgn )DeleteObject( m_hRgn );
if( m_hRgnBase )DeleteObject( m_hRgnBase );
if( m_hRgnHour )DeleteObject( m_hRgnHour );
if( m_hRgnMinute )DeleteObject( m_hRgnMinute );
if( m_hRgnSecond )DeleteObject( m_hRgnSecond );
DeleteObject( m_hPen1 );
DeleteObject( m_hPen2 );
DeleteObject( m_hBrush );
int i,j;
for(j=0;j<3;j++)for(i=0;i<2;i++)
{
DeleteObject( m_hPenPine[j][i] );
DeleteObject( m_hBrushPine[j][i] );
}
}
/*≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
□---时钟初始化处理。-------
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡*/
void CWatch::InitWatch(HWND hWnd)
{
m_hWnd = hWnd;
m_hPopMenu = LoadMenu(g_hInstance,MAKEINTRESOURCE(IDR_MENU_WATCH));
#ifdef CHINESE_LANGUAGE
ModifyMenu(m_hPopMenu, IDM_ABOUT, MF_STRING, IDM_ABOUT,"关于电脑助理");
ModifyMenu(m_hPopMenu, IDM_EXIT, MF_STRING, IDM_EXIT,"退出");
#endif
ResizeDlgClientSize(hWnd,126,126);
RECT rc;
GetWindowRect(hWnd, &rc);
MapWindowPoints(NULL, hWnd, (LPPOINT)&rc, 2);
int a=rc.left,b=rc.top;
m_WndInClientX=a;
m_WndInClientY=b;
int i;
m_hRgnBase = CreateRectRgn(BaseRc[0].left,BaseRc[0].top,BaseRc[0].right,BaseRc[0].bottom);
HRGN hRgn2;
for(i=1;i<12;i++)
{
hRgn2 = CreateRectRgn(BaseRc[i].left,BaseRc[i].top,BaseRc[i].right,BaseRc[i].bottom);
CombineRgn( m_hRgnBase,m_hRgnBase,hRgn2,RGN_OR );
DeleteObject( hRgn2 );
}
//hRgn2 = CreateEllipticRgn(57,57,71,71);
//CombineRgn( m_hRgnBase,m_hRgnBase,hRgn2,RGN_OR );
//DeleteObject( hRgn2 );
OffsetRgn(m_hRgnBase,-a,-b);
Timer();
CenterWindows(hWnd);
}
void CWatch::OnMouseMove(int flag,int x,int y)
{
//移动窗口。
if(m_IfMoveWnd)
{
POINT pt;
GetCursorPos(&pt);
RECT rc;
GetWindowRect(m_hWnd, &rc);
if(flag&MK_LBUTTON)
{
MoveWindow(m_hWnd,
pt.x-m_MouseDownPt.x+m_WndInClientX,
pt.y-m_MouseDownPt.y+m_WndInClientY,
rc.right-rc.left,
rc.bottom-rc.top,
TRUE);
}
}
}
void CWatch::OnLButtonDown(int flag,int x,int y)
{
//获得鼠标状态。
m_MouseDownPt.x=x;
m_MouseDownPt.y=y;
m_IfMoveWnd = true;
SetCapture(m_hWnd);
}
void CWatch::OnLButtonUp(int flag,int x,int y)
{
if(m_IfMoveWnd)
{
m_IfMoveWnd = false;
ReleaseCapture();
}
}
void CWatch::OnRButtonDown(int flag,int x,int y)
{
if(m_hPopMenu)
{
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = GetSubMenu (m_hPopMenu, 0);
TrackPopupMenu(
hMenu,
0,
pt.x,
pt.y,
0,
m_hWnd,
NULL);
}
}
void CWatch::DrawPine(HDC hDc,POINT *pt,int index)
{
SelectObject( hDc, m_hPenPine[index][0] );
SelectObject(hDc,m_hBrushPine[index][0]);
//绘制。
Polygon(hDc,pt,4);
SelectObject( hDc, m_hPenPine[index][1] );
SelectObject(hDc,m_hBrushPine[index][1]);
Polygon(hDc,pt,3);
}
#define ORIGIN_X 63//手表图案中心点的X坐标
#define ORIGIN_Y 63//手表图案中心点的Y坐标
#define RADIUS_HOUR 33//手表的时针长度
#define RADIUS_MINUTE 44//手表的分针长度
#define RADIUS_SECOND 50//手表的秒针长度
#define RADIAN 0.0174532925
void AddRgnLine(HRGN hRgn,long x1,long y1,long x2,long y2)
{
int x,y;
int m,n,dx,dy,x_inc,y_inc,i,error,offset;
error=0;
offset=(y1<<8)+x1;
dx=x2-x1;
dy=y2-y1;
if(dx>=0)x_inc=1;
else
{
x_inc=-1;
dx=-dx;
}
if(dy>=0)y_inc=256;
else
{
y_inc=-256;
dy=-dy;
}
m=(dx>>1);n=(dy>>1);
if(dx>dy)
{
for(i=0;i<=dx;i++)
{
{
x= (offset&0x00ff); y = (offset>>8);
HRGN hRgn1 = CreateRectRgn(x,y,x+1,y+1);
CombineRgn( hRgn,hRgn,hRgn1,RGN_OR );
DeleteObject( hRgn1 );
}
error+=dy;
if(error>m)
{
error-=dx;
offset+=y_inc;
}
offset+=x_inc;
}
}
else
{
for(i=0;i<=dy;i++)
{
{
x= (offset&0x00ff); y = (offset>>8);
HRGN hRgn1 = CreateRectRgn(x,y,x+1,y+1);
CombineRgn( hRgn,hRgn,hRgn1,RGN_OR );
DeleteObject( hRgn1 );
}
error+=dx;
if(error>n)
{
error-=dy;
offset+=x_inc;
}
offset+=y_inc;
}
}
}
void CWatch::Timer()
{
double angle;
//得到时间。
SYSTEMTIME Time;
GetLocalTime(&Time);
//得到秒针的端点坐标。
angle=(Time.wSecond*6)*RADIAN;
ptSecond[0].x = (int)(RADIUS_SECOND*sin(angle))+ORIGIN_X;
ptSecond[0].y = (int)(RADIUS_SECOND*(-cos(angle)))+ORIGIN_Y;
double aa = 1.4*cos(angle);
double bb = 1.4*sin(angle);
int mm = (int)aa;
if(aa - mm > 0.5) mm++;
int nn = (int)bb;
if(bb - nn > 0.5) nn++;
ptSecond[1].x = mm+ORIGIN_X;
ptSecond[1].y = nn+ORIGIN_Y;
ptSecond[2].x = ORIGIN_X-(int)(6*sin(angle));
ptSecond[2].y = ORIGIN_Y+(int)(6*cos(angle));
ptSecond[3].x = -ptSecond[1].x +2*ORIGIN_X;
ptSecond[3].y = -ptSecond[1].y +2*ORIGIN_Y;
//得到分针的端点坐标。
angle = Time.wMinute*6*RADIAN;
ptMinute[0].x = (int)(RADIUS_MINUTE*sin(angle))+ORIGIN_X;
ptMinute[0].y = (int)(RADIUS_MINUTE*(-cos(angle)))+ORIGIN_Y;
ptMinute[1].x = (int)(5*cos(angle))+ORIGIN_X;
ptMinute[1].y = (int)(5*sin(angle))+ORIGIN_Y;
ptMinute[3].x = -ptMinute[1].x +2*ORIGIN_X;
ptMinute[3].y = -ptMinute[1].y +2*ORIGIN_Y;
ptMinute[2].x = ORIGIN_X-(int)(10*sin(angle));
ptMinute[2].y = ORIGIN_Y+(int)(10*cos(angle));
//得到时针的端点坐标。
angle = (Time.wHour%12*30+(Time.wMinute/12)*6)*RADIAN;
ptHour[0].x = (int)(RADIUS_HOUR*sin(angle))+ORIGIN_X;
ptHour[0].y = (int)(RADIUS_HOUR*(-cos(angle)))+ORIGIN_Y;
ptHour[
Windows指针时钟程序
5星 · 超过95%的资源 需积分: 31 185 浏览量
2010-01-16
20:03:57
上传
评论
收藏 27KB RAR 举报
dadahacker
- 粉丝: 77
- 资源: 10