Solved

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

Posted on 2004-04-21
22
1,079 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
22 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
Would the function IsKindOf () returning the runtime class help you?
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
??

=> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
If you have ths source from CUGCtrl you can implement it there as well, Otherwise RTTI is still an option.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:mike_marquet
Comment Utility
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
Comment Utility
PS: Can you ask the author to implement this. It should be done for a MFC class anyhow.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
It's not helping me but seems that there is no solution
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQed, with points refunded (76)

Computer101
E-E Admin
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to calculate times for developing software? 8 60
ADO Memory leak with DELPHI 2007 37 153
Perl Awk Need Help 3 94
unix example issues 18 44
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now