Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created an SDI application with CFormView view class that contains some basic controls. I need to know at a certain time if the Edit Boxes has gain the focus, so I called the function GetFocus(); & then called the function IsKindOf(RUNTIME_CLASS(CEdit)) from the returned CWnd* pointer. I never got an CEdit class with input focus.
So I implemented this line of code to diagnostic the issue,
AfxMessageBox(GetFocus()->GetRuntimeClass()->m_lpszClassName);
That line of code always displays to me a message box with CTempWnd text, & when I call this code:
AfxMessageBox(GetFocus()->GetRuntimeClass()->m_pBaseClass->m_lpszClassName); it displays to me a message box with CWnd text. & never the CEdit is returned.

Help me please to solve this issue. :sigh:
Posted

As Andrew said, it could be solved at the ID-level :) :
UINT uiEditIDs[] = {
  IDC_EDIT122,
  IDC_EDIT34,
  IDC_EDIT67,
  IDC_EDIT_NAME /*, ... */
};

bool CYourView::IsAnEditBoxFocused()
{
  bool bFocudes(false);
  CWnd* pcFocusedWnd(GetFocus());
  if (pcFocusedWnd->GetSafeHwnd()) {
    for (int i = 0; i < _countof(uiEditIDs) && !bFocused; i++) {
      bFocused = (uiEditIDs[i] == pcFocusedWnd->GetDlgCtrlId());
    }
  }
  return bFocused;
}
 
Share this answer
 
> Check the ID of the focus HWND is the ID of one of your edit controls.

Actually what I forgot to mention here is that there may be other windows with the same ID in the system (but there should not be windows with the same ID and same parent window). For example, if the user had open 2 of your forms there would be two sets of child windows for each form.

If you want to make sure you know exactly which edit control you are dealing with then you need to check its ID and also that its parent's HWND is the same as the HWND of the CFormView.
 
Share this answer
 
Thanks guys for your help. I also found another way to do this.
Is to call the GetClassName Function:
int WINAPI GetClassName(
  __in   HWND hWnd,
  __out  LPTSTR lpClassName,
  __in   int nMaxCount
);

& then compare the returned lpClassName with _T("Edit"), so if they are the same, then an Edit Box Control is getting the focus.
TCHAR szClassName[32];
GetClassName(GetFocus()->GetSafeHwnd(), szClassName, sizeof(szClassName));
if (!lstrcmpi(szClassName, _T("Edit")))
   // An Edit Box control has gain the focus. Do the appropriate code

NB: The purpose for this code is to Enable the (Undo, Cut, Copy & Paste) buttons from the main menu & the main toolbar if an Edit Box control has gain the focus & Disable them if no Edit Box control is currently having the focus.
 
Share this answer
 
v2
IsKindOf is an MFC facility that allows you to find the type of an object pointed to by a pointer. So if you have a CEdit * that is stored in a CWnd * you can tell that it is. This is like C++ RTTI but was invented before RTTI was added to C++.

If you have an edit control where you got the handle from Windows there is no class at all associated with it. I think when you create AQ CformView in MFC you get a class form the view but not for each of the child controls. You can associate a CEdit class with the window handle using SubclassWindow(), but you have to know that it is a handle for an edit control to begin with.

What you are trying to do given a Windows handle is find out the type of window/control that is behind that handle. This is not an MFC problem but a Windows problem. There are a few ways you could do this:

Check that the HWND of the focus window is the same as the HWND of one of your edit controls.

Check the ID of the focus HWND is the ID of one of your edit controls.

You can get Windows to work out if the control is an edit box, but from memory this is tricky.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900