#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);//函數的申明
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinProc;
wndcls.lpszClassName="sunxin2006";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);//設計一個窗口類
HWND hwnd;
hwnd=CreateWindow("sunxin2006","http://www.sunxin.org",WS_OVERLAPPEDWINDOW,
0,0,600,600,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wParam);//當消息爲字符時,將wParam的字符寫入szChar
MessageBox(hwnd,szChar,"char",0);//第三個參數爲消息盒子的名字
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);//獲得當前窗口句柄的設備描述表
TextOut(hdc,0,50,"程序猿之家",strlen("程序猿之家"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www.sunxin.org"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的結束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
Win32 Application和Win32 Console Application 都是工作在32位Windows環境的程序。
其中Win32 Application 就是普通的常見的窗口應用程序,當然有的界面做得比較個性化,比如
圓形的、不規則形狀的…… 它們都是所謂的GUI(Graphics User Interface圖形用戶接口),我們
可以通過鼠標點擊來完成控制。 Win32 Application是爲你開發windows應用程序所準備的,程序
以WinMain()爲入口,#include<windows.h>,能夠使用win32 API函數。
而Win32 Console Application(win32控制檯應用程序)往往是像MS-DOS窗口(XP中叫命令提示
符)的樣子出現,我們得用鍵盤輸入各種命令來使用它,它與純dos程序區別是:它是32位的;
或者叫CUI(Character User Interface字符用戶接口)。 console程序以main()爲入口,不能使用
win32 API函數。
vc6裏Win32 Application和Win32 console Application的區別:
很多VC初學者可能會常遇到如下鏈接錯誤:
Linking... /subsystem:windows
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol WinMain @16
Debug/TestWin.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
解決方法是:將project-->settings-->link的project options裏的/subsystem:windows 改
成/subsystem:console
原因:Win32 Application的入口函數爲WinMain
Win32 Console Application的入口函數是main
換句話說,如果你編寫傳統的C程序,必須建立Win32 Console程序,但VC裏面默認的是Win32
Application,於是上面提及的鏈接錯誤就就經常出現了。而Win32 Application和Win32 Console
的區別就在於VC裏鏈接參數不同。
本應該建Win32 Console Application工程的,而卻錯誤的建成了Win32 Application 工程:
project(工程)-->settings(設置)-->Link(連接)選項卡,在最下方的"Project Options"列表框中裏有這麼一堆東東
/nologo /subsystem:windows /incremental:yes /pdb:"Debug/............
其中帶下劃線的是需要修改的地方.subsystem:windows-->subsystem:console.
反之如果要建立Win32 Application 建立成了Win32 Console Application
則把subsystem:console-->subsystem:windows.
關於調用的約定:
_stdcall與_cdecl是兩種不同的函數調用約定 WINAPI CALLBACK 顯式的加入_stdcall