中年人学C语言Windows程序设计,2 第一个windows窗口

中年人学C语言Windows程序设计,2 第一个windows窗口

窗口类详解

要想创建窗口,首先要注册一个窗口类,SDK为我们提供了一个结构“WNDCLASS”,先来看看这个结构的原型

typedef struct tagWNDCLASS {
   
  UINT      style;
  WNDPROC   lpfnWndProc;
  int       cbClsExtra;
  int       cbWndExtra;
  HINSTANCE hInstance;
  HICON     hIcon;
  HCURSOR   hCursor;
  HBRUSH    hbrBackground;
  LPCTSTR   lpszMenuName;
  LPCTSTR   lpszClassName;
} WNDCLASS, *PWNDCLASS;

style:窗口风格

style:窗口类风格(0为缺省风格)
类样式定义窗口类的其他元素。两个或多个样式可以使用位或运算。
我一般都用0,有特殊需要时用一下风格:

CS_BYTEALIGNCLIENT
Aligns the window’s client area on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.
将窗口的客户区 (在 x 方向) 的字节边界上对齐。这种风格影响的窗口和其水平位置上显示的宽度。

CS_BYTEALIGNWINDOW
Aligns the window on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.
将窗口 (在 x 方向) 的字节边界上对齐。这种风格影响的窗口和其水平位置上显示的宽度。

CS_CLASSDC
Allocates one device context to be shared by all windows in the class. Because window classes are process specific, it is possible for multiple threads of an application to create a window of the same class. It is also possible for the threads to attempt to use the device context simultaneously. When this happens, the system allows only one thread to successfully finish its drawing operation.
分配一个设备上下文类中的所有窗口共享。因为窗口类是具体的过程,有可能为多个线程的应用程序创建一个窗口在同一类。它也是可能的线程试图同时使用的设备上下文。当发生这种情况时,系统只允许一个线程能顺利完成其绘图操作。

CS_DBLCLKS
Sends a double-click message to the window procedure when the user double-clicks the mouse while the cursor is within a window belonging to the class.
当用户双击鼠标光标位于内属于类的窗口时,将双击消息发送到窗口过程。

CS_DROPSHADOW
Enables the drop shadow effect on a window. The effect is turned on and off through SPI_SETDROPSHADOW. Typically, this is enabled for small, short-lived windows such as menus to emphasize their Z-order relationship to other windows. Windows created from a class with this style must be top-level windows; they may not be child windows.
使窗口上有阴影效果。效果是通过 SPI_SETDROPSHADOW 开启和关闭。通常情况下,这是为启用小而短寿的窗口,例如菜单强调他们到其他窗口的 Z 顺序关系。从具有这种风格的类创建的窗口必须是顶级窗口;他们可能不是子窗口。

CS_GLOBALCLASS
Indicates that the window class is an application global class. For more information, see the “Application Global Classes” section of About Window Classes.
指示窗口类是应用程序的全局类。更多的信息,请参阅关于窗口类的"应用程序全局类"节(MSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/ms633574(v=vs.85).aspx)。

CS_HREDRAW
Redraws the entire window if a movement or size adjustment changes the width of the client area.
如果移动或大小调整更改客户端区域的宽度将重新绘制整个窗口。

CS_NOCLOSE
Disables Close on the window menu.
在窗口菜单中禁用关闭

CS_OWNDC
Allocates a unique device context for each window in the class.
每个窗口类中分配一个唯一的设备上下文。

CS_PARENTDC
Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent. A window with the CS_PARENTDC style bit receives a regular device context from the system’s cache of device contexts. It does not give the child the parent’s device context or device context settings. Specifying CS_PARENTDC enhances an application’s performance.
这样的子窗口可以画在父到父窗口设置子窗口的裁剪的矩形。一个带有 CS_PARENTDC 样式位窗口接收常规设备上下文从系统的缓存中的设备上下文。它不给孩子父母的设备上下文或设备上下文设置。指定 CS_PARENTDC 增强应用程序的性能。

CS_SAVEBITS
Saves, as a bitmap, the portion of the screen image obscured by a window of this class. When the window is removed, the system uses the saved bitmap to restore the screen image, including other windows that were obscured. Therefore, the system does not send WM_PAINT messages to windows that were obscured if the memory used by the bitmap has not been discarded and if other screen actions have not invalidated the stored image.
This style is useful for small windows (for example, menus or dialog boxes) that are displayed briefly and then removed before other screen activity takes place. This style increases the time required to display the window, because the system must first allocate memory to store the bitmap.
作为一个位图保存屏幕图像被此类窗口遮盖的部分。窗口删除时,系统将使用保存的位图来还原屏幕图像,包括其他窗口遮挡。因此,系统并不发送 WM_PAINT 消息如果位图使用的内存不被丢弃,并且其他屏幕操作都不会失效所存储的图像就被遮住的窗口。
这种风格是有用的小窗口 (例如,菜单或对话框中),并简要显示然后删除其他屏幕活动发生之前。这种风格会增加因为系统必须首先分配的内存来存储位图来显示窗口,所需的时间。

CS_VREDRAW
Redraws the entire window if a movement or size adjustment changes the height of the client area.
如果移动或大小调整更改客户端区域的高度,重新绘制整个窗口。

lpfnWndProc 窗口过程(窗口回调函数指针)

窗口回调函数的原型为:

LRESULT CALLBACK WindowProc(
  _In_ HWND   hwnd,
  _In_ UINT   uMsg,
  _In_ WPARAM wParam,
  _In_ LPARAM lParam
);

hwnd是消息响应的窗口句柄

uMsg是消息

wParam和lParam是消息附加值

cbClsExtra

额外 的 字节 分配给 窗口类 结构 数目 。默认 为 零 字节 。

cbWndExtra

额外 分配 的 字节 后 窗口 实例 数 。 系统 初始化 为 零 字节 。 如果 应用程序 使用 WNDCLASS 注册 对话框 中 的 资源 文件 中 使用 类 指令 创建 的 它 必须 将 此 成员 设置 为 DLGWINDOWEXTRA 。(就是说普通的窗口为0即可)

hInstance 窗口类的实例句柄。

WinMain的第一个参数,也可以用GetModuleHandle()动态获取

hIcon 图标句柄

设置窗口图标,LoadIcon(NULL,IDI_APPLICATION);是默认图标,也可以从自己的资源里面加载,那样的话LoadIcon的第一个参数用实例句柄。(我将会在下一篇的RC资源简单使用中详细说)

hCursor 光标指针句柄

默认的箭头是:LoadCursor(NULL, IDC_ARROW);

=======================================================

IDC_APPSTARTING 标准的箭头和小沙漏
IDC_ARROW 标准的箭头
IDC_CROSS 十字光标
IDC_HAND Windows 98/Me, Windows 2000/XP: Hand
IDC_HELP 标准的箭头和问号
IDC_IBEAM 工字光标
IDC_ICON Obsolete for applications marked version 4.0 or later.
IDC_NO 禁止圈
IDC_SIZE Obsolete for applications marked version 4.0 or later. Use IDC_SIZEALL.
IDC_SIZEALL 四向箭头指向东、西、南、北
IDC_SIZENESW 双箭头指向东北和西南
IDC_SIZENS 双箭头指向南北
IDC_SIZENWSE 双箭头指向西北和东南
IDC_SIZEWE 双箭头指向东西
IDC_UPARROW 垂直箭头

IDC_WAIT 沙漏,Windows7系统下会显示为选择的圆圈表示等待

同理,也可以用自己资源里的光标指针,那样的话LoadCursor的第一个参数用实例句柄。(与使用自定义图标相同)

hbrBackground 背景画刷的句柄

这个可以是一个画刷句柄,也可以用已下预定值:COLOR_ACTIVEBORDER,COLOR_ACTIVECAPTION,COLOR_APPWORKSPACE,COLOR_BACKGROUND,COLOR_BTNFACE,COLOR_BTNSHAD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值