We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Using HDC.FromHandle and CDC::GetSafeHdc

psdavis
psdavis asked
on
Medium Priority
2,041 Views
Last Modified: 2013-11-19
OK, I'm trying to pass a CDC* to my ActiveX control by passing the HDC of the control and using it to display.  

Now to test my problem, I tested a few functions and tried to see if the CDC was really my new CDC or not.

TRACE1( "Map Mode: %d\n",  pDC->GetMapMode( ));

HDC hDC = pDC->GetSafeHdc( );
CDC* pTestDC = CDC::FromHandle( hDC );

TRACE1( "Map Mode: %d\n",  pTestDC->GetMapMode( ));

Hmmm! The mapping modes are now different! When printing, it went from MM_TEXT to MM_ANISOTROPIC.

SOOO... the way that I'm converting it doesn't work.  So how do I do it?

HDC hDC = pDC->GetSafeHdc( );
CDC pTestDC;
pTestDC.Attach( hDC );

Doesn't seem to work either.  Anyone?

Phillip

Comment
Watch Question

Commented:
What about using SaveDC and RestoreDC? Right in your code this will look like:

TRACE1( "Map Mode: %d\n",  pDC->GetMapMode( ));

HDC hDC = pDC->GetSafeHdc( );
int nSavedDC = SaveDC(hDC);
CDC* pTestDC = CDC::FromHandle( hDC );
RestoreDC(pTestDC->m_hDC, nSavedDC);

TRACE1( "Map Mode: %d\n",  pTestDC->GetMapMode( ));

This is not as elegant but at least solves the problem :-) You just have to pass to your control the value returned by SaveDC along with the HDC itself.

Author

Commented:
I'm already around the problem, so I'm not looking for a kludge.  I just really want to know how you're supposed to do it in the first place, but thanks.

Commented:
I looked the the Attach version of your test.
I looked at the code of CDC::Attach() and it seems that there shouldn't be a different result. Why don't you debug the functions deeper in the MFC code. Check if both HDCs in the first and second trace are the same (they should be). If they are not try to spot where and how the HDC gets changed (frankly, I don't see how this is possible looking at the MFC code). And finally if they are equal try to find out why the map mode changed during the transition.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I'll look into both comments.  Thanks guys.

To clarify, I'm eliminating worrying about the ActiveX passing until I figure out how to do it in just the code snipplet.  Then I'll worry.

HDC hDC = pDC->GetSafeHdc( );

Yes, I was passing the hDC.  To get around it, I'm currently passing the pDC as a long* to my ActiveX control.  Once it gets there, I'm casting it back to a CDC*.  Actually works great.  I'm doing it with the tooltips as well.  No problems.

Phillip

Author

Commented:
Answers2000,

So this is the final solution for this code snipplet?

// To get the two HDC

HDC hDC       = pDC->m_hDC;
HDC hAttribDC = pDC->m_hAttribDC;

// To convert it back

CDC* pTestDC = CDC::FromHandle( hDC );
pTestDC->SetAttribDC( hAttribDC );

// or alternatively

CDC pTestDC;
pTestDC.Attach( hDC );
pTestDC.SetAttribDC( hAttribDC );

// Correct?

Phillip

Yes I believe so Phillip,

I've been reading the CDC sources and it looks right to me, though I have to say I haven't actually tried a test project.

Let me know how you get on

Incidentally I believe I've figured out milenvk's work round works.  It's because the SaveDC get's the attribute DC's attributes saved in the main app, and the restore puts the attributes back in the ActiveX's attribute DC (which may not be the same attribute DC as in the main app).

The only funny thing is (as usual) the MFC doc's are less than 100% clear when the attribute DC won't be the same as the hDC.

As far as I can tell your final code should work in all cases though.

Author

Commented:
> Incidentally I believe I've figured out milenvk's work round works

I agree completely, but it was more of a intellectual question than a solution question.

Thanks,
Phillip
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.