CreateCompatibleDC() vs RestoreDC

This is a discussion on CreateCompatibleDC() vs RestoreDC within the Windows Programming forums, part of the Platform Specific Boards category; Came across the function RestoreDC()... tried using it in the folowing code Code: hBmp = RadarBgrnd.GetHandle(); hMemDC = CreateCompatibleDC(0); hOldBmp ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    CreateCompatibleDC() vs RestoreDC

    Came across the function RestoreDC()... tried using it in the folowing code

    Code:
    hBmp = RadarBgrnd.GetHandle();
    hMemDC = CreateCompatibleDC(0);
    hOldBmp = SelectObject(hMemDC, hBmp);	
    
    //Save the DC to be used later
    savedDC = SaveDC(hMemDC);			
    			
    SelectObject (hMemDC, hOldBmp);					
    DeleteDC(hMemDC);	
    
    // Restore the saved DC and draw line on it
    RestoreDC(hMemDC, savedDC);						
    
    MoveToEx(hMemDC, coord.x,coord.y, &myPoint);		
    LineTo(hMemDC, coord.x+factor.x,coord.y+factor.y);	
    
    SelectObject (hMemDC, hOldBmp);					
    DeleteDC(hMemDC);									
    RadarBgrnd.Repaint ();
    Really this is just to test if Restore DC works... Results are as if hMemDC to draw to does not exist because no line is drawn... Is RestoreDC correctly used in the above code?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ya know what, I was looking at the platform docs and its not so much that you are "doing it wrong" so much that it does something different than what you think it does.

    Its supposed to retain information about the HDC not the HDC itself. Thus, what you are doing does what you told it to do (which was save information about drawing mode, pens used, etc.) however, you just thought it was going to store your HDC for later use (which it does not).

    Don't be affraid to download the platform docs, btw. If you don't have high speed net just use http://msdn2.microsoft.com (which practically requires high speed due to server lag )

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by master5001 View Post
    what you are doing does what you told it to do (which was save information about drawing mode, pens used, etc.) however, you just thought it was going to store your HDC for later use (which it does not).

    Oh! then how can i retain an original Bitmap DC? say i create a CompitableDC of a bitmap, draw a line on that bitmap, then delete DC (this done in a function).. By calling the function for the second time (or looping it) would like to continue drawing a different line but on an original Bitmap (one without a drawing)... My idea was to create a DC of the bitmap, save a copy as copiedDC, draw on draw something originalDC and delete. Next time i call the function i just need to restore the copiedDC with nothing drawn....

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Device contexts can be a screwy game. As you mess with them more and more you will swiftly realize their biggest draw back is creating them is a slow deal. So what I find useful is to do your CreateCompatibleDC() method but do all the drawing on the HDC of the window. The fewer HDCs you need to create (particularly in real-time) the better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateCompatibleBitmap & CreateCompatibleDC
    By XSquared in forum Windows Programming
    Replies: 7
    Last Post: 08-21-2003, 02:53 AM
  2. MFC, CreateCompatibleDC question.
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 05-13-2003, 01:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21