#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