vs2010运行这段代码有问题,窗口注册那一句,求解释?
#include<windows.h>
#include<tchar.h>
#include<math.h>
typedef struct Time
{
int hour,min,sec;
}TimeStructure;
BOOLEAN InitWindowClass(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void AdjustTime(TimeStructure *x);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow);
char str[80];
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if(!InitWindowClass(hInstance,nCmdShow))
{
MessageBox(NULL,L"窗口创建失败!",_T("创建窗口"),NULL);
return 1;
}
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hDc;
PAINTSTRUCT ps;
HBRUSH hBrush;
HPEN hPen;
RECT clientRect;
static TimeStructure x;
float sita = 0;
int xOrg,yOrg,rSec,rMin,rHour,rClock,xBegin,xEnd,yBegin,yEnd;
switch(message)
{
case WM_CREATE:
SetTimer(hWnd,9999,1000,NULL);
break;
case WM_PAINT:
x.sec++;
AdjustTime(&x);
hDc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&clientRect);///////////////////////////////////
hPen=(HPEN)GetStockObject(BLACK_PEN);
hBrush=CreateSolidBrush(RGB(255,220,220));
SelectObject(hDc,hPen);
SelectObject(hDc,hBrush);
xOrg = (clientRect.left+clientRect.right)/2;
yOrg = (clientRect.top+clientRect.bottom)/2;
rClock=min(xOrg,yOrg)-50;
rSec=rClock*6/7;
rMin=rClock*5/6;
rHour=rClock*2/3;
Ellipse(hDc,xOrg-rClock,yOrg-rClock,xOrg+rClock,yOrg+rClock);
for(int i=0; i<60; i++)
{
if(i%5)
{
hPen = CreatePen (PS_SOLID,2,RGB(255,0,0));
SelectObject(hDc, hPen);
xBegin = xOrg+(int)(rClock*sin(2*3.1415926*i/60));
yBegin = yOrg+(int)(rClock*cos(2*3.1415926*i/60));
MoveToEx(hDc,xBegin,yBegin,NULL);
xEnd = xOrg+(int)((rClock-20)*sin(2*3.1415926*i/60));
yEnd = yOrg+(int)((rClock-20)*cos(2*3.1415926*i/60));
}
else
{
hPen = CreatePen(PS_SOLID,5,RGB(255,0,0));
SelectObject(hDc,hPen);
xBegin = xOrg+(int)(rClock*sin(2*3.1415926*i/60));
yBegin = yOrg+(int)(rClock*cos(2*3.1415926*i/60));
MoveToEx(hDc,xBegin,yBegin,NULL);
xEnd = xOrg+(int)((rClock-25)*sin(2*3.1415926*i/60));
yEnd = yOrg+(int)((rClock-25)*cos(2*3.1415926*i/60));
}
LineTo(hDc,xEnd,yEnd);
DeleteObject(hPen);
}
hPen = CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hDc,hPen);
sita = (float)(2*3.1415926*x.sec/60);
xBegin = xOrg+(int)(rSec*sin(sita));
yBegin = yOrg-(int)(rSec*cos(sita));
xEnd = xOrg+(int)(rClock*sin(sita+3.1415926)/8);
yEnd = yOrg+(int)(rClock*cos(sita+3.1415926)/8);
MoveToEx(hDc,xBegin,yBegin,NULL);
LineTo(hDc,xEnd,yEnd);
hPen = CreatePen(PS_SOLID,5,RGB(0,0,0));
SelectObject(hDc,hPen);
sita=(float)(2*3.1415926*x.min/60);
xBegin = xOrg+(int)(rClock*sin(sita+3.1415926)/8);
yBegin = yOrg-(int)(rClock*cos(sita+3.1415926)/8);
MoveToEx(hDc,xBegin,yBegin,NULL);
LineTo(hDc,xEnd,yEnd);
hPen = CreatePen(PS_SOLID,10,RGB(0,0,0));
SelectObject(hDc,hPen);
sita=(float)(2*3.1415926*x.hour/12);
xBegin = xOrg+(int)(rHour*sin(sita));
yBegin = yOrg-(int)(rHour*cos(sita));
xEnd = xOrg+(int)(rClock*sin(sita+3.1415926)/8);
yEnd = yOrg-(int)(rClock*cos(sita+3.1415926)/8);
MoveToEx(hDc,xBegin,yBegin,NULL);
LineTo(hDc,xEnd,yEnd);
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hWnd,&ps);
break;
case WM_TIMER:
if(wParam ==9999)
InvalidateRect(hWnd,NULL,true);
break;
case WM_SIZE:
InvalidateRect(hWnd,NULL,true);
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
break;
}
return 0;
}
void AdjustTime(TimeStructure *x)
{
if(x->sec==60)
{
x->sec=0;
x->min++;
if(x->min==60)
{
x->min=0;
x->hour++;
if(x->hour==12)
x->hour=0;
}
}
}
BOOLEAN InitWindowClass(HINSTANCE hInstance,int nCmdShow)
{
WNDCLASSEX wcex;
HWND hWnd;
TCHAR szWindowClass[] =L"窗口示例";
TCHAR szTitle[]=L"模拟时钟";
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style =0;
wcex.lpfnWndProc =WndProc;
wcex.cbClsExtra =0;
wcex.cbWndExtra =0;
wcex.hInstance =hInstance;
wcex.hIcon =LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor =LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpfnWndProc =NULL;
wcex.lpszClassName =szWindowClass;
wcex.hIconSm =LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
if(!RegisterClassEx(&wcex))//////////////////////////////////错误
return false;
hWnd=CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return false;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return true;
}
[解决办法]
看上去没有明显的错,,,你还是在if(!RegisterClassEx(&wcex))后面 GetLastError 输出一下错误看看吧