生活随笔
收集整理的這篇文章主要介紹了
基于Win32Api创建窗口程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近在整理資料, 記得上學(xué)期間寫得第一個程序是用匯編語言寫了個類似HD-COPY的程序, 那個時候真是廢寢忘食,程序正常運行后真得讓人非常激動, 可惜太久遠(yuǎn)了找不到代碼了, 那個時候有張3寸盤就很不錯了,筆記本電腦更是買不起的。
今天重寫了當(dāng)初學(xué)習(xí)windows平臺編程時的第一個程序, 貼出來做個紀(jì)念。
#include <windows.h>LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);HINSTANCE hInst;
HWND hWndMain;
char * pszClassName = "MainWndClass";// 應(yīng)用入口
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{MSG msg;if (!InitApplication(hInstance))return FALSE;if (!InitInstance(hInstance, nCmdShow))return FALSE;while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}BOOL InitApplication(HINSTANCE hInstance)
{WNDCLASS wc;wc.style = CS_HREDRAW | CS_VREDRAW;wc.lpfnWndProc = (WNDPROC)MainWndProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = hInstance;wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);wc.hCursor = LoadCursor(NULL, IDC_ARROW);wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wc.lpszMenuName = NULL;wc.lpszClassName = pszClassName;//注冊窗口類if (!RegisterClass(&wc)){return FALSE;}return TRUE;
}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{hInst = hInstance;hWndMain = CreateWindow(pszClassName, "sample", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);if (!hWndMain){return FALSE;}ShowWindow(hWndMain, nCmdShow);UpdateWindow(hWndMain);return TRUE;
}//窗口消息處理函數(shù)
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{HDC hdc;PAINTSTRUCT ps;char * s = "hello the world";switch (message){case WM_PAINT:hdc = BeginPaint(hWnd, &ps);TextOut(hdc, 20, 10, s, strlen(s));EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;
}
運行效果
總結(jié)
以上是生活随笔為你收集整理的基于Win32Api创建窗口程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。