091113(星期五)Deriving Controls from a Standard Control

本文介绍了如何通过继承现有的控件类来创建自定义控件,包括覆盖Create成员函数以提供必要的参数、提供消息处理函数以修改特定Windows消息的行为、添加新成员函数以扩展功能等步骤。同时讲解了如何在对话框中使用这些自定义控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1113

 

As with any CWnd-derived class, you can modify a control's behavior by deriving a new class from an existing control class.

 

To create a derived control class

 

Derive your class from an existing control class and optionally override the Create member function so that it provides the necessary arguments to the base-class Create function.

Provide message-handler member functions and message-map entries to modify the control's behavior in response to specific Windows messages. See Mapping Messages to Functions.

Provide new member functions to extend the functionality of the control (optional).

Using a derived control in a dialog box requires extra work. The types and positions of controls in a dialog box are normally specified in a dialog-template resource. If you create a derived control class, you cannot specify it in a dialog template since the resource compiler knows nothing about your derived class.

 

To place your derived control in a dialog box

 

Embed an object of the derived control class in the declaration of your derived dialog class.

Override the OnInitDialog member function in your dialog class to call the SubclassDlgItem member function for the derived control.

SubclassDlgItem "dynamically subclasses" a control created from a dialog template. When a control is dynamically subclassed, you hook into Windows, process some messages within your own application, then pass the remaining messages on to Windows. For more information, see the SubclassDlgItem member function of class CWnd in the MFC Reference. The following example shows how you might write an override of OnInitDialog to call SubclassDlgItem:

 

BOOL CMyDialog::OnInitDialog()

{

    CDialog::OnInitDialog();

    m_wndMyBtn.SubclassDlgItem(IDC_MYBTN, this);

    return TRUE;

}

Because the derived control is embedded in the dialog class, it will be constructed when the dialog box is constructed, and it will be destroyed when the dialog box is destroyed. Compare this code to the example in Adding Controls By Hand.

继承一个控件或许有控件处理自身消息的答案。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值