在扩展CFileDialog时捕获ON_CBN_SELCHANGE和其他下拉事件

时间:2009-04-05 17:46:04

标签: events mfc combobox

我正在尝试使用C ++ / VS2008为XP创建自定义文件保存对话框。我的对话框将有三个下拉菜单和一个编辑框。我需要下拉列表的内容在其他下拉菜单上进行选择时动态更新。因此,我需要捕获CBN_事件。

我创建了一个扩展CFileDialog的类和一个扩展CComboBox的类。我可以显示自定义对话框(带有组合),但我无法捕获CBN_事件。

我使用VS'Create MFC DLL'向导来获取两个类:app类和自定义文件保存对话框类。我创建了一个派生的CComboBox类,所以我可以添加消息/事件陷阱代码。我在对话框类中输入了消息映射,DoDataExchange和事件捕获代码。

下面的代码是我正在使用的简略版本。我想,当我甚至无法获得一个下拉列表时,我不需要发布三个下拉列表的创建。我也没有为编辑框添加任何代码,因为我没有遇到问题。

一些代码:从app类 - 创建自定义对话框的实例,调用DoModal:

BOOL CSaveAsApp::InitInstance() 
{
CWinApp::InitInstance();
//  Parse the command line to get the defaultExtension and the file filter 
CString cmdLineAsString =   CString( theApp.m_lpCmdLine );

int curPos  =   0 ;
CString fileExtension   = cmdLineAsString.Tokenize(" ", curPos ) ;
CString fileFilter  = cmdLineAsString.Tokenize(" ", curPos ) ; ;


MyFileDialog    dlg( FALSE, fileExtension.GetBuffer(), "Enter File Name Here",
                    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
                    fileFilter.GetBuffer(), m_pMainWnd) ;


m_pMainWnd = &dlg;

int nResponse = dlg.DoModal();

if (nResponse == IDOK)
{
   ...  
}
else if (nResponse == IDCANCEL)
       ...

return FALSE;
}

以下是自定义文件保存对话框中的一些代码:

IMPLEMENT_DYNAMIC(MyFileDialog, CFileDialog ) 
//  LJM Put in last two parms: DWORD dwSize, BOOL bVistaStyle (0, 0 )
//      bVistaStyle = 0 ==> XP-Style dialog

MyFileDialog::MyFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, 
            LPCTSTR    lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, 
            CWnd* pParentWnd) :
    CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags,
            lpszFilter, pParentWnd, 0, 0) 
{ }

MyFileDialog::~MyFileDialog() { }

void MyFileDialog::DoDataExchange(CDataExchange* pDX)
{
   CFileDialog::DoDataExchange(pDX);
   //{{AFX_DATA_MAP(MyFileDialog)
   //DDX_Control(pDX, IDC_CONTEXT_COMBO, m_ComboContext);
   //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(MyFileDialog, CFileDialog)
ON_CBN_SELCHANGE(IDC_CONTEXT_COMBO, &MyFileDialog::OnCbnSelchangeUniqueNumber)
ON_CBN_SELENDOK(IDC_CONTEXT_COMBO, &MyFileDialog::OnCbnSelchangeUniqueNumber)
END_MESSAGE_MAP()

BOOL MyFileDialog::OnInitDialog() 
{
CFileDialog::OnInitDialog();

//AfxEnableControlContainer();

const UINT iExtraSize = 300;
// Get a pointer to the original dialog box.
CWnd *wndDlg = GetParent();
RECT Rect;


wndDlg->GetWindowRect(&Rect);
// Change the size of FileOpen dialog
wndDlg->SetWindowPos(NULL, 0, 0, 
                    Rect.right - Rect.left, 
                    Rect.bottom - Rect.top + iExtraSize,  
                    SWP_NOMOVE);

CWnd *wndComboCtrl = wndDlg->GetDlgItem(cmb1);   
wndComboCtrl->GetWindowRect(&Rect);
wndDlg->ScreenToClient(&Rect); // Remember it is child controls

Rect.top += 60;
Rect.bottom += 120;
Rect.left += 50;

m_ComboContext.Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | CBS_SORT | WS_VSCROLL | WS_TABSTOP, Rect, wndDlg, IDC_CONTEXT_COMBO);      


    m_ComboContext.SetFont(wndComboCtrl->GetFont(), TRUE);

        m_ComboContext.AddString(_T("Lou1") );
    m_ComboContext.AddString(_T("L432") );
    m_ComboContext.AddString(_T("Lou2") );
    m_ComboContext.AddString(_T("Lou3") );



return true ;
}

void MyFileDialog::OnCbnSelchangeUniqueNumber()
{
AfxMessageBox( _T("OnCbnSelchangeUniqueNumber"), 0, 0 ) ;
}

这是来自CComboBox的派生类:

   IMPLEMENT_DYNAMIC(LouComboBox, CComboBox)

   LouComboBox::LouComboBox() {  }

   LouComboBox::~LouComboBox() {  }

BEGIN_MESSAGE_MAP(LouComboBox, CComboBox)
ON_CONTROL_REFLECT(CBN_SELCHANGE, &LouComboBox::OnCbnSelchange)
ON_CONTROL_REFLECT(CBN_KILLFOCUS, &LouComboBox::OnCbnKillfocus)
ON_CONTROL_REFLECT(CBN_SELENDCANCEL, &LouComboBox::OnCbnSelendcancel)

ON_CBN_SELCHANGE(IDC_CONTEXT_COMBO, &LouComboBox::OnCbnSelchangeUniqueNumber)
END_MESSAGE_MAP()

void LouComboBox::OnCbnSelchange()
{
// TODO: Add your control notification handler code here
AfxMessageBox( _T("Menu selected"), 0, 0 ) ;
}

void LouComboBox::OnCbnKillfocus()
{
// TODO: Add your control notification handler code here
AfxMessageBox( _T("Lost focus"), 0, 0 ) ;
}

void LouComboBox::OnCbnSelendcancel()
{
AfxMessageBox( _T("OnCbnSelendcancel"), 0, 0 ) ;
}

void LouComboBox::OnCbnSelchangeUniqueNumber()
{
// TODO: Add your control notification handler code here    
AfxMessageBox( _T("OnCbnSelchangeUniqueNumber"), 0, 0 ) ;
}

我尝试过对话框类有messagemap / DoDataExchange的版本,ComboBox类有它们,两个类都有它们。

我能够捕获一些事件 - 但没有CComboBox。我可以让对话框响应ON_NOTIFY,ON_WM_DESTROY,编辑框响应ON_WM_KILLFOCUS。

我有这种唠叨的想法,这是一个前额扒手,我太接近这个,看不到明显的。

非常感谢任何帮助,协助,指导。

1 个答案:

答案 0 :(得分:1)

您需要为对话框提供一个钩子过程来捕获消息。详情请见http://msdn.microsoft.com/en-us/library/windows/desktop/ms646931%28v=vs.85%29.aspx