q MFC Windows 2Jeff Prosise q Visual CDavid

  • Slides: 48
Download presentation

相关参考书 q. MFC Windows 程序设计(第 2版)Jeff Prosise著; q. Visual C++技术内幕(第四版)David J. Kruglinski著 q深入解析MFC George

相关参考书 q. MFC Windows 程序设计(第 2版)Jeff Prosise著; q. Visual C++技术内幕(第四版)David J. Kruglinski著 q深入解析MFC George Shepherd,Scot Wingo 著 q深入浅出MFC(第二版)侯俊杰(侯捷)著 6

句柄(HANDLE) 20

句柄(HANDLE) 20

Win. Main函数 2 -1 q在Windows应用程序中main()被Win. Main() 取代 q. Win. Main函数的原型 int WINAPI Win. Main(

Win. Main函数 2 -1 q在Windows应用程序中main()被Win. Main() 取代 q. Win. Main函数的原型 int WINAPI Win. Main( HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lp. Cmd. Line, int n. Cmd. Show ); #define WINAPI FAR PASCAL //修饰符WINAPI 代表_stdcall 24 //当前实例句柄 //上一个实例句柄 //传递给应用程序的参数字符串 //窗口显示状态

Win. Main函数 2 -2 #include <windows. h> /*定义了Windows的所有数据类型、函数调用、 数据结构和符号常量*/ int WINAPI Win. Main( HINSTANCE

Win. Main函数 2 -2 #include <windows. h> /*定义了Windows的所有数据类型、函数调用、 数据结构和符号常量*/ int WINAPI Win. Main( HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lp. Cmd. Line, int n. Cmd. Show) { if (h. Prev. Instance){ Message. Box(NULL, "程序已经运行", "提示", MB_OK |MB_ICONINFORMATION); return 0; } Message. Box(NULL, "Hello World", "提示", MB_OK); } 25

WNDCLASS结构体 3 -1 q 通过WNDCLASS结构体变量设计窗口,指定窗口 的属性(光标,图标,菜单,背景色等) typedef struct _WNDCLASS { UINT style; WNDPROC lpfn.

WNDCLASS结构体 3 -1 q 通过WNDCLASS结构体变量设计窗口,指定窗口 的属性(光标,图标,菜单,背景色等) typedef struct _WNDCLASS { UINT style; WNDPROC lpfn. Wnd. Proc; int cb. Cls. Extra; int cb. Wnd. Extra; HINSTANCE h. Instance; HICON h. Icon; HCURSOR h. Cursor; HBRUSH hbr. Background; LPCTSTR lpsz. Menu. Name; LPCTSTR lpsz. Class. Name; } WNDCLASS, *PWNDCLASS; 27 // 窗口风格 // 窗口过程 // 指定该结构的额外字节数 // Windows内部保存窗口时预留空间 // 进程句柄 // 应用程序图标句柄 // 应用程序光标句柄 // 背景画刷句柄 // 菜单资源名 // 窗口类名

设计窗口类代码 int WINAPI Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lp.

设计窗口类代码 int WINAPI Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lp. Cmd. Line, int n. Cmd. Show){ HWND hwnd ; // 窗口句柄 WNDCLASS wndclass ; // 窗口类 wndclass. style = CS_HREDRAW | CS_VREDRAW ; wndclass. lpfn. Wnd. Proc = Wnd. Proc ; wndclass. cb. Cls. Extra = 0 ; wndclass. cb. Wnd. Extra = 0 ; wndclass. h. Instance = h. Instance ; wndclass. h. Icon = Load. Icon (NULL, IDI_APPLICATION) ; wndclass. h. Cursor = Load. Cursor (NULL, IDC_ARROW) ; wndclass. hbr. Background = (HBRUSH) Get. Stock. Object (WHITE_BRUSH) ; wndclass. lpsz. Menu. Name = NULL ; wndclass. lpsz. Class. Name = “Hello. Win”; // 窗口类名 //以下代码省略 } 30

注册窗口类-Register. Class q设计完WNDCLASS后,需调用Register. Class 函数对其进行注册,经过注册之后,才可 以创建该窗口 q函数的原型: q. ATOM Register. Class( CONST WNDCLASS *lp.

注册窗口类-Register. Class q设计完WNDCLASS后,需调用Register. Class 函数对其进行注册,经过注册之后,才可 以创建该窗口 q函数的原型: q. ATOM Register. Class( CONST WNDCLASS *lp. Wnd. Class // class data ); q返回值为 0代表注册失败 31

创建窗口——Create. Window 2 -1 q 设计好了窗口类并且将其注册好以后就可以用 Create. Window函数产生这种类型的窗口了 q Create. Window函数原型如下: HWND Create. Window(

创建窗口——Create. Window 2 -1 q 设计好了窗口类并且将其注册好以后就可以用 Create. Window函数产生这种类型的窗口了 q Create. Window函数原型如下: HWND Create. Window( LPCTSTR lp. Class. Name, // registered class name LPCTSTR lp. Window. Name, // window name DWORD dw. Style, // window style int x, // horizontal position of window int y, // vertical position of window int n. Width, // window width int n. Height, // window height HWND h. Wnd. Parent, // handle to parent HMENU h. Menu, // menu handle or child identifier HINSTANCE h. Instance, // handle to application instance LPVOID lp. Param // window-creation data ); 32

显示创建的窗口 2 -1 q函数Show. Window和Update. Window分别表示 显示和刷新窗口,其函数原形分别如下: BOOL Show. Window( HWND h. Wnd, //

显示创建的窗口 2 -1 q函数Show. Window和Update. Window分别表示 显示和刷新窗口,其函数原形分别如下: BOOL Show. Window( HWND h. Wnd, // handle to window int n. Cmd. Show // show state ); BOOL Update. Window( HWND h. Wnd // handle to window ); 34

消息循环 3 -3 //Win. Main函数 //…以上代码省略 MSG msg; while (Get. Message (&msg, NULL, 0,

消息循环 3 -3 //Win. Main函数 //…以上代码省略 MSG msg; while (Get. Message (&msg, NULL, 0, 0)) { Translate. Message (&msg) ; // 转换某些键盘消息 Dispatch. Message (&msg) ; } return msg. w. Param; //…以下代码省略 38

回调函数 2 -2 LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT message, WPARAM w. Param,

回调函数 2 -2 LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT message, WPARAM w. Param, LPARAM l. Param) { switch (message) { case WM_LBUTTONDOWN: Message. Box (NULL, “Hello World”, “问候”, 0) ; return 0 ; case WM_CHAR: char message[100]; sprintf(message, "your enter char is: %c", w. Param); Message. Box(hwnd, message, "提示", 0); return 0; } return Def. Window. Proc (hwnd, message, w. Param, l. Param) ; // 执行默认的消息处理 } 40

常用的Windows标准消息 操作 键盘键入 WM_CHAR 鼠标移动 WM_MOUSEMOVE 鼠标左键按下 WM_LBUTTONDOWN 鼠标右键松开 WM_RBUTTONUP 窗口创建 WM_CREATE 窗口销毁 WM_DESTROY

常用的Windows标准消息 操作 键盘键入 WM_CHAR 鼠标移动 WM_MOUSEMOVE 鼠标左键按下 WM_LBUTTONDOWN 鼠标右键松开 WM_RBUTTONUP 窗口创建 WM_CREATE 窗口销毁 WM_DESTROY 窗口关闭 WM_CLOSE 窗口显示 WM_SHOWWINDOW 窗口激活 WM_ACTIVE … 43 消息名 …

Windows程序的“生死存亡” 3 -3 //Wnd. Proc函数 //…以上代码省略 switch(u. Msg) { case WM_CLOSE: if (IDYES==Message. Box(hwnd,

Windows程序的“生死存亡” 3 -3 //Wnd. Proc函数 //…以上代码省略 switch(u. Msg) { case WM_CLOSE: if (IDYES==Message. Box(hwnd, "确认要退出吗?", "提示", MB_YESNO | MB_ICONQUESTION)){ Destroy. Window(hwnd); } break; case WM_DESTROY: Post. Quit. Message(0); break; case WM_CREATE: Message. Box(hwnd, “first", "提示", 0); break; }//…以下代码省略 46