从鼠标中获得输入(2)


什么?没有边框的窗口?

TicTac 在一个很重要的方面与一二章的范例程序有所不同:它使用了一个CWnd的派生类作为其主窗口,而不是用的一个框架窗口.这并非意味着CFrameWnd不 能完成任务,而只是说光用CWnd完成这个任务已经足矣.CWndMFC中所有窗口类的基类,依照你的应该程序的不同,你可以选择从CWnd派生出不同 的窗口类或者是直接使用CWnd作为主窗口.尽管如此,作为一个MFC程序员,我们还是应当知道如何从CWnd派生出自己的类,同时,通过参详一个从 CWnd派生的窗口类也对MFC程序并不一定非要使用框架窗口加深了理解.

创 建一个你自己的CWnd派生类是非常简单的.首先,你从CWnd派生一个窗口类而不是从CFrameWnd.同是,BEGIN_MESSAGE_MAP 消息映射宏中也要注意把基类改为CWnd.然后,在窗口的构造函数中,使用AfxRegisterWndClass注册一个窗口类,接着调用CWnd:: CreateEx创建一个窗口.还记得在第一章我们看过的一个Windows SDK程序的C语言代码吗?在创建窗口之前,WinMain用描述窗口各种属性的值初始化了一个WNDCLASS结构,接着调用:: RegisterClass注册这个WNDCLASS.一般情况下在MFC程序中你不必自己注册窗口类,因为MFC已经替你代劳了. CFrameWnd::Create的第一个参数指定NULL值表明接受默认窗口类.但是当你从CWnd派生自己你窗口类时,你必须注册自己你 WNDCLASS,因为CWnd::CreateExWNDCLASS参数不接受NULL.


AfxRegisterWndClass函数

MFC 利用AfxRegisterWndClass全局函数使得注册一个WndClass变的非常容易.如果你使用::RegisterClass或者是MFC AfxRegisterClass注册一个WNDCLASS,你必须亲自初始化WNDCLASS的各个字段.AfxRegisterWndClass 替你填定好了大部分的内容,而只要求你指定MFC程序一般情况下关心的四个值.AfxRegisterWndClass的原型如下:

LPCTSTR AfxRegisterWndClass(UINT nClassStyle,HCURSOR hCurosor=0,HBRUSH hbrBackground=0,HICON hIcon=0)

AfxRegisterWndClass返回的是一个以null结尾的字符串指针,指向所注册的WNDCLASS名字.在开始研究TicTac是如何如用AfxRegisterWndClass之间,让我们来仔细看看这个函数本身和它的各个参数.

nClassStyle指明了类的样式,它定义了一个窗口的行为特征.nClassStyle是下表中一个或多个位标志的结合:

WNDCLASS样式标记

Class Style

Description

CS_BYTEALIGNCLIENT

Ensures that a window's client area is always aligned on a byte boundary in the video buffer to speed drawing operations.

CS_BYTEALIGNWINDOW

Ensures that the window itself is always aligned on a byte boundary in the video buffer to speed moving and resizing operations.

CS_CLASSDC

Specifies that the window should share a device context with other windows created from the same WNDCLASS.

CS_DBLCLKS

Specifies that the window should be notified of double clicks with WM_xBUTTONDBLCLK messages.

CS_GLOBALCLASS

Registers the WNDCLASS globally so that all applications can use it. (By default, only the application that registers a WNDCLASS can create windows from it.) Used primarily for child window controls.

CS_HREDRAW

Specifies that the entire client area should be invalidated when the window is resized horizontally.

CS_NOCLOSE

Disables the Close command on the system menu and the close button on the title bar.

CS_OWNDC

Specifies that each window created from this WNDCLASS should have its own device context. Helpful when optimizing repaint performance because an application doesn't have to reinitialize a private device context each time the device context is acquired.

CS_PARENTDC

Specifies that a child window should inherit the device context of its parent.

CS_SAVEBITS

Specifies that areas of the screen covered by windows created from this WNDCLASS should be saved in bitmap form for quick repainting. Used primarily for menus and other windows with short life spans.

CS_VREDRAW

Specifies that the entire client area should be invalidated when the window is resized vertically.


其中CS_BYTEALIGNCLIENT和BS_BYTEALIGNWINDOW样式在过去基本的帧缓冲和单色视频系统时代还是非常有用武之地的,但而今已经基本不需要了.CS_CLASSDC,CS_OWNDC和CS_PARENTDC用于对DC的特殊处理.你可能仅仅会使用CS_GLOBALCLASS注册一些诸如列表框,按钮和其它的一些标准控件.CS_HREDRAW和CS_VREDRAW样式在窗口内容依窗口大小按比例变化的可以变换大小的窗口中是非常有用的.


hCursor指定了所有从该WNDCLASS创建的窗口的"类指针",但鼠标指针在窗口客户区域上方移动时,Windows从窗口的WNDCLASS中找回类指针的句柄,并使用它画出指针.你能使用图标编辑器创建出自定义的指针,同时你也可以不自己创建,而使用系统预先定义好的系统指针.CWinApp::LoadStandardCursor载入一个系统指针,语句
AfxGetApp()->LoadStandardCurosr(IDC_ARROW);
返回的是大多数Windows应用程序都要使用的箭头指针.你可以在CWindApp::LoadStandardCursor或者::LoadCurosr函数的文档中找到系统指针的完整列表.一般说来,只有IDC_ARROW,IDC_IBEAM和IDC_CROSS是最常用的.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值