CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2005
    Posts
    230

    Question Unclear on HDC and SelectObject

    Hi people,

    Im currently working on a project that requires me to change the font. Im using
    Code:
    SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT));
    to do this and it works great.

    The msdn states the following:
    "This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object."

    I do not understand why it is neccary to replace the object with the original, whats not helping is that some examples i look at do replace the original back and others dont.

    Could someone help me with this?
    Thanks very much!!

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Unclear on HDC and SelectObject

    Handles, and especially DC's in general, are not an unlimited resource. If you use them up, you run the risk of your application failing.
    Gort...Klaatu, Barada Nikto!

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Unclear on HDC and SelectObject

    There are good examples and bad examples.

    Well, it's a long story. I'll try to explain by short without include all cases.
    If we got a common DC we have to release it when no more needed by calling ReleaseDC (as Mike already stated).
    For a similar reason we have to delete created GDI objects (pen, brushes, bitmaps, etc.) when no more needed (see DeleteObject).
    We must not delete objects currently selected in a DC.
    Also it's a very bad ideea to delete objects currently selected in a DC, after ReleaseDC call.
    So... call SelectObject to "set free" the GDI objects you have created, before release the DC and delete them.

    In your particular case you have selected a stock object (font) so deleting it is not necessary (anyhow calling DeleteObject has no effect) so you can let it inside.
    However, take this as a good practice: always "free" your objects from DC.

    Another easier way to do that is using SaveDC and RestoreDC:
    Code:
       int nSavedDC = SaveDC(hDC);
    // ...
       HBRUSH hBrushBlue = CreateSolidBrush(RGB(0,0,255));
       SelectObject(hDC, hBrushBlue);
    // ...
       RestoreDC(hDC, nSavedDC);
       DeleteObject(hBrushBlue);
    // ...
    NOTE: The device context has stock objects initially selected as for example BLACK_PEN.


    [ Redirected thread ]
    Last edited by ovidiucucu; February 17th, 2006 at 09:10 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Unclear on HDC and SelectObject

    I'd like to add one illustration to ovidiucucu's great explanation:
    if you pass the DC to another function (like to an object, to draw itself), the callee expects that DC to be returned in its original states. Othervise, it will continue to use your stock font (might not be desired).
    And one performance note: if you need to re-select more than two objects, go with SaveDC() / RestoreDC().
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Oct 2005
    Posts
    230

    Re: Unclear on HDC and SelectObject

    Wow, thats very much for the replys and thanku Odiucucu =) They have helped heaps.

    I may come accross a couple more similar questions, if so ill keep this updated.

    Cheeers to all!

  6. #6
    Join Date
    Oct 2005
    Posts
    230

    Re: Unclear on HDC and SelectObject

    Ok, thanks for all the help so far, i have just one more question (for now)..

    In all the examples i have seen the programe saves the dc, does the printing and then restores it and deletes brushes etc.. the programe does all this EVERY time is prints ...

    Is there any reason why they do it like this (it seems in-efficient) and why i could not not save the dc in a constructor and restore in the destructure while having a HDC member in the class? This way i only have to do it once instead of every time i paint..?

    Thanks again

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Unclear on HDC and SelectObject

    Quote Originally Posted by UnfitElf
    Is there any reason why they do it like this (it seems in-efficient) and why i could not not save the dc in a constructor and restore in the destructure while having a HDC member in the class? This way i only have to do it once instead of every time i paint..?

    Thanks again
    The main reason can be seen in three words: to "avoid being selfish".
    Anyhow the way you have suggested do not increase the efficiency at all (saving and restoring DC each time you paint take "infinitely" less time than painting itself).
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured