[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to save and restore GDI objects from TDC

Posted on 1998-01-27
7
Medium Priority
?
364 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

872 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