动态子类化CComboBox以得到子控件EDIT及LISTBOX

类别:VC语言 点击:0 评论:0 推荐:

动态子类化CComboBox以得到子控件EDIT及LISTBOX

Joise.LI写于2004-4-6

 

ComboBox是比较常用的一个控件,有三种样式:CBS_SIMPLE(简单),CBS_DROPDOWN(下拉),CBS_DROPDOWNLIST(下拉列表),其中下拉列表样式是不允许输入的,简单样式是永远都显示下拉框的。

ComboBox是由一个ListBox和一个Edit框组合而成。本文将以限制只能输入电话号码为例介绍子类化过程得到Edit框(类似的可以得到ListBox)。

电话号码只能有数字和“-”和“+”组成,经查ASCII表,知道对应的ASCII值为48-57及45和43共12个值。

好,闲话暂停,开始子类化之旅。

第一步     先建立一个MFC应用程序工程,取名叫UseSuperCombox(这里表示一下SORRY,因为顺手多写了一个X,呵呵)。

第二步     新建一个MFC类,选择继承自CEdit,取名叫CSuperEdit,这个类是用来替换ComboBox中的Edit框的。代码如下:

²      SuperEdit.h:

class CSuperEdit : public CEdit

{

  DECLARE_DYNAMIC(CSuperEdit)

 

public:

  CSuperEdit();

  virtual ~CSuperEdit();

 

protected:

  DECLARE_MESSAGE_MAP()

public:

  afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);

};

 

²       SuperEdit.cpp:

// SuperEdit.cpp : 实现文件

//

 

#include "stdafx.h"

#include "UseSuperCombox.h"

#include "SuperEdit.h"

 

 

// CSuperEdit

 

IMPLEMENT_DYNAMIC(CSuperEdit, CEdit)

CSuperEdit::CSuperEdit()

{

}

 

CSuperEdit::~CSuperEdit()

{

}

 

 

BEGIN_MESSAGE_MAP(CSuperEdit, CEdit)

  ON_WM_CHAR()

END_MESSAGE_MAP()

 

// CSuperEdit 消息处理程序

void CSuperEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{

  if ( (nChar < 48 || nChar > 57) && nChar != 45 && nChar != 43 )

  {

       AfxMessageBox( "you must type the number or the char '-' or the char '+'" );

       return;

  }

 

  CEdit::OnChar(nChar, nRepCnt, nFlags);

}

 

第三步     再新建一个MFC类,选择继承自CComboBox,取名叫CSuperComboBox。代码如下(注:以下代码原文见http://support.microsoft.com/default.aspx?scid=kb;en-us;Q174667):

²       SuperComboBox.h

#pragma once

#include "SuperEdit.h"

class CSuperComboBox : public CComboBox

{

  DECLARE_DYNAMIC(CSuperComboBox)

 

public:

  CSuperEdit    m_Edit;

 

public:

  CSuperComboBox();

  virtual ~CSuperComboBox();

 

protected:

  DECLARE_MESSAGE_MAP()

public:

  afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

  afx_msg void OnDestroy();

};

²        SuperComboBox.cpp

// SuperComboBox.cpp : 实现文件

//

 

#include "stdafx.h"

#include "UseSuperCombox.h"

#include "SuperComboBox.h"

 

 

// SuperComboBox

 

IMPLEMENT_DYNAMIC(CSuperComboBox, CComboBox)

CSuperComboBox::CSuperComboBox()

{

}

 

CSuperComboBox::~CSuperComboBox()

{

}

 

// SuperComboBox 消息处理程序

BEGIN_MESSAGE_MAP(CSuperComboBox, CComboBox)

     ON_WM_CTLCOLOR()

     ON_WM_DESTROY()

END_MESSAGE_MAP()

 

HBRUSH CSuperComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

     if ( nCtlColor == CTLCOLOR_EDIT )

     {

         if ( m_Edit.GetSafeHwnd() == NULL )

         {

              m_Edit.SubclassWindow( pWnd->GetSafeHwnd() );

         }

//Such Code don't use in this example,but you can use it if you need the ListBox Control

         //else if (nCtlColor == CTLCOLOR_LISTBOX)

         //{

         //   //ListBox control

         //   if (m_listbox.GetSafeHwnd() == NULL)

         //       m_listbox.SubclassWindow(pWnd->GetSafeHwnd());

         //}

 

     return CComboBox::OnCtlColor( pDC, pWnd, nCtlColor );  

}

 

void CSuperComboBox::OnDestroy()

{

     if ( m_Edit.GetSafeHwnd() != NULL )

     {

         m_Edit.UnsubclassWindow();

     }

 

     CComboBox::OnDestroy();

}

注意:

1.   该类中使用了消息WM_CTLCOLOR,通过判断消息的参数nCtlColor是否等于CTLCOLOR_EDIT来将参数pWnd与对应的控件关联起来,本例仅需要得到Edit框,所以将取得LISTBOX的代码注释掉了。

2.   OnCtlColor是CWnd的消息处理函数,用于当框架描绘子控件时调用。只有使用该消息处理函数可以得到子控件的句柄。

3.   SubclassWindow可以动态的子类化一个窗口并且将自己附加到该CWnd对象。

4.   调用SubclassWindow需要在关闭之前调用UnsubclassWindow。该函数可以将WndProc回到初始状态并且令子类化窗口与CWnd对象脱离。

5.   使用该方法动态子类化时对话框必须先被至少描绘一次。如果对话框窗口未被描绘(如在对话框显示之前就关闭或者隐藏它),该方法将不适合。(原文:Note that for subclassing to occur, the dialog box must be painted at least once. There are cases when the dialog box doesn't paint at all (for example, closing the dialog box before it is displayed, hidden dialog boxes). This method may not be suitable when access to the subclassed windows are needed in these cases. )

 

第四步     在对话框类的OnInitDialog中添加以下代码:

     RECT rect;

     rect.top = 20;

     rect.bottom = 420;

     rect.left = 20;

     rect.right = 180;

     m_Combo.Create( WS_CHILD|CBS_DROPDOWN, rect, this, IDC_COMBO1 );

     m_Combo.ShowWindow( SW_NORMAL );

     m_Combo.UpdateWindow();

 

第五步     编译、运行。

 

另,本文的关键不在于限制输入,而在于取得ComboBox控件的子控件Edit或ListBox,如果仅是为了限制输入使用重载CBN_EDITCHANGE代码将会是更快捷的办法(代码来自CSDN网友ymbymb(毛病大哥),原文见http://expert.csdn.net/Expert/topic/2931/2931917.xml?temp=.3829462 )。

void CTestDlg::OnEditchangeCombo1()

{

     CString  str;

     m_Combo1.GetWindowText(str);

     int len = str.GetLength();

     if(str[len-1] < '0' || str[len-1] >'9')

     {

         str = str.Left(len-1);

         m_Combo1.SetWindowText(str);

     }

}

 

总结:

以上是我对子类化CComboBox的一些理解,程序运行在VS2002(MFC7.0)+WINXP PROFESSIONAL,编译通过,运行正确。谢谢大家的阅读,敬请指正。

如有需要源程序或任何意见或建议请联系我:[email protected]  http://joise.126.com

本文地址:http://com.8s8s.com/it/it1035.htm