Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SubclassWindow Question

Posted on 2001-08-14
12
Medium Priority
?
1,072 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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