windows编程之自己动手写WinMain函数
来源:互联网 发布:领航一号软件下载 编辑:程序博客网 时间:2024/04/30 12:12
这是我参考msdn和书本自己写的窗口程序,
WinMain函数式所有windows应用程序的入口,类似C语言里的main函数,其功能是:完成一系列的定义及初始化工作,并产生消息循环。消息循环是整个程序运行的核心 。
WinMain函数主要实现以下功能:
1.注册窗口类,建立窗口,执行其他必要的初始化的操作;
2.进入消息循环,根据从应用程序消息队列接受的消息,调用相应的处理过程;
3.当消息循环检索到WM_QUIT消息时,终止程序运行。
代码如下:
#include<windows.h>#include<stdio.h>LRESULT CALLBACK WndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter);int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state){WNDCLASS wndclass;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.hInstance = hInstance;wndclass.lpfnWndProc = WndProc;wndclass.lpszClassName = "我的窗口";wndclass.lpszMenuName = NULL;wndclass.style = CS_HREDRAW | CS_VREDRAW;RegisterClass(&wndclass); //注册窗口类HWND hwnd;hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);ShowWindow(hwnd, SW_SHOWNORMAL);UpdateWindow(hwnd);MSG Msg;while(GetMessage(&Msg, NULL, 0, 0)){TranslateMessage(&Msg); DispatchMessage(&Msg); }return 0;}LRESULT CALLBACK WndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter){switch(uMsg){case WM_CHAR:char strChar[20];sprintf(strChar, "char is %d", wParam);MessageBox(hwnd, strChar, "window", MB_OK);break;case WM_LBUTTONDOWN:MessageBox(hwnd, "mouse left-click", "window", MB_OK);HDC hdc;hdc = GetDC(hwnd);TextOut(hdc, 0, 50, "hello mouse left-click", strlen("hello mouse left-click"));ReleaseDC(hwnd, hdc);break;case WM_RBUTTONUP:MessageBox(hwnd, "release mouse", "window", MB_OK);hdc = GetDC(hwnd);TextOut(hdc, 0, 80, "hello release mouse", strlen("hello release mouse"));ReleaseDC(hwnd, hdc);break;case WM_CLOSE:PostQuitMessage(0);break;case WM_DESTROY:DestroyWindow(hwnd);break;default:return DefWindowProc(hwnd, uMsg, wParam, lParam);}return 0;}
总结:
感觉自己动手写窗口是有点难度的,因为写的不多,很多东西没记住,都要自己去查资料。
- windows编程之自己动手写WinMain函数
- Windows 编程之WinMain
- Windows API WinMain函数
- Windows编程原理及自己动手写计算器
- Win32编程之WinMain
- windows编程之四 WinMain: The Application Entry Poin
- 自己动手写 printf函数
- 自己动手写 printf函数
- 自己动手写 printf函数
- 自己动手写strcat函数
- DirectX学习之WinMain函数
- windows编程--第一个WinMain程序
- C++源代码游戏编程--WinMain()函数集
- WinMain() 函数
- WinMain函数
- WinMain()函数
- WinMain函数
- WinMain函数
- Android 如何画圆饼图来表示智能机的市场份额
- 建行卡行家
- 新建maven工程作为引擎
- 再不'折腾'就老了
- cuda入门——结合opncv和cuda编程(2)
- windows编程之自己动手写WinMain函数
- UVa 12036 Stable Grid (想法题)
- 代码应该写的更简洁更优化
- 获取当前时间的毫秒数
- 和国家开放空间
- java定时器的使用
- cocos2d-x网络开发基础 HTTP的报文格式、GET和POST格式解析
- 在android 4.2.2上调试MU609步骤
- android代码混淆报错总结:java.io.IOException: Please correct the above