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
Solved

SubclassWindow Question

Posted on 2001-08-14
12
1,024 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

837 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