www.pudn.com > myclock.rar > main.cpp


#include  
#include  
#include "resource.h" 
#include "main.h" 
 
//***************************** 
//程序设计:李继飞 
//设计时间:2005年10月24日 
//邮箱:dasecom@sina.com 
//***************************** 
 
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) 
{ 
	char *szClassName = "MainClass"; 
 
	WNDCLASSEX wndclass; 
	wndclass.cbSize = sizeof(wndclass); 
	wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 
	wndclass.lpfnWndProc = WndProc; 
	wndclass.cbClsExtra = 0; 
	wndclass.cbWndExtra = 0; 
	wndclass.hIcon = LoadIcon(hInstance,LPCTSTR(IDI_ICON1)); 
	wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); 
	wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); 
	wndclass.hInstance = hInstance; 
	wndclass.lpszClassName = szClassName; 
	wndclass.lpszMenuName = NULL; 
	wndclass.hIconSm = NULL; 
	 
	//注册窗口类 
	::RegisterClassEx(&wndclass); 
 
	//创建窗口 
	HWND hwnd = ::CreateWindowEx(0,						//扩展样式 
								szClassName,         //类名 
								"时钟",                  //窗口标题 
								WS_POPUP | WS_SYSMENU | WS_SIZEBOX, //窗口风格 
								50,  //默认 X 坐标 
								50,  //默认 Y 坐标 
								300,  //默认宽度 
								300,  //默认高度 
								NULL,			//父窗口句柄 
								NULL,			//菜单句柄 
								hInstance,		//程序实例句柄 
								NULL);			//用户数据 
 
	if(hwnd == NULL) 
	{ 
		MessageBox(NULL,"创建窗口出错","error",MB_OK); 
		return -1; 
	} 
 
	//显示窗口 
	::ShowWindow(hwnd,nCmdShow); 
	::UpdateWindow(hwnd); 
 
	//进入消息循环,把消息取出交给消息处理函数处理 
	MSG msg; 
	while(::GetMessage(&msg,NULL,0,0)) 
	{ 
		::TranslateMessage(&msg); 
		::DispatchMessage(&msg); 
	} 
 
	//当 
	return msg.wParam; 
} 
 
 
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) 
{ 
	HDC hdc; 
	PAINTSTRUCT ps; 
	RECT rt; 
	UINT_PTR IDT_CLOCK = 1;  //计时器ID号 
	const int IDM_TOPMOST = 100; 
	const int IDM_ABOUT = 101; 
	switch(msg) 
	{ 
	case WM_CREATE: 
		{ 
			SYSTEMTIME time; 
			//取得系统当前时间 
			::GetLocalTime(&time); 
			if(time.wHour < 12) 
			{ 
				nHour = time.wHour; 
			} 
			else 
			{ 
				nHour = time.wHour -12; 
			} 
			nMinute = time.wMinute; 
			nSecond = time.wSecond; 
 
			//创建计时器 
			::SetTimer(hwnd,IDT_CLOCK,1000,NULL); 
 
 
			//向系统菜单添加自定义的项 
			HMENU hMenu; 
			 
			hMenu = ::GetSystemMenu(hwnd,FALSE); 
			::AppendMenu(hMenu,MF_SEPARATOR,0,NULL); 
			::AppendMenu(hMenu,MF_STRING,IDM_TOPMOST,"总在最前面"); 
			::AppendMenu(hMenu,MF_STRING,IDM_ABOUT,"关于"); 
			return 0; 
		} 
	 
	case WM_CLOSE: 
		{ 
			//删除计时器 
			KillTimer(hwnd,IDT_CLOCK); 
			//结束程序 
			::DestroyWindow(hwnd); 
			return 0; 
		} 
	case WM_PAINT: 
		{ 
			hdc = ::BeginPaint(hwnd,&ps); 
			::GetClientRect(hwnd,&rt); 
			SetIsotropic(hdc,rt.right - rt.left,rt.bottom - rt.top); 
			DrawClockFace(hdc); 
			//一小时走30度 
			DrawHand(hdc,250,8,nHour * 30 + int((double)nMinute * (30.000 / 60.000)),RGB(0,0,0)); 
			//一分钟走6度 
			DrawHand(hdc,350,6,nMinute * 6,RGB(0,0,0)); 
			//一秒钟走6度 
			DrawHand(hdc,420,4,nSecond * 6,RGB(0,0,0)); 
			::EndPaint(hwnd,&ps); 
			return 0; 
		} 
	case WM_SIZE: 
		{ 
			cxClient = LOWORD(lParam); 
			cyClient = HIWORD(lParam); 
		} 
	case WM_TIMER:   //计时器消息处理 
		{ 
			if(::IsIconic(hwnd))  //IsIconic 函数用来判断窗口是否处于最小化状态 
			{ 
				return 0; 
			} 
 
			//建立坐标系 
			HDC hdc = ::GetDC(hwnd); 
			SetIsotropic(hdc,cxClient,cyClient); 
			SYSTEMTIME time; 
			::GetLocalTime(&time); 
			COLORREF clrColor = ::GetSysColor(COLOR_3DFACE); 
 
			//把原来的指针重画成与背景颜色相同,等于擦除了指针 
			//一小时走30度 
			DrawHand(hdc,250,8,nHour * 30 + int((double)nMinute * (30.000 / 60.000)),clrColor); 
			//一分钟走6度 
			DrawHand(hdc,350,6,nMinute * 6,clrColor); 
			//一秒钟走6度 
			DrawHand(hdc,420,4,nSecond * 6,clrColor); 
			 
			//重设所有时间 
			if(time.wHour < 12) 
			{ 
				nHour = time.wHour; 
			} 
			else 
			{ 
				nHour = time.wHour -12; 
			} 
			nMinute = time.wMinute; 
			nSecond = time.wSecond; 
 
 
			//重画所有指针 
			//一小时走30度 
			DrawHand(hdc,250,8,nHour * 30 + int((double)nMinute * (30.000 / 60.000)),RGB(0,0,0)); 
			//一分钟走6度 
			DrawHand(hdc,350,6,nMinute * 6,RGB(0,0,0)); 
			//一秒钟走6度 
			DrawHand(hdc,420,4,nSecond * 6,RGB(0,0,0)); 
			return 0; 
		} 
	case WM_NCHITTEST:  //处理鼠标单击消息 
		UINT nHitTest; 
		nHitTest = ::DefWindowProc(hwnd,msg,wParam,lParam); 
		//如果鼠标按下,GetAsyncKeyState 函数的返回值小于 0 
		if(nHitTest == HTCLIENT && ::GetAsyncKeyState(MK_LBUTTON) < 0) 
			nHitTest = HTCAPTION; 
		return nHitTest; 
	case WM_CONTEXTMENU: 
		{ 
			POINT pt; 
			pt.x = LOWORD(lParam); 
			pt.y = HIWORD(lParam); 
			//取得系统菜单句柄 
			HMENU hMenu = ::GetSystemMenu(hwnd,FALSE); 
 
			//弹出系统菜单 
			int nID = ::TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_RETURNCMD,pt.x,pt.y,0,hwnd,NULL); 
			if(nID > 0) 
				::SendMessage(hwnd,WM_SYSCOMMAND,nID,0); 
			return 0; 
		} 
 
	case WM_SYSCOMMAND: 
		{		 
			int nID = wParam; 
			if(nID == IDM_ABOUT) 
			{ 
				::MessageBox(hwnd,"飘云时钟 v1.0\n程序设计:李继飞\nQQ:617784446","clock",MB_OK); 
			} 
			else if(nID == IDM_TOPMOST) 
			{ 
				HMENU hMenu = ::GetSystemMenu(hwnd,FALSE); 
				if(bTopMost) 
				{ 
					//设置ID号为 IDM_TOPMOST 的菜单为未选中状态 
					::CheckMenuItem(hMenu,IDM_TOPMOST,MF_UNCHECKED); 
 
					//取消窗口置顶的状态 
					::SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE); 
					bTopMost = FALSE; 
				} 
				else 
				{ 
					//设置ID号为 IDM_TOPMOST 的菜单为选中状态 
					::CheckMenuItem(hMenu,IDM_TOPMOST,MF_CHECKED); 
					//将窗口提到所有的窗口最前面 
					::SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE); 
					bTopMost = TRUE; 
				} 
			} 
			return ::DefWindowProc(hwnd,WM_SYSCOMMAND,nID,0); 
		} 
		 
	case WM_DESTROY: 
		{ 
			::PostQuitMessage(0); 
			return 0; 
		} 
	} 
	return ::DefWindowProc(hwnd,msg,wParam,lParam); 
} 
 
int SetIsotropic(HDC hdc,int cx,int cy) 
{ 
	::SetMapMode(hdc,MM_ISOTROPIC); 
	::SetWindowExtEx(hdc,1000,1000,NULL); 
	::SetViewportExtEx(hdc,cx,-cy,NULL); 
	::SetViewportOrgEx(hdc,cx / 2,cy / 2,NULL); 
	return 0; 
} 
 
void DrawClockFace(HDC hdc) 
{ 
	const int SQUARESIZE = 15; 
	int x,y; 
 
	//选择一个黑色的画刷 
	SelectObject(hdc,::GetStockObject(BLACK_BRUSH)); 
 
	//将角度转化成弧度 2 * 3.141592 / 360 = 0.0174533; 
	double nRadians; 
	for(int i = 0; i < 360; i += 6) 
	{ 
		nRadians = (double)i * 0.0174533; 
		x = (int)(450 * sin(nRadians)); 
		y = (int)(450 * cos(nRadians)); 
		if( i % 30  ==  0) 
		{ 
			::Ellipse(hdc,x - SQUARESIZE , y + SQUARESIZE , x + SQUARESIZE , y - SQUARESIZE); 
		} 
		else 
		{ 
			::Ellipse(hdc,x - 7 , y + 7 , x + 7 , y - 7); 
		} 
	} 
} 
 
void DrawHand(HDC hdc,int nLength,int nWidth,int nDegrees,COLORREF clrColor) 
{ 
	//将角度转化成弧度 2 * 3.1415926 / 360 = 0.0174533; 
	double nRadians = (double)nDegrees * 0.0174533; 
 
	//获得坐标的值 
	POINT pt[2]; 
	pt[0].x = int(nLength * sin(nRadians)); 
	pt[0].y = int (nLength * cos(nRadians)); 
	pt[1].x = -pt[0].x / 5; 
	pt[1].y = -pt[0].y / 5; 
 
	//创建画笔 
	HPEN hPen = ::CreatePen(PS_SOLID,nWidth,clrColor); 
	HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen); 
 
	//画线 
	::MoveToEx(hdc,pt[0].x,pt[0].y,NULL); 
	::LineTo(hdc,pt[1].x,pt[1].y); 
	::SelectObject(hdc,hOldPen); 
	 
	//删除画笔 
	::DeleteObject(hPen); 
}