Solved

SubclassWindow Question

Posted on 2001-08-14
12
1,008 Views
Last Modified: 2013-12-03
I want to experiment with the SubclassWindow() function and here is what I did:

- Create a dialog based MFC app with a textbox (mapped to member variable m_myedit)

- Created a class CSubWnd derived from CWnd using classwizard. Didn't add any code to the class.

- In my dialog's class added a member CSubWnd m_test;

- In my dialog's OK button handler, add the following code to subclass the textbox's window.

     if(m_test.SubclassWindow(m_myedit.m_hWnd)) {
          AfxMessageBox("succeeded");
     }

I was hoping at least it should return TRUE. But instead, it gave assertion failure. May I know what is wrong here?

Thanks..
0
Comment
Question by:onlygo
  • 8
  • 4
12 Comments
 
LVL 2

Expert Comment

by:The Master
ID: 6383260

First of all, what's the assertion it's giving you?

Secondly, make sure that you're not calling the base class CDialog::OnOK() function in your OnOK() function.
0
 

Author Comment

by:onlygo
ID: 6383285
It's in wincore.cpp line 312:
     ASSERT(FromHandlePermanent(hWndNew) == NULL);
     // must not already be in permanent map

I am not calling the base CDialog::OnOK()
0
 
LVL 2

Accepted Solution

by:
The Master earned 100 total points
ID: 6383307

Don't map the textbox to a member variable (remove the variable m_myedit).  Then instead of calling...

m_test.SubclassWindow (m_myedit.m_hWnd)

call...

m_test.SubclassWindow (GetDlgItem (IDC_MYEDIT))

...(replacing IDC_MYEDIT with the dlg item id of your edit control).

Good luck!
0
 

Author Comment

by:onlygo
ID: 6383330
Thanks! The error is gone now. But it doesnt seem to have any effect after I added the WM_KEYDOWN message handler to my CSubWnd class. I mean I pressed key inside the textbox but my message handler does not seem to run at all. Also, what does the "permanent map" mean?
0
 
LVL 2

Expert Comment

by:The Master
ID: 6383356

MFC keeps a map (a one-to-one relationship table) of window handles to CWnd object pointers.  It's "permanent" in the sense that the CWnd object pointers are not temporary objects.  As soon as the object is destroyed, the handle-to-object relationship is removed from the map.  MFC was just making sure that the window handle wasn't already mapped to a CWnd object before it mapped it to your object.

As for not handling WM_KEYDOWN, I'm not sure about that.  It's been a while since I've used subclassing in MFC.  As far as I know, you should be getting it.  Try adding a handler for WM_CHAR and see if that works.
0
 
LVL 2

Expert Comment

by:The Master
ID: 6383383

MFC keeps a map (a one-to-one relationship table) of window handles to CWnd object pointers.  It's "permanent" in the sense that the CWnd object pointers are not temporary objects.  As soon as the object is destroyed, the handle-to-object relationship is removed from the map.  MFC was just making sure that the window handle wasn't already mapped to a CWnd object before it mapped it to your object.

As for not handling WM_KEYDOWN, I'm not sure about that.  It's been a while since I've used subclassing in MFC.  As far as I know, you should be getting it.  Try adding a handler for WM_CHAR and see if that works.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:The Master
ID: 6383395

MFC keeps a map (a one-to-one relationship table) of window handles to CWnd object pointers.  It's "permanent" in the sense that the CWnd object pointers are not temporary objects.  As soon as the object is destroyed, the handle-to-object relationship is removed from the map.  MFC was just making sure that the window handle wasn't already mapped to a CWnd object before it mapped it to your object.

As for not handling WM_KEYDOWN, I'm not sure about that.  It's been a while since I've used subclassing in MFC.  As far as I know, you should be getting it.  Try adding a handler for WM_CHAR and see if that works.
0
 

Author Comment

by:onlygo
ID: 6383397
ok thanks for ur help! i think i will go figure out why the WM_KEYDOWN is not working myself. thanks again.
0
 
LVL 2

Expert Comment

by:The Master
ID: 6383401

Wait...I remember now.  Instead of calling SubclassWindow() call SubclassDlgItem().  So your code would look like this...

if (m_test.SubclassDlgItem (IDC_MYEDIT, this)) {
...
}

This will work for you.  And you don't have to worry about calling UnsubclassWindow().

I hope this helps!

0
 
LVL 2

Expert Comment

by:The Master
ID: 6383413

Wait...I remember now.  Instead of calling SubclassWindow() call SubclassDlgItem().  So your code would look like this...

if (m_test.SubclassDlgItem (IDC_MYEDIT, this)) {
...
}

This will work for you.  And you don't have to worry about calling UnsubclassWindow().

I hope this helps!

0
 
LVL 2

Expert Comment

by:The Master
ID: 6383416

Wait...I remember now.  Instead of calling SubclassWindow() call SubclassDlgItem().  So your code would look like this...

if (m_test.SubclassDlgItem (IDC_MYEDIT, this)) {
...
}

This will work for you.  And you don't have to worry about calling UnsubclassWindow().

I hope this helps!

0
 

Author Comment

by:onlygo
ID: 6383434
works excellently.. thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

17 Experts available now in Live!

Get 1:1 Help Now