C ++ MFC如何使用GetDlgItem() [英] C++ MFC how to use GetDlgItem()
问题描述
这在主"example dlg.cpp"文件中:
This is in the main "example dlg.cpp" file:
void CHelixV3Dlg::OnBnClickedCancel()
{
CEdit* editbox = (CEdit*)GetDlgItem(IDC_EDIT1);
//works fine, defined as: *CWnd GetDlgItem(int nID); in this file
}
这是test.cpp源文件
This is test.cpp source file
void test()
{
CEdit* editbox = (CEdit*)GetDlgItem(IDC_EDIT1);
//does not work at all, seems to be a winAPI function instead of MFC...
//defined as: HWND __stdcall GetDlgItem(HWND hDlg, int nIDDlgItem);
}
两个源文件都在同一个项目中,使用相同的头,但是test()的GetDlgItem显然是Win32 API函数,在MFC中不起作用. 我该如何在test.cpp文件中使用GetDlgItem()?
both source files are in the same project, use the same headers, but test()'s GetDlgItem is obviously a Win32 API function, which does not work in MFC... How could I get GetDlgItem() working in the test.cpp file?
推荐答案
您不了解C ++范围规则.
You don't understand C++ scoping rules.
在第一次使用时,您最终会调用CWnd :: GetDlgItem(),因为您是从CHelixV3Dlg进行调用的.您的对话框类是从CDialog派生的,而CDialog是从CWnd派生的.使用普通的C ++范围规则,如果有一个成员函数以GetDlgItem作为其名称,它将被使用.
In your first usage, you end up calling CWnd::GetDlgItem() because you are making your call from CHelixV3Dlg. Your dialog class is derived from CDialog which is derived from CWnd. Using normal C++ scoping rules, if there is a member function that has GetDlgItem as its name, it will be the one to be used.
在第二种用法中,您最终按照WINAPI标头中的定义调用GetDlgItem().那是因为CWnd :: GetDlgItem()不在您的范围内.
In your second usage, you end up calling GetDlgItem() as defined in the WINAPI headers. That is because CWnd::GetDlgItem() is not in your scope.
如果要在第二次使用中调用CWnd版本,则必须以某种方式获取指向已创建并具有有效HWND的CHelixV3Dlg实例的指针或引用.有了该指针后:
If you want to call the CWnd version in your second usage, somehow you will have to get a pointer or reference to an instance of CHelixV3Dlg that has been created and has a valid HWND. Once you have that pointer:
void test(CHelixV3Dlg* pDlg)
{
CEdit* editbox = (CEdit*)pDlg->GetDlgItem(IDC_EDIT1);
// do some stuff with editbox...
}
这篇关于C ++ MFC如何使用GetDlgItem()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!