Solved

How to save and restore GDI objects from TDC

Posted on 1998-01-27
7
347 Views
Last Modified: 2013-12-26
In the program that I am developing, it would be very convenient if I could save and then restore the state of various GDI objects at certain points in my code (without using SaveDC and RestoreDC). I would guess that I need to use

HANDLE GetCurrentObject(uint objectType) const;

and then convert the HANDLE into a TFont, TPen, ... but I don't know how to do that.
0
Comment
Question by:quinlan
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1299558
Why do you need to save the state of the DC? What is it exactly that you're trying to do?
0
 

Author Comment

by:quinlan
ID: 1299559
I have methods like:

void CalculateTextHeight( TDC &dc, const char *text )
{
   dc.SelectObject( GetTextFont( ) );
   TSize extent = dc.GetTextExtent( *text , strlen( *text ) );
   return extent.cx;
}

that are called by methods like:

void DoSomething( TDC &dc )
{
   dc.SelectObject( TFont( "Arial", 12 ) );
   
   ...
   ...
   CalculateTextHeight( dc, "Hello!" );
   ...
   ...
   ...
}

Notice how the call to CalculateTextHeight will screw up the font. Of course, I can simply reselect Arial 12 after making the call but I have a lot of methods like this and it is sloppy (from a software engineering point of view) to have to clean up after each of these calls.
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1299560
You are supposed to restore the DC's state when you're done:

  void CalculateTextHeight(TDC& dc)
  {
    dc.SelectObject(GetTextFont());
    // as above
    dc.RestoreFont();
  }

For everything that you can select into a DC, there is a corresponding RestoreXXX object in there. This way, you CalculateTextHeight() function puts the state of the DC back to what it was before you found it.

   
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:quinlan
ID: 1299561
That will not work if you change the object more than once because RestoreFont restores the font to the original DC font. In my example, it would not necessarily restore the font to Arial 12.
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 1299562
I think that it is a funny implementation Borland has done. The normal selectobject in Windows returns the previous handle. And then you use that handle to restor the font. One way you can do it with OWL is to use SaveDC() and RestoreDC(). I don't know how slow this is though. Because this should save and restore all. Not just what you change.

 Jens Yllman
 (http://www.uniweb.se/~jens/)
0
 

Author Comment

by:quinlan
ID: 1299563
There are some implementation problems with SaveDC and RestoreDC (apparently) that I am trying to avoid. Right now I do what you suggest but I want to avoid that.
0
 
LVL 2

Accepted Solution

by:
JensUniweb earned 200 total points
ID: 1299564
Another solution could be to use selectobject from the Windows API in this case. If you're just wanting this in small functions it would not mess up the TDC object. Example:

 int CalculateTextHeight( TDC& dc )
 {
   HANDLE tOldH = ::SelectObject( dc,GetTextFont() );
   TSize extent = dc.GetTextExtent( *text , strlen( *text ) );
   ::SelectObject( dc,tOldH );

   return extent.cy;
 }

 Jens Yllman
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

691 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