Solved

SubclassWindow Question

Posted on 2001-08-14
12
1,018 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to have excel show file name on the title bar 4 50
Mixed results 10 85
Need a complete list of ASP.NET page load events 3 47
Licensing Sharepoint 2016 for developers 11 47
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.  …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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