Solved

SubclassWindow Question

Posted on 2001-08-14
12
1,013 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

24 Experts available now in Live!

Get 1:1 Help Now