Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-21
22
Medium Priority
?
1,210 Views
Last Modified: 2013-11-20
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
Comment
Question by:mike_marquet
  • 7
  • 5
  • 4
  • +3
20 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10876585
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
 

Author Comment

by:mike_marquet
ID: 10876907
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
 
LVL 13

Expert Comment

by:SteH
ID: 10876960
Would the function IsKindOf () returning the runtime class help you?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10876975
??

=> 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
 

Author Comment

by:mike_marquet
ID: 10877281
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
 
LVL 13

Expert Comment

by:SteH
ID: 10877434
What problems are you facing with IsKindOf and CRuntimeclass?

Otherwise you could enable RTTI (RunTime Type Information) and use typeid and type_info.
0
 

Author Comment

by:mike_marquet
ID: 10877516
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10877562
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
 

Author Comment

by:mike_marquet
ID: 10877757
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
 
LVL 13

Expert Comment

by:SteH
ID: 10877832
If you have ths source from CUGCtrl you can implement it there as well, Otherwise RTTI is still an option.
0
 

Author Comment

by:mike_marquet
ID: 10878079
Source code is provided but doesn't what to modify it because if I get a new version, must than recode it.
0
 
LVL 13

Expert Comment

by:SteH
ID: 10878232
PS: Can you ask the author to implement this. It should be done for a MFC class anyhow.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 10878756
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
 

Author Comment

by:mike_marquet
ID: 10878951
It's not helping me but seems that there is no solution
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 10879030
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
 
LVL 13

Expert Comment

by:SteH
ID: 10879407
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10881579
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
 
LVL 3

Expert Comment

by:aphillips
ID: 10884765
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
 

Author Comment

by:mike_marquet
ID: 10885860
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12049878
PAQed, with points refunded (76)

Computer101
E-E Admin
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question