#include <stdio.h> int main()
{ printfss("Hello world\n"); return 0; }
无数人知道这段代码,而知道下面的代码的人数比上面的要稍少了一些.
#include <windows.h>
int main()
{
MessageBox(NULL,"Hello World","window",MB_OK);
return 0;
}
这两段代码运行后都会显示dos窗口,下面的代码将把你真正带入windows环境,就没有dos窗口什么事了。
#include <windows.h>
int WINAPI WinMain(HINSTANCE hins,HINSTANCE preHins,LPSTR cmd,int show)
{
MessageBox(NULL,"Hello World","window",MB_OK);
return 0;
}
这样,你就编写了一个最简单的windows程序,但只有一个消息框,还没有真正意义上的窗口。
#include <windows.h>
//消息处理函数
LRESULT CALLBACK WinPorc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hins,HINSTANCE phins,LPSTR cmd,int show)
{
HWND hwnd;
MSG msg;
WNDCLASS wnd;
ZeroMemory(&wnd,sizeof(WNDCLASS));
wnd.hbrBackground = (HBRUSH)::GetStockObject(DKGRAY_BRUSH);
wnd.hInstance = hins;
wnd.lpfnWndProc = WinPorc;
wnd.lpszClassName="test";
wnd.style = CS_VREDRAW|CS_HREDRAW;
if(!::RegisterClass(&wnd))
{
return 0;
}
hwnd = ::CreateWindow("test","test",WS_OVERLAPPED|WS_SYSMENU,0,0,100,100,NULL,NULL,hins,NULL);
if(hwnd==NULL)
{
return 0;
}
ShowWindow(hwnd,show);
UpdateWindow(hwnd);
while(TRUE){
if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
if(msg.message == WM_QUIT){
break;
}
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WinPorc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return ::DefWindowProc(hwnd,msg,wParam,lParam);
}