• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1245
  • Last Modified:

Getting c++ class name after calling GetFocus() !

How is it possible to get the C++ class name of the CWnd object returned by GetFocus() ?

Or

What other solution are possible on how to correctly cast the CWnd object returned if you don't know his type ?

I have tried to use GetRuntimeClass() but the information stored in m_pszClassName is not usuable in my case because the control is subclassed on a CStatic and the runtime returns me 'CWnd'.

But if I look in the debug watch view, I can see the base C++ class name which is not my C++ class name but the parents one.

Can someone help me but without using Type Lib !
0
mike_marquet
Asked:
mike_marquet
  • 7
  • 5
  • 4
  • +3
1 Solution
 
AndyAinscowFreelance programmer / ConsultantCommented:
GetDlgCtrlID will return the ID of the window.  I assume the window is in your own app so you should 'know' which ID's are edits, which are buttons and so on
0
 
mike_marquetAuthor Commented:
the control is a grid control. The application has more than 1 of this control.

Some controls are also in a separated DLL.

The think I want to do, is that I can print the current selected grid where else it is (app, dll, ...).

=> If  the current focused control is a grid than I can print it

=> If  the current focused control is a listctrl than I can print it

=> Else I can not print it

Therefore, I can not use the ID
0
 
SteHCommented:
Would the function IsKindOf () returning the runtime class help you?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AndyAinscowFreelance programmer / ConsultantCommented:
??

=> If  the current focused control is a grid than I can print it

=> If  the current focused control is a listctrl than I can print it

=> Else I can not print it

Therefore, I can not use the ID

Do you mean your ID's are not unique, or that you do not know what the ID's are?
If you know a) what the ID's are and b) that they are unique then you can identify each window with the focus that you could print.
0
 
mike_marquetAuthor Commented:
I already tried using CRuntimeClass but it's not the good data

The IDs are not unique AND I don't know what it is.
0
 
SteHCommented:
What problems are you facing with IsKindOf and CRuntimeclass?

Otherwise you could enable RTTI (RunTime Type Information) and use typeid and type_info.
0
 
mike_marquetAuthor Commented:
For example :

My class is called CMyGrid which is derived from CUGCtrl (DUNDAS)

CRuntimeClass *pRuntimeClass = GetFocus()->GetRuntimeClass()

In pRuntimeClass->m_lpszClassName I have CWnd and not CMyGrid
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Are you using the following in your class?


DECLARE_DYNAMIC( class_name )

Parameters

class_name

The actual name of the class (not enclosed in quotation marks).

Remarks

When deriving a class from CObject, this macro adds the ability to access run-time information about an object’s class.

Add the DECLARE_DYNAMIC macro to the header (.H) module for the class, then include that module in all .CPP modules that need access to objects of this class.

If you use the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros as described, you can then use the RUNTIME_CLASS macro and the CObject::IsKindOf function to determine the class of your objects at run time.

If DECLARE_DYNAMIC is included in the class declaration, then IMPLEMENT_DYNAMIC must be included in the class implementation.

0
 
mike_marquetAuthor Commented:
No, I have tried to use it without success => compilation error

The error is because the base class (from DUNDAS, not my code) is not using it
0
 
SteHCommented:
If you have ths source from CUGCtrl you can implement it there as well, Otherwise RTTI is still an option.
0
 
mike_marquetAuthor Commented:
Source code is provided but doesn't what to modify it because if I get a new version, must than recode it.
0
 
SteHCommented:
PS: Can you ask the author to implement this. It should be done for a MFC class anyhow.
0
 
ZoppoCommented:
Hi mike_marquet,


IMO the problem simply is that function GetFocus (as lot of other MFC functions) returns a pointer to a temporary CWnd object
(which in fact really is a CTempWnd) with the HWND of the window attached ... this is because MFC is just wraps WinAPI calls
which of course can't have any knowledge about your classes. So IMO there's no chance to get what you want using C++ RTTI or
CRuntimeClass ...

If you have access to the pointers to your objects representating the windows in question you can try to find among them the window
with the HWND of the window returned by GetFocus()...

Hope that helps,

ZOPPO

0
 
mike_marquetAuthor Commented:
It's not helping me but seems that there is no solution
0
 
ZoppoCommented:
hm ... don't understand ... you just need to keep i.e. a list of pointers to windows you created and
query this list to find a pointer which has the HWND returned by GetFocus attached ... this is quite simple.
0
 
SteHCommented:
He should be able to get a pointer to the CWnd with a call to CWnd::FromHandlePermanent using the window handle HWND. Not sure whether this is just the same CWnd object GetFocus returns.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
With your grid control.  You are aware a of grid could have an edit control that they position over the 'cell' that has the focus that the user actually does the editing in, not the grid control itself.  (I think the grid from dundas behaves that way).  If that is the case then the focus of the 'grid' actually is a window of type CEdit.
0
 
aphillipsCommented:
First, forget type info stored by MFC or C++ runtime (IsKindof/C++ RTTI).  As Zoppo pointed out CWnd::GetFocus justs wraps a temporary CWnd (or CTempWnd) around a HWND.  How can the runtime possibly work out what the original class that created the window was?

You might be able to uniquely identify a window by its title/text (GetWindowText) or its style etc.  However, I recommend that when you create the windows you want to identify you assign a unique value to the 32 bit user data that you can attach to a HWND (using SetWindowLong with GWL_USERDATA).  Then you can later check that value with GetWindowLong.
0
 
mike_marquetAuthor Commented:
Making a list of pointers is not possible in this case because application is made of some DLLs.

The MFC extention DLL contains some dialog boxes which appears in the application. Therefore its difficult to manage a list of pointers within my application which has no idea of what is in my DLL.

Regarding using SetWindowLong using GWL_USERDATA was an idea I have also had but was search for a more flexible solution first.

The thing I have temporary made is that the my subclassed grid (I have made) is sending a registrered message when it lose or become focus. This solution is working.
0
 
Computer101Commented:
PAQed, with points refunded (76)

Computer101
E-E Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now