基于Win32Api创建窗口程序

来源:互联网 发布:苏联军力鼎盛时期知乎 编辑:程序博客网 时间:2024/04/27 16:31

最近在整理资料, 记得上学期间写得第一个程序是用汇编语言写了个类似HD-COPY的程序, 那个时候真是废寝忘食,程序正常运行后真得让人非常激动, 可惜太久远了找不到代码了, 那个时候有张3寸盘就很不错了,笔记本电脑更是买不起的。

今天重写了当初学习windows平台编程时的第一个程序, 贴出来做个纪念。


#include <windows.h>LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);BOOL InitApplication(HINSTANCE);BOOL InitInstance(HINSTANCE, int);HINSTANCE hInst;HWND hWndMain;char * pszClassName = "MainWndClass";// 应用入口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;}//窗口消息处理函数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;}


运行效果



0 0
原创粉丝点击