Solved

How to save and restore GDI objects from TDC

Posted on 1998-01-27
7
328 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

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