个人总结

来源:互联网 发布:淘宝优惠券领取app 编辑:程序博客网 时间:2024/04/29 03:08

书写win32程序过程分析

代码:

#include <Windows.h>#include <stdio.h>#include <windowsx.h>#pragma comment(lib,"Winmm.lib")LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI CALLBACK WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nShowCmd ){//定义UI 并且初始化他的参数WNDCLASS win;MSG msg;HWND hwin;static TCHAR szAppName[] = TEXT ("HelloWin") ;win.style=CS_HREDRAW|CS_VREDRAW;win.lpfnWndProc=WndProc;win.cbClsExtra=0;win.cbWndExtra=0;win.hInstance=hInstance;win.hIcon=NULL;win.hCursor=NULL;win.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);win.lpszMenuName=szAppName;win.lpszClassName=szAppName;//注册windowRegisterClass(&win);//create an instance hwin=CreateWindow(szAppName,TEXT("hello world"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);//showShowWindow(hwin,nShowCmd);UpdateWindow(hwin);//set up message loopwhile(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("Hello,win api"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);EndPaint(hwnd,&ps);return 0;case WM_KEYDOWN://测试键盘按键{int code=(int)wParam;int state=(int)lParam;switch(code){case VK_RIGHT:MessageBox(NULL,TEXT("hello"),TEXT(" fy"),NULL);break;case VK_LEFT:MessageBox(NULL,TEXT("fy"),TEXT("fyyyyy"),NULL);break;case 65:MessageBox(NULL,TEXT("a"),TEXT("a"),NULL);break;}return 0;}case WM_SIZE:MessageBox(NULL,TEXT("hello "),TEXT("my happy life"),NULL);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,message,wParam,lParam);}
一个win32 api程序的过程如下

1.Define the window UI

主要是定义,并且初始化WNDCLASS 结构体的一些变量

2.Registe the window

就一句话,RegisterClass(&win);

3.Create an instance of the window and show it

调用CreateWindow,又是填了一大堆参数,有鼠标的,有关于窗体位置的,有关于窗体标题的等等。
然后show一下,更新一下。

4.Set  up  a message loop to catch the events;

不断地处于死循环状态,时刻获取消息,然后翻译下,处理掉。

5.Deal the message;

回调函数,处理消息。有键盘的,鼠标的,窗体本身的,等等。



原创粉丝点击