#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);
}