windows程序设计绘制模拟时钟
各位大侠好,本人初学windows程序设计 现在看到Gdi书上有个例题是绘制一个模拟时钟代码如下 但是我运行完,不显示时针,麻烦给为大侠给小弟我看下,急用。
#include<windows.h>
#include<time.h>
#include<math.h>
#include<stdio.h>
//时间
static int s_nprehour;
static int s_npreminute;
static int s_npresecond;
//窗口客户区的大小
static int s_cxclient;
static int s_cyclient;
//是否位于最顶层
static BOOL s_btopmost;
#define IDT_CLOCK 22
LRESULT _stdcall MainWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void DrawClockFace(HDC hDc);
void SeIsotropic(HDC hDc,int cx,int cy);
void DrawHand(HDC hDc,int nlength,int nwidth,int ndegress,COLORREF clrcolor);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
char szClassName[]="mainwclass";
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=MainWndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=::LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szClassName;
wndclass.hIconSm=NULL;
::RegisterClassEx(&wndclass);
HWND hwnd=::CreateWindowEx(
0,
szClassName,
"my first window !",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
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 _stdcall MainWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
SYSTEMTIME time;
::GetLocalTime(&time);
s_nprehour=time.wHour%12;
s_npreminute=time.wMinute;
s_npresecond=time.wSecond;
::SetTimer(hWnd,IDT_CLOCK,1000,NULL);
return 0;
}
case WM_SIZE:
{
s_cxclient=LOWORD(lParam);
s_cyclient=HIWORD(lParam);
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc=::BeginPaint(hWnd,&ps);
// she zhi zuo biao xi
SeIsotropic(hdc,s_cxclient,s_cyclient);
//hui zhi shi zhong wai guan
DrawClockFace(hdc);
DrawHand(hdc,200,8,s_nprehour*30+s_npreminute/2,RGB(255,0,0));
DrawHand(hdc,400,6,s_npreminute*6,RGB(255,0,0));
DrawHand(hdc,400,1,s_npresecond*6,RGB(255,0,0));
::EndPaint(hWnd,&ps);
return 0;
}
case WM_CLOSE:
{
::KillTimer(hWnd,IDT_CLOCK);
::DestroyWindow(hWnd);
return 0;
}
case WM_DESTROY:
{
::PostQuitMessage(0);
return 0;
}
}
return ::DefWindowProc(hWnd,message,wParam,lParam);
}
void SeIsotropic(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);
}
void DrawClockFace(HDC hDc)
{
const int SQUARESIZE=20;
static POINT pt[]=
{
0,450,
225,390,
390,225,
450,0,
390,-225,
225,-390,
0,-450,
-225,-390,
-390,-225,
-450,0,
-390,225,
-225,390
};
::SelectObject(hDc,::GetStockObject(BLACK_BRUSH));
for(int i=0;i<12;i++)
{
::Ellipse(hDc,pt[i].x-SQUARESIZE,pt[i].y+SQUARESIZE,
pt[i].x+SQUARESIZE,pt[i].y-SQUARESIZE);
}
}
void DrawHand(HDC hDc,int nlength,int nwidth,int ndegress,COLORREF clrcolor)
{
double nradians=(double)ndegress*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[1].x,pt[1].y,NULL);
::LineTo(hDc,pt[1].x,pt[1].y);
::SelectObject(hDc,hOldPen);
::DeleteObject(hPen);
}