#include <windows.h>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=0;
wc.lpszMenuName="myhello";
wc.lpszClassName="myhello";
if(!RegisterClass(&wc))
return FALSE;
hWnd=CreateWindow("myhello","hello.c",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rect;
PAINTSTRUCT ps;
HDC hdc;
static int uRed=0,uGreen=0,uBlue=0;
switch(msg)
{
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
SetTextColor(hdc,RGB(uRed,uGreen,uBlue));
DrawText(hdc,TEXT("Hello World!!"),-1,&rect,0);
EndPaint(hWnd,&ps);
break;
case WM_KEYDOWN:
uRed=rand()%255;
uGreen=rand()%255;
uBlue=rand()%255;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0L;
}错误:
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/hello.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=0;
wc.lpszMenuName="myhello";
wc.lpszClassName="myhello";
if(!RegisterClass(&wc))
return FALSE;
hWnd=CreateWindow("myhello","hello.c",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rect;
PAINTSTRUCT ps;
HDC hdc;
static int uRed=0,uGreen=0,uBlue=0;
switch(msg)
{
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
SetTextColor(hdc,RGB(uRed,uGreen,uBlue));
DrawText(hdc,TEXT("Hello World!!"),-1,&rect,0);
EndPaint(hWnd,&ps);
break;
case WM_KEYDOWN:
uRed=rand()%255;
uGreen=rand()%255;
uBlue=rand()%255;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0L;
}错误:
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/hello.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
或用向导生成 Win32 Application 空白的工程之后把你的文件加入都行。
/subsystem:console
改成
/subsystem:windows
Alt+F7,link选项~
程序)。
当然改编译器选项自然也可以。