第一天

来源:互联网 发布:sql2008数据库下载 编辑:程序博客网 时间:2024/05/02 00:16
 

第一天

Windows程序包含程序代码和UI(User Interface)资源两部份。最后以RC编译器整合成一个完整的EXE文件。

UI指功能菜单、对话框外貌、程序图标、光标形状等

 

Windows程序支持动态联编。应用程序调用的API函数是在执行时期才联结上的。

Windows程序调用的函数可分为 C Runtimes和Windows API两在部份,Visual C++4.0后才支持动态联编。

32位操作系统,不再有 small medium large等内存模式之分。

LIBC.LIB C Runtimes函数库的静态联接版本。

MSVCRT.LIB C Runtimes函数库的动态联接版本。必须添加MSVCRT40.LIB

Windows API 由操作系统本身提供,包含如下三部份:GDI32.DLL USER32.DLL KERNEL32.DLL

新增的API函数 如:Common Dialog ToolHelp 放在COMMDLG.LIB、TOOLHELP.LIB中

CMDLG32.LIB TH32.LIB

 

Windows程序必须包含 WINDOWS.H头文件 它只照顾三大模块所提供的API函数,如果用到其它的system DLLs必须包含相对应的头文件。

如COMDLG.DLL 为COMDLG.H  MAPI.DLL 为MAPI.H   TAPI.DLL为TAPI.H

 

以消息为基础,以事件驱动之

USER模块掌管各个外围的驱动程序 如键盘、鼠标发生的事件

输入分类:硬件装置产生的消息 存放在系统队列中system queue   由windows系统或其它windows程序传送过来的消息 放在application queue (程序队列)

每个windows程序都应有一个死循环。如下:

MSG msg;

While(GetMessage(&msg,NULL,NULL,NULL)){

TranslateMessage(&msg);

DispatchMessage(&msg);}

定义的MSG结构体如下:

Typedef struct tagMSG

{

HWND hwnd;

UINT message;

WPARAM wParam;

LPARAM lParam;

DWORD time;

POINT pt;

}MSG;

 

接受并外理消息的主角是窗口,每一个窗口都应有一个函数负责处理消息,必须设计这个窗口函数

 

函数接入点 WinMain()

int CALLBACK WinMain(

HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdline,

int nCmdShow)

{

定义一个WNDCLASS结构体,

初始化 WNDCLASS结构体

注册结构体;

<在函数InitApplication中实现>

 

创建窗口CreateWindow

显示窗口 ShowWindow

更新窗口 UpdateWindow

 

进入消息循环while(GetMessage(&msg,NULL,NULL,NULL))

{TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

WinMain()中的四个参数由系统传递进来。

 

窗口产生之前,其属性必须要设定好,包括窗口的外貌(边框、颜色、标题、位置等)和行为(窗口接收消息后的反应)

程序必须要在窗口产生之前利用API函数RegisterClass设定窗口属性(称为注册窗口类别)

WNDCLASS.lpfnWndProc指定的函数就是窗口行为的中枢,也就是所谓的窗口函数

CreateWindow产生窗口,但不显示窗口,所以必须调用ShowWindow显示窗口。

WNDCLASS

 Typedef struct{

UINT style;

WNDPROC lpfnWndProc;

Int cbClsExtra;

Int cbWndExtra;

HINSTANCE hInstance;

HICON hIcon;

HCURSOR hCursor;

HBRUSH hbrBackground;

LPCTSTR lpszMenuName;

LPCTSTR lpszClassName;

}WNDCLASS,*PWNDCLASS;

style

Specifies the class style(s),This member can any combination of the class styles

指定窗口的风格,可是任意 class styles的组合

lpfnWndProc

Pointer to the window procedure.You must use the CallWindowProc function to call the window procedure

指定窗口过程函数。你必须使用CallWindowProc函数来呼叫窗口过程函数。

cbClsExtra

Specifies the number of extra bytes to allocate following the window-class structure. The system initializes the bytes to zero.

指定为窗口结构体额外分配的字节,系统初始化此字节为0

cbWndExtra

Specifies the number of extra bytes to allocate following the window instance. The system initializes the bytes to zero. If an application uses WNDCLASS to register a dialog box created by using the CLASS directive in the resource file, it must set this member to DLGWINDOWEXTRA.

指定窗口实例的额外字节 。系统初始化此字节为0 如果一个应用程序使用WNDCLASS结构体去注册一个对话框 在资源文件中创建窗口指令,此参数必须设置为DLGWINDOWEXTRA

hInstance

Handle to the instance that contains the window procedure for the class.

标识一个包含窗口进程的实例。

hIcon

Handle to the class icon. This member must be a handle to an icon resource. If this member is NULL, the system provides a default icon.

标识窗口的图标,这个成员变量必须是一个图标资源 ,如果这个成员变量为NULL,系统提供一个预设的图标。

hCursor

Handle to the class cursor. This member must be a handle to a cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into the application's window.

标识窗口的光标。这个成员必须是一个光标资源,如果些成员为NULL,应用程序必须显式给出当鼠标进入应用程序窗体时光标的形状。

hbrBackground

Handle to the class background brush. This member can be a handle to the physical brush to be used for painting the background, or it can be a color value. A color value must be one of the following standard system colors (the value 1 must be added to the chosen color). If a color value is given, you must convert it to one of the following HBRUSH types:

标识窗口的背景画刷。此成员可以作为任意画刷的标识用作绘制背景,也可以是一个颜色值。颜色值必须是是以下标准系统颜色。

lpszMenuName

Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu.

指向一个指定窗口菜单源资的以0结尾的字符串。作为在资源文件出现的名称。如果你使用一个整数去标识菜单,使用MAKEINTRESOURCE宏。如果此成员为空,窗口将会没有菜单栏。

lpszClassName

Pointer to a null-terminated string or is an atom. If this parameter is an atom, it must be a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpszClassName; the high-order word must be zero.

指向一个字符串或者是一个原子,如果此参数是一个原子,它必须由RegisterClass或RegisterClassEx注册。原子必须是lpszClassName的低字节,高字节为0

 

HWND CreateWindow(

LPCTSTR lpClassName,//必须要与WNDCLASS.lpszClassName一致。

LPCTSTR lpWindowName,//窗口标题标显示的名字。

DWORD dwStyle,//指定将要创建窗口的风格

Int x,//窗口左上角X坐标点

Int y,//窗口右上角Y坐标点

Int nWidth,//宽度

Int nHeight,//高度

HWND hWndParent,//父窗口句柄

HMENU hMenu,//菜单

HANDLE hInstance,//与窗口关联的应用程序实例

PVOID lpParam/*指向一个值的指针,该值传递给窗口 WM_CREATE消息。该值通过在IParam参数中的CREATESTRUCT结构传递。如果应用程序调用CreateWindow创建一个MDI客户窗口,则lpParam必须指向一个CLIENTCREATESTRUCT结构。)*/

 

窗口的生命中枢 窗口函数

DispatchMessage把消息通过USER模块的协助送到该窗口的窗口函数中去了。窗口函数是由系统调用的,所以必须指定为callback<指在你程序中,但是由Window系统调用> 这个函数由你设计,但永远不应由你调用,它是为windows系统准备的。

不论什么样的消息,都必须处理,所以必须在default处调用DefWindowProc<windows内预设的消息处理函数>

窗口函数:

LRESULT CALLBACK WndProc(

HWND hwnd,

UINT message,

WPARAM wParam,

LPARAM lParam)

wParam lParam 依消息的不同而异 wParam在16位系统是16位。在32位系统是32位

 

消息映射 Message Map

 

Modal窗口依靠DialogBox EndDialog两个API函数,激活与结束。

原创粉丝点击