Click here to Skip to main content
15,609,332 members
Articles / Desktop Programming / MFC
Posted 24 Jan 2001


113 bookmarked

Device Context Utilities

Rate me:
Please Sign up or sign in to vote.
4.59/5 (23 votes)
24 Jan 2001
A class that encapsulates some useful, GUI related, static functions

Image 1


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 GetDesktopImage( ... );
    static void PaintRect( ... );
    static void DrawText( ... );
    static void PaintHatch( ... );
    static void Draw3DRect( ... );
    static void LinearGradient( ... );
    static void RadialGradient( ... );
    static void DrawElectricity( ... );
    static void MakeGlass( ... );
    static void PaintBitmap( ... );
    static void PaintDesktop( ... );
    static void DrawLine( ... );
    static void DrawSineWave( ... );
    static void TileBitmap( ... );
    static void BlitBitmap( ... );

    // Other
    virtual ~CDCUtils();


Image 2

static void CDCUtils::PaintHatch(CDC* pDC, CRect area, 


The colour of the grid (or hatch).


The background colour.


Image 3

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.


Image 4

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 its propagated functionality.

int nFontHeight


COLORREF crFontColour

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

long FontWeight

Specify either FW_BOLD, FW_NORMAL, etc.


Image 5

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 useful when 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.


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


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


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


Image 6

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


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).


Image 7

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 its 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 ;)


Image 8

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


RGB value used for the "starting" colour.


RGB value used for the "end" colour.


Image 9

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.


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


Image 10

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

CWND* pWnd

The calling window would supply the CWnd pointer in which 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* you want to paint your desktop image.


Image 11

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.


RGB value used to determine what should be made transparent.

double dDegreesRotation

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


Image 12

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 which the draw will take place (typically a 'this' in the OnDraw() function).


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.


Image 13

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 its member variable, m_bmpDesktop.


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.

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionMore recent version? Pin
Like2Byte26-Jun-08 12:49
Like2Byte26-Jun-08 12:49 
AnswerRe: More recent version? Pin
Like2Byte8-Jul-08 11:40
Like2Byte8-Jul-08 11:40 
GeneralRe: More recent version? Pin
Southmountain12-Sep-20 11:26
Southmountain12-Sep-20 11:26 
QuestionCan I capture the image of background window? Pin
puimv10-Jul-07 21:59
puimv10-Jul-07 21:59 
Generalbug in rotation... Pin
Tim Stubbs1-Dec-05 0:23
Tim Stubbs1-Dec-05 0:23 
GeneralCapturing Image of Minimized Window Pin
IslamianFalcon30-Nov-05 1:32
IslamianFalcon30-Nov-05 1:32 
GeneralGood Work Pin
Jibesh24-Feb-05 0:52
professionalJibesh24-Feb-05 0:52 
GeneralDrawText Function Pin
Charlie Curtis25-May-04 11:03
Charlie Curtis25-May-04 11:03 
QuestionHow can I change a widow default cdc Pin
broadoceans21-Dec-03 16:36
broadoceans21-Dec-03 16:36 
AnswerRe: How can I change a widow default cdc Pin
James Duy Trinh (VietDoor)24-Mar-05 21:19
James Duy Trinh (VietDoor)24-Mar-05 21:19 
Generalif( m_article.IsScrolling() ) Pin
Nish Nishant30-Jun-03 16:10
sitebuilderNish Nishant30-Jun-03 16:10 
GeneralRe: if( m_article.IsScrolling() ) Pin
Jason Troitsky (was Hattingh)30-Jun-03 22:54
Jason Troitsky (was Hattingh)30-Jun-03 22:54 
GeneralWell, first you think: lame ass collection of useless %$#*...Then... Pin
Briball21-Jan-03 11:51
Briball21-Jan-03 11:51 
Generalat first I thought: lame ass circumlocution...Then... Pin
Jason Troitsky (was Hattingh)22-Jan-03 1:58
Jason Troitsky (was Hattingh)22-Jan-03 1:58 
GeneralMouse dragging of bitmaps Pin
Dave EE8-Sep-02 10:31
Dave EE8-Sep-02 10:31 
GeneralRe: Mouse dragging of bitmaps Pin
Jason Troitsky (was Hattingh)8-Sep-02 23:26
Jason Troitsky (was Hattingh)8-Sep-02 23:26 
GeneralRe: Mouse dragging of bitmaps Pin
sengchai17-Aug-03 22:02
sengchai17-Aug-03 22:02 
GeneralGreat article Pin
srinivas vaithianathan8-Jul-02 14:30
srinivas vaithianathan8-Jul-02 14:30 
GeneralRotation about an arbitary point. Pin
6-Jun-02 1:03
suss6-Jun-02 1:03 
GeneralFlicker when OnSize!!! Pin
12-Mar-02 19:04
suss12-Mar-02 19:04 
GeneralLinearGradient has visual bands Pin
4-Mar-02 12:18
suss4-Mar-02 12:18 
GeneralNothing but flashing snow! Pin
6-Dec-01 12:07
suss6-Dec-01 12:07 
GeneralGreat... Pin
23-Aug-01 22:48
suss23-Aug-01 22:48 
GeneralGood job... Pin
dswigger22-Aug-01 12:21
dswigger22-Aug-01 12:21 
GeneralWay cool - but one strange feature... Pin
Tim Deveaux3-Feb-01 13:09
Tim Deveaux3-Feb-01 13:09 

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.