//呼叫api函式建立視窗
//示例:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//視窗過程函式
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");//定義視窗類名
HWND hwnd;//視窗控制代碼
MSG msg; //
WNDCLASS wndclass; //視窗類
wndclass.style = CS_HREDRAW | CS_VREDRAW;//指定視窗型別,各種“類風格”(詳見下方↓)可以使用按位或運算子組合起來
wndclass.lpfnWndProc = WndProc;//指定視窗過程(必須是回撥函式)
wndclass.cbClsExtra = 0;//預留的額外空間,一般為 0
wndclass.cbWndExtra = 0;//預留的額外空間,一般為 0
wndclass.hInstance = hInstance;//應用程式的例項控制代碼
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//為所有基於該視窗類的視窗設定一個圖示
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);//為所有基於該視窗類的視窗設定一個滑鼠指標
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//指定視窗背景色
wndclass.lpszMenuName = NULL;//指定視窗選單
wndclass.lpszClassName = szAppName;//指定視窗類名
if (!RegisterClass(&wndclass))//註冊視窗
MessageBox(NULL, TEXT("這個程式需要在 Windows NT 才能執行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, //建立視窗
TEXT("windows"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow); //顯示視窗
UpdateWindow(hwnd); //更新視窗,重繪
while (GetMessage(&msg, NULL, 0, 0)) //獲得訊息
TranslateMessage(&msg);
DispatchMessage(&msg);
return msg.wParam;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//回撥函式
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawText(hdc, TEXT("第一個C語言視窗程式!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
case WM_DESTROY:
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
//呼叫api函式建立視窗
//示例:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//視窗過程函式
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");//定義視窗類名
HWND hwnd;//視窗控制代碼
MSG msg; //
WNDCLASS wndclass; //視窗類
wndclass.style = CS_HREDRAW | CS_VREDRAW;//指定視窗型別,各種“類風格”(詳見下方↓)可以使用按位或運算子組合起來
wndclass.lpfnWndProc = WndProc;//指定視窗過程(必須是回撥函式)
wndclass.cbClsExtra = 0;//預留的額外空間,一般為 0
wndclass.cbWndExtra = 0;//預留的額外空間,一般為 0
wndclass.hInstance = hInstance;//應用程式的例項控制代碼
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//為所有基於該視窗類的視窗設定一個圖示
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);//為所有基於該視窗類的視窗設定一個滑鼠指標
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//指定視窗背景色
wndclass.lpszMenuName = NULL;//指定視窗選單
wndclass.lpszClassName = szAppName;//指定視窗類名
if (!RegisterClass(&wndclass))//註冊視窗
{
MessageBox(NULL, TEXT("這個程式需要在 Windows NT 才能執行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, //建立視窗
TEXT("windows"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow); //顯示視窗
UpdateWindow(hwnd); //更新視窗,重繪
while (GetMessage(&msg, NULL, 0, 0)) //獲得訊息
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//回撥函式
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawText(hdc, TEXT("第一個C語言視窗程式!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}