Solved

How to save and restore GDI objects from TDC

Posted on 1998-01-27
7
314 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now