简体   繁体   English

如何通过它的 id 获取 C++ MFC Control HANDLE?

[英]How to get C++ MFC Control HANDLE by it's id?

I have for example IDC_EDIT1 C++ MFC Controls , How Can I get its handle ?例如,我有 IDC_EDIT1 C++ MFC 控件,我如何获得它的句柄? By which Windows API , Can I done it ?通过哪个 Windows API,我可以做到吗?

CWnd派生对象有一个GetSafeHwnd成员函数来检索该对象的 Windows 句柄,因此如果您的控件在对话框中,您可以像这样检索句柄:

HWND hwnd = GetDlgItem(IDC_EDIT1).GetSafeHwnd();

You can get a handle to a control by calling the Windows API function GetDlgItem :您可以通过调用 Windows API 函数GetDlgItem来获取控件的句柄:

Retrieves a handle to a control in the specified dialog box.检索指定对话框中控件的句柄。

The CWnd::GetDlgItem class member of the CWnd class also has an overload to retrieve a control's handle:的CWnd ::函数GetDlgItem的类成员CWnd类也有过载来检索控件的句柄:

HWND hWnd = NULL;
someWnd->GetDlgItem( IDC_EDIT1, &hWnd );

I would like to throw my two cents in, too.我也想投入我的两分钱。

  • Since you are in MFC world, you might be better off with the pointer to CWnd than with the raw HWND.由于您在 MFC 世界中,使用指向 CWnd 的指针可能比使用原始 HWND 更好。 In that case, use pWnd = GetDlgItem(IDC_EDIT1) .在这种情况下,请使用pWnd = GetDlgItem(IDC_EDIT1)
  • Take it one step further and create a "control variable" for your IDC_EDIT1 using Class Wizard.更进一步,使用类向导为您的 IDC_EDIT1 创建一个“控制变量”。 That way you would have a class member variable associated with that edit control, and you would not need to get its handle.这样,您将拥有一个与该编辑控件关联的类成员变量,并且您不需要获取其句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM