Dev-C++雖然是免費的C/C++編譯器
但是Dev-C++也幫我們建立好Win32 API的相關連結
可以讓我們快速的製作出視窗介面
首先第一個步驟式開啟專案
接下來點選Windows Application
最後儲存編譯就可以執行底下的視窗程式
這些步驟雖然簡單
但是這其實是Dev-C++的作者幫我們些好的範本
如果不了解範本裡面的程式是很難繼續下一步的
不過本人對於範本的程式碼也還沒到完全了解的地步
因此只能做部分的解說
#include <windows.h> //將windows的函式庫給呼叫近來
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);//宣告windows的程序
char szClassName[ ] = "WindowsApp"; //建立類別名稱
/*windows 主程式開始*/
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* 宣告視窗類別的資料結構*/
/* 視窗類別的設定 */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* 功能表的設定,目前是沒有的*/
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; //視窗的背景設定
/* 註冊視窗類別,如果失敗就會跳出*/
if (!RegisterClassEx (&wincl))
return 0;
/* 如果註冊成功,就能建立這個視窗程式*/
/* CreateWindowEx為設定視窗的各種樣式*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* 類別名稱 */
"Windows App", /* 標題名稱*/
WS_OVERLAPPEDWINDOW, /* 視窗的風格設定 */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* 寬度 */
375, /* 高度 */
HWND_DESKTOP, /* 這個視窗程式是桌面的子視窗*/
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* 將此視窗秀到螢幕上 */
ShowWindow (hwnd, nFunsterStil);
/* 持續執行訊息回圈. 直到GetMessage傳回0時 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}