自己都觉得,主要是其中的类型问题!
#include <stdafx.h>#define window_class_name "clock"
HWND main_window_handle;
HINSTANCE main_instance;LRESULT CALLBACK WinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
CTime time;
CSize size;
RECT rect;
PAINTSTRUCT ps;
HDC hdc;
CString str;
int centerx;
int centery;
double Radians;
switch(msg)
{
case WM_CREATE:
SetTimer(hwnd,TIMER_ID1,1000,NULL);
GetClientRect(main_window_handle,&rect);
break; case WM_TIMER:
time=CTime::GetCurrentTime();
break; case WM_PAINT:
hdc=BeginPaint(main_window_handle,&ps);
centerx=rect.right/2;
centery=rect.bottom/2;
CPen Pen(PS_SOLID,5,RGB(255,255,0));
CPen *oldpen=SelectObject(hdc,&Pen);
Ellipse(hdc,5,5,rect.right-5,rect.bottom-5);
SetTextColor(hdc,RGB(255,0,0));
for(int i=0;i<=12;i++)
{
str.Format("%d",i);
size=GetTextExtent(hdc,str,str.GetLength());
Radians=(double)i*6.28/12.0;
x=centerx-(size.cx/2)+(int)((double)(centerx-20)*sin(Radians));
y=centery-(size.cy/2)-(int)((double)(centery-20)*cos(Radians));
TextOut(hdc,x,y,str);
}
Radians=(double)time.GetHour()+(double)time.GetMinute()/60.0+(double)time.GetSecond()/3600.0;
Radians*=6.28/12.0;
CPen hourpen(PS_SOLID,5,RGB(0,255,0));
SelectObject(hdc,&hourpen);
MoveTo(centerx,centery);
LineTo(hdc,centerx+(int)((double)(centerx/3)*sin(Radians)),centery-(int)((double)(centery/3)*cos(Radians))); Radians=(double)time.GetMinute()+(double)time.GetSecond()/60.0;
Radians*=6.28/60.0;
CPen minpen(PS_SOLID,3,RGB(0,0,255));
SelectObject(hdc,&minpen);
MoveTo(centerx,centery);
LineTo(hdc,centerx+(int)((double)(centerx*2/3)*sin(Radians)),centery-(int)((double)(centery*2/3)*cos(Radians))); Radians=(double)time.GetSecond();
Radians*=6.28/60.0;
CPen secpen(PS_SOLID,1,RGB(0,255,255));
SelectObject(hdc,&secpen);
MoveTo(centerx,centery);
LineTo(hdc,centerx+(int)((double)(centerx*4/5)*sin(Radians)),centery-(int)((double)(centery*4/5)*cos(Radians))); SelectObject(hdc,oldpen);
EndPaint(main_window_handle,&ps);
break;
default:
return(DefWindowProc(hwnd,msg,wparam,lparam));
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASS winclass;
HWND hwnd;
MSG msg; winclass.style=0;
winclass.cbClsExtra=0;
winclass.cbWndExtra=0;
winclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hCursor=LoadCursor(NULL,IDC_ARROW);
winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
winclass.lpfnWndProc=WinProc;
winclass.hInstance=hInstance;
winclass.lpszClassName=window_class_name;
winclass.lpszMenuName=NULL; if(!RegisterClass(&winclass))
return (0); if(!(hwnd=CreateWindow(window_class_name,
"clock",
WS_OVERLAPPEDWINDOW,
0,0,
300,300,
NULL,
NULL,
hInstance,
NULL)))
return (0);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd); main_window_handle=hwnd;
main_instance=hInstance;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
#include <stdafx.h>#define window_class_name "clock"
HWND main_window_handle;
HINSTANCE main_instance;LRESULT CALLBACK WinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
CTime time;
CSize size;
RECT rect;
PAINTSTRUCT ps;
HDC hdc;
CString str;
int centerx;
int centery;
double Radians;
switch(msg)
{
case WM_CREATE:
SetTimer(hwnd,TIMER_ID1,1000,NULL);
GetClientRect(main_window_handle,&rect);
break; case WM_TIMER:
time=CTime::GetCurrentTime();
break; case WM_PAINT:
hdc=BeginPaint(main_window_handle,&ps);
centerx=rect.right/2;
centery=rect.bottom/2;
CPen Pen(PS_SOLID,5,RGB(255,255,0));
CPen *oldpen=SelectObject(hdc,&Pen);
Ellipse(hdc,5,5,rect.right-5,rect.bottom-5);
SetTextColor(hdc,RGB(255,0,0));
for(int i=0;i<=12;i++)
{
str.Format("%d",i);
size=GetTextExtent(hdc,str,str.GetLength());
Radians=(double)i*6.28/12.0;
x=centerx-(size.cx/2)+(int)((double)(centerx-20)*sin(Radians));
y=centery-(size.cy/2)-(int)((double)(centery-20)*cos(Radians));
TextOut(hdc,x,y,str);
}
Radians=(double)time.GetHour()+(double)time.GetMinute()/60.0+(double)time.GetSecond()/3600.0;
Radians*=6.28/12.0;
CPen hourpen(PS_SOLID,5,RGB(0,255,0));
SelectObject(hdc,&hourpen);
MoveTo(centerx,centery);
LineTo(hdc,centerx+(int)((double)(centerx/3)*sin(Radians)),centery-(int)((double)(centery/3)*cos(Radians))); Radians=(double)time.GetMinute()+(double)time.GetSecond()/60.0;
Radians*=6.28/60.0;
CPen minpen(PS_SOLID,3,RGB(0,0,255));
SelectObject(hdc,&minpen);
MoveTo(centerx,centery);
LineTo(hdc,centerx+(int)((double)(centerx*2/3)*sin(Radians)),centery-(int)((double)(centery*2/3)*cos(Radians))); Radians=(double)time.GetSecond();
Radians*=6.28/60.0;
CPen secpen(PS_SOLID,1,RGB(0,255,255));
SelectObject(hdc,&secpen);
MoveTo(centerx,centery);
LineTo(hdc,centerx+(int)((double)(centerx*4/5)*sin(Radians)),centery-(int)((double)(centery*4/5)*cos(Radians))); SelectObject(hdc,oldpen);
EndPaint(main_window_handle,&ps);
break;
default:
return(DefWindowProc(hwnd,msg,wparam,lparam));
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASS winclass;
HWND hwnd;
MSG msg; winclass.style=0;
winclass.cbClsExtra=0;
winclass.cbWndExtra=0;
winclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hCursor=LoadCursor(NULL,IDC_ARROW);
winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
winclass.lpfnWndProc=WinProc;
winclass.hInstance=hInstance;
winclass.lpszClassName=window_class_name;
winclass.lpszMenuName=NULL; if(!RegisterClass(&winclass))
return (0); if(!(hwnd=CreateWindow(window_class_name,
"clock",
WS_OVERLAPPEDWINDOW,
0,0,
300,300,
NULL,
NULL,
hInstance,
NULL)))
return (0);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd); main_window_handle=hwnd;
main_instance=hInstance;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
GetTextExtent改为
CDC *pdc = CDC::FromHandle(hdc);
size=pdc->GetTextExtent(str,str.GetLength());
其中GetTextExtent, MoveTo一类的函数都是CDC的成员函数,需要得到响应的CDC,具体参考MSDNCPen *oldpen=SelectObject(hdc,&Pen);改为
CPen *oldpen=(CPen*)SelectObject(hdc,&Pen);
//#include <stdafx.h>
//#include <afx.h>
#include "math.h"
#define window_class_name "clock"
HWND main_window_handle;
HINSTANCE main_instance;
CTime g_time;LRESULT CALLBACK WinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
CSize size;
RECT rect = {0};
PAINTSTRUCT ps = {0};
HDC hdc = NULL;
CString str;
int centerx = 0;
int centery = 0;
double Radians = 0; switch(msg)
{
case WM_CREATE:
SetTimer(hwnd,1,1000,NULL);
g_time=CTime::GetCurrentTime();
GetClientRect(main_window_handle,&rect);
break; case WM_TIMER:
g_time=CTime::GetCurrentTime();
break; case WM_PAINT:
{
INT x = 0, y = 0;
hdc=BeginPaint(hwnd,&ps);
CDC *pdc = CDC::FromHandle(hdc);
centerx=rect.right/2;
centery=rect.bottom/2;
CPen Pen(PS_SOLID,5,RGB(255,255,0));
CPen *oldpen=(CPen*)SelectObject(hdc,&Pen);
Ellipse(hdc,5,5,rect.right-5,rect.bottom-5);
SetTextColor(hdc,RGB(255,0,0));
for(int i=0;i<=12;i++)
{
str.Format("%d",i);
CDC *pdc = CDC::FromHandle(hdc);
size=pdc->GetTextExtent(str,str.GetLength());
Radians=(double)i*6.28/12.0;
x=(INT)(centerx-(size.cx/2)+(int)((double)(centerx-20)*sin(Radians)));
y=(INT)(centery-(size.cy/2)-(int)((double)(centery-20)*cos(Radians)));
TextOut(hdc,x,y,str, str.GetLength());
}
Radians=(double)g_time.GetHour()+(double)g_time.GetMinute()/60.0+(double)g_time.GetSecond()/3600.0;
Radians*=6.28/12.0;
CPen hourpen(PS_SOLID,5,RGB(0,255,0));
SelectObject(hdc,&hourpen);
pdc->MoveTo(centerx,centery);
pdc->LineTo(centerx+(int)((double)(centerx/3)*sin(Radians)),centery-(int)((double)(centery/3)*cos(Radians))); Radians=(double)g_time.GetMinute()+(double)g_time.GetSecond()/60.0;
Radians*=6.28/60.0;
CPen minpen(PS_SOLID,3,RGB(0,0,255));
SelectObject(hdc,&minpen);
pdc->MoveTo(centerx,centery);
pdc->LineTo(centerx+(int)((double)(centerx*2/3)*sin(Radians)),centery-(int)((double)(centery*2/3)*cos(Radians))); Radians=(double)g_time.GetSecond();
Radians*=6.28/60.0;
CPen secpen(PS_SOLID,1,RGB(0,255,255));
SelectObject(hdc,&secpen);
pdc->MoveTo(centerx,centery);
pdc->LineTo(centerx+(int)((double)(centerx*4/5)*sin(Radians)),centery-(int)((double)(centery*4/5)*cos(Radians))); SelectObject(hdc,oldpen);
EndPaint(main_window_handle,&ps);
}
break;
default:
return(DefWindowProc(hwnd,msg,wparam,lparam));
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASS winclass;
HWND hwnd;
MSG msg; winclass.style=0;
winclass.cbClsExtra=0;
winclass.cbWndExtra=0;
winclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hCursor=LoadCursor(NULL,IDC_ARROW);
winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
winclass.lpfnWndProc=WinProc;
winclass.hInstance=hInstance;
winclass.lpszClassName=window_class_name;
winclass.lpszMenuName=NULL; if(!RegisterClass(&winclass))
return (0); if(!(hwnd=CreateWindow(window_class_name,
"clock",
WS_OVERLAPPEDWINDOW,
0,0,
300,300,
NULL,
NULL,
hInstance,
NULL)))
return (0);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd); main_window_handle=hwnd;
main_instance=hInstance;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}真是一塌糊涂,修改后的
D:\Vc\MSDev98\myproject\clock\clock.cpp(7) : error C2501: 'CTime' : missing storage-class or type specifiers
D:\Vc\MSDev98\myproject\clock\clock.cpp(7) : fatal error C1004: unexpected end of file found
Error executing cl.exe.clock.obj - 3 error(s), 0 warning(s)
是不是没有包含CTime的头文件?
//#include <windows.h>
#include <afxwin.h> // MFC core and standard components2.在编译设置中C/C++页,code generation中的use run time library选debug multiThreaded
在编译设置中C/C++页,code generation中的use run time library选debug multiThreaded
2.#include "time.h"
试试