?
Solved

How to save and restore GDI objects from TDC

Posted on 1998-01-27
7
Medium Priority
?
353 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 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