Click here to Skip to main content
12,694,121 members (35,413 online)
Click here to Skip to main content
Add your own
alternative version


112 bookmarked

Device Context Utilities

, 24 Jan 2001
Rate this:
Please Sign up or sign in to vote.
A class that encapsulates some useful, GUI related, static functions
  • Download source files - 487 Kb
  • Download demo application - 141 Kb


    Hundreds of my applications <grin> use GUI functions to a greater or lesser degree. I get a huge kick out of writing anything that makes a visual impact. It must be by virtue of the fact that whilst being technical, we can also be creative. MFC is fantastic, IMHO, since it provides a framework to free you from scribbling code targeted at the "underlying" architecture, and frees you to write the code you need to efficiently extend what already exists.

    I've learned a few things while compiling this code. NOTE the word "compiling"! While it's true that I wrote some of the stuff, a lot of it was adapted (which was sometimes more difficult than writing!). I recommend a visit to It's an awesome site, and has only ONE problem.... Visit it and find out! I owe a HUGE thanks to the guys who put that site together.

    Class Structure

    I've tried as hard as I could to keep the GUI functions as simple to use as possible. I like to be able to make ONE call and have it perform the task I required. This hasn't always been possible or efficient. For instance, there are times when a massive image, such as a snapshot of your entire screen, should be kept aside and reused at the expense of accuracy, rather than re-retrieving the image every time a change occurs.

    I don't doubt that many will question my methods/techniques/proficiency/etc. While we are by nature challenging and competitive creatures, please keep in mind:

    I wrote this firstly for myself, and as should follow any discovery of anything useful, I want to share it. There's heaps missing and I bet we could make a massively cool library of GUI functions. If you find a bug, or have an idea/code for another cool GUI function, please let me know!

    The Functions and their Parameters

    A couple of functions are not documented because they are either extremely self explanatory or because they are very simple helper functions for the more complex DC Utilities present in the class.

    Most of the functions have in common the following two parameters, and I won't repeat the documentation of their use unless I forget that I've told you this or because I really NEED to ;)

    void CDCUtils::SomeFunction( CDC* pDC, CRect area, ..Other params.. )

    The pointer to a CDC class is the context on which the drawing will take place. In my sample app, I have consistently supplied a "Memory DC", of which the ubiquitous CMemDC is the finest (Written by Kieth Rule).

    void CDCUtils::SomeFunction( CDC* pDC, CRect area, ..Other params.. )

    The CRect supplied is the TARGET AREA for the work you want performed.

    The Class, as it stands

    class CDCUtils 
        // Helper Functions and variables:
        static CBitmap  m_bmpDesktop;
        static double   DegreeToRadian( ... );
        static CDC* GetDCFromBmp( ... );
        static CDC* GetDCFromBmp( ... );
        static CDC* CreateBitmappedDC( ... );
        //   "Worker Functions"
        static void <A href="">GetDesktopImage</A>


    static void CDCUtils::PaintHatch(CDC* pDC, CRect area, 
            COLORREF crFG, COLORREF crBG)


    The colour of the grid (or hatch).


    The background colour.


    static void TileBitmap( CDC *pDC, CRect rect, UINT uResourceID );

    UINT uResourceID

    The BMP resource from your VC project that contains your to-be-tiled bitmap.


    static void DrawText(
            CDC* pDC, 
            CPoint Location,    
            CString strText, 
            CString strFontName, 
            int nFontWidth, 
            int nFontHeight, 
            COLORREF crFontColour, 
            long FontWeight=FW_BOLD);

    CString strText

    The text that will be displayed.

    CString strFontName

    The name of the font you want used. e.g. "Arial". (I hope this isn't too difficult...)

    int nFontWidth

    I'm not going to waste time typing more than four words to describe what potentially this function could efficiently produce for any equanimous employment of it's propagated functionality.

    int nFontHeight


    COLORREF crFontColour

    I commonly supply RGB info using the RGB() function.

    long FontWeight

    Specify either FW_BOLD, FW_NORMAL, etc.


    static void Draw3DRect(    CDC* pDC, 
                CRect& area, 
                bool bDeflate=false, 
                COLORREF crFill=-1, 
                COLORREF crHilite=-1, 
                COLORREF crLoLite=-1);

    bool bDeflate

    This is for me a simple but useful parameter. supplying a true will result in the Crect param being deflated by CSize(1,1). This is usefulwhen the next function you call, uses the same destination rectangle area, but will NEED to be deflated to prevent erasing the border that was previously drawn.

    COLORREF crFill

    RGB value that will be used to perform a solid fill of the specified rectangle. The default value specifies that no fill is required.

    COLORREF crHilite

    RGB value used for the "Highlight" part of the rectangle. Default results in the use of ::GetSysColor(COLOR_BTNHILIGHT)

    COLORREF crLoLite

    RGB value used for the "Lowlight" part of the rectangle. Default results in the use of ::GetSysColor(COLOR_BTNSHADOW)


    static void LinearGradient( CDC *pDC, 
                CRect &rect, 
                COLORREF clrFrom, 
                COLORREF clrTo, 
                BOOL bHorizontal = TRUE);

    COLORREF crFrom

    RGB value used for the "starting" colour.


    RGB value used for the "end" colour.

    BOOL bHorizontal

    Default is TRUE. Specifies whether you want to spread the gradient horizontally or vertically (example uses TRUE).


    static void DrawElectricity( CDC *pDC, CRect rect );

    I "lifted" this from a recent addition to CodeProject: CCreditsCtrl - An advanced About Box - Marc Richarme. He apologized for it's lameness, but I don't, I think it's cool. The demo executable does this a bit more justice because the time randomizes the result and makes it more.... electric ;)


    static void RadialGradient( CDC *pDC, 
                CRect &rect, 
                COLORREF clrFrom, 
                COLORREF clrTo );

    COLORREF crFrom

    RGB value used for the "starting" colour.


    RGB value used for the "end" colour.


    static void DrawSineWave(   CDC *pDC, 
                CRect rect, 
                double dwCycles, 
                COLORREF crPen = 0x000 );

    double dwCycles

    Determines how many full cycles will be drawn into the rect provided.

    COLORREF crPen

    RGB value used for the drawing of the wave. Default is black


    static void PaintDesktop( CDC *pDC, 
                CWnd* pWnd, 
                CRect SourceRect, 
                CRect DestRect);

    CWND* pWnd

    The calling window would supply the CWnd pointer in whic the draw will take place (typically a 'this' in the OnDraw() function).

    CRect SourceRect

    Specify what part of the desktop image you want to 'paint'.

    CRect DestRect

    Specify where on the supplied CDC* do you want to paint your desktop image.


    static void BlitBitmap(   CDC *pDC,
                CRect rect, 
                UINT uResourceID, 
                COLORREF crMask=RGB(0,0,0), 
                double dDegreesRotation=0 );

    UINT uResourceID

    The resource ID of the bitmap residing in your application.

    COLORREF crMask

    RGB value used to determin what sould be made transparent.

    double dDegreesRotation

    Allows rotation of the bitmap. Values over 360 are "wrapped".


    static void PaintBitmap(  CDC *pDC,
                CWnd* pWnd, 
                CRect rect, 
                UINT uBmpID, 
                bool bStretchToFit  = true, 
                bool bOverlay   = false );

    CWND* pWnd

    The calling window would supply the CWnd pointer in whic the draw will take place (typically a 'this' in the OnDraw() function).

    UINT uBmpID

    The resource ID of the bitmap residing in your application.

    bool bStretchToFit

    Determines what the parameter implies (no offence intended;).

    bool bOverlay

    Tells the function to use the SRCAND instead of SRCCPY when performing the blitting operation.


    static void MakeGlass(    CDC *pDC, 
                CWnd* pWnd, 
                CRect rect, 
                UINT uBmpID );

    This function collects the parameters common to two other static functions within the CDCUtils class, and passes them to the classes as needed. The effect is made by first painting the relevant section of a previously made snapshot of the desktop, and then "overlaying" a bitmap using the SRCAND flag:

    PaintDesktop() is called first, and

    PaintBitmap() is called last, with the parameters needed to create the requested effect.


    static void GetDesktopImage( CBitmap* pBitmap = NULL );

    CBitmap* pBitmap

    Pointer to a CBitmap that will contain the desktop image. If not specified, the function assumes you're taking a snapshot for later use and will store the image to it's member variable, m_bmpDesktop.

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Jason Troitsky
    United Kingdom United Kingdom
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    QuestionMore recent version? Pin
    Like2Byte26-Jun-08 13:49
    memberLike2Byte26-Jun-08 13:49 
    AnswerRe: More recent version? Pin
    Like2Byte8-Jul-08 12:40
    memberLike2Byte8-Jul-08 12:40 
    QuestionCan I capture the image of background window? Pin
    cottonAwind10-Jul-07 22:59
    membercottonAwind10-Jul-07 22:59 
    Generalbug in rotation... Pin
    Tim Stubbs1-Dec-05 1:23
    memberTim Stubbs1-Dec-05 1:23 
    GeneralCapturing Image of Minimized Window Pin
    IslamianFalcon30-Nov-05 2:32
    memberIslamianFalcon30-Nov-05 2:32 
    GeneralGood Work Pin
    jibesh24-Feb-05 1:52
    memberjibesh24-Feb-05 1:52 
    GeneralDrawText Function Pin
    CharlieC25-May-04 12:03
    memberCharlieC25-May-04 12:03 
    QuestionHow can I change a widow default cdc Pin
    broadoceans21-Dec-03 17:36
    memberbroadoceans21-Dec-03 17:36 
    AnswerRe: How can I change a widow default cdc Pin
    meouvn24-Mar-05 22:19
    membermeouvn24-Mar-05 22:19 
    Generalif( m_article.IsScrolling() ) Pin
    Nishant S30-Jun-03 17:10
    editorNishant S30-Jun-03 17:10 
    GeneralRe: if( m_article.IsScrolling() ) Pin
    J.G. Hattingh30-Jun-03 23:54
    memberJ.G. Hattingh30-Jun-03 23:54 
    GeneralWell, first you think: lame ass collection of useless %$#*...Then... Pin
    Briball21-Jan-03 12:51
    memberBriball21-Jan-03 12:51 
    Generalat first I thought: lame ass circumlocution...Then... Pin
    J.G. Hattingh22-Jan-03 2:58
    memberJ.G. Hattingh22-Jan-03 2:58 
    GeneralMouse dragging of bitmaps Pin
    Dave EE8-Sep-02 11:31
    memberDave EE8-Sep-02 11:31 
    GeneralRe: Mouse dragging of bitmaps Pin
    J.G. Hattingh9-Sep-02 0:26
    memberJ.G. Hattingh9-Sep-02 0:26 
    GeneralRe: Mouse dragging of bitmaps Pin
    sengchai17-Aug-03 23:02
    membersengchai17-Aug-03 23:02 
    GeneralGreat article Pin
    srinivas vaithianathan8-Jul-02 15:30
    membersrinivas vaithianathan8-Jul-02 15:30 
    GeneralRotation about an arbitary point. Pin
    Pabitra6-Jun-02 2:03
    memberPabitra6-Jun-02 2:03 
    GeneralFlicker when OnSize!!! Pin
    Anonymous12-Mar-02 20:04
    memberAnonymous12-Mar-02 20:04 
    GeneralLinearGradient has visual bands Pin
    Lin Song4-Mar-02 13:18
    memberLin Song4-Mar-02 13:18 
    GeneralNothing but flashing snow! Pin
    Todd Howard6-Dec-01 13:07
    memberTodd Howard6-Dec-01 13:07 
    GeneralGreat... Pin
    simus23-Aug-01 23:48
    membersimus23-Aug-01 23:48 
    GeneralGood job... Pin
    dswigger22-Aug-01 13:21
    memberdswigger22-Aug-01 13:21 
    GeneralWay cool - but one strange feature... Pin
    Tim Deveaux3-Feb-01 14:09
    memberTim Deveaux3-Feb-01 14:09 
    GeneralDesign issue Pin
    Thales31-Jan-01 8:54
    memberThales31-Jan-01 8:54 
    GeneralLove the bitmap-rotation Pin
    AlexMarbus30-Jan-01 1:28
    memberAlexMarbus30-Jan-01 1:28 
    GeneralLove the CWinApp site Pin
    Sven Axelsson25-Jan-01 23:28
    memberSven Axelsson25-Jan-01 23:28 
    GeneralRe: Love the CWinApp site Pin
    Yuri Gershanov26-Jan-01 9:00
    memberYuri Gershanov26-Jan-01 9:00 
    GeneralRe: Love the CWinApp site Pin
    KoB :-)29-Jan-01 2:57
    memberKoB :-)29-Jan-01 2:57 
    GeneralRe: Love the CWinApp site Pin
    Dhirendra5-May-03 4:02
    memberDhirendra5-May-03 4:02 
    GeneralRe: Love the CWinApp site Pin
    Muhammed Nasir25-Jun-07 22:20
    memberMuhammed Nasir25-Jun-07 22:20 
    GeneralGreat Work!!! Pin
    Holger Persch25-Jan-01 21:03
    memberHolger Persch25-Jan-01 21:03 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.170118.1 | Last Updated 25 Jan 2001
    Article Copyright 2001 by Jason Troitsky
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid