3

I have for example IDC_EDIT1 C++ MFC Controls , How Can I get its handle ? By which Windows API , Can I done it ?

Adrian
  • 10,246
  • 4
  • 44
  • 110
Majed Maniat
  • 37
  • 1
  • 1
  • 5

3 Answers3

5

CWnd-derived objects have a GetSafeHwnd member function to retrieve the Windows handle of that object, so if your control is in a dialog you can retrieve the handle like this:

HWND hwnd = GetDlgItem(IDC_EDIT1).GetSafeHwnd();
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
3

You can get a handle to a control by calling the Windows API function 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:

HWND hWnd = NULL;
someWnd->GetDlgItem( IDC_EDIT1, &hWnd );
IInspectable
  • 46,945
  • 8
  • 85
  • 181
3

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. In that case, use pWnd = GetDlgItem(IDC_EDIT1).
  • Take it one step further and create a "control variable" for your IDC_EDIT1 using Class Wizard. That way you would have a class member variable associated with that edit control, and you would not need to get its handle.
dlmeetei
  • 9,905
  • 3
  • 31
  • 38
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27