Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / MFC

Device Context Utilities

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

Image 1

Introduction

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 http://www.cwinapp.com. 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. ;)

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

C++
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

C++
class CDCUtils 
    {
    public:

    // 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
    CDCUtils();
    virtual ~CDCUtils();
    };    

PaintHatch()

Image 2

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

COLORREF crFG

The colour of the grid (or hatch).

COLORREF crBG

The background colour.


TileBitmap()

Image 3

C++
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.


DrawText()

Image 4

C++
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

Ditto.

COLORREF crFontColour

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

long FontWeight

Specify either FW_BOLD, FW_NORMAL, etc.


Draw3DRect()

Image 5

C++
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.

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


LinearGradient()

Image 6

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

COLORREF crFrom

RGB value used for the "starting" colour.

COLORREF crTo

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


DrawElectricity()

Image 7

C++
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 ;)


RadialGradient()

Image 8

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

COLORREF crFrom

RGB value used for the "starting" colour.

COLORREF crTo

RGB value used for the "end" colour.


DrawSineWave()

Image 9

C++
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.


PaintDesktop()

Image 10

C++
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.


BlitBitmap()

Image 11

C++
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 determine what should be made transparent.

double dDegreesRotation

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


PaintBitmap()

Image 12

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

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.


MakeGlass()

Image 13

C++
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

GetDesktopImage()

C++
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.

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.


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 
Thanks for the excellent article with all the useful techniques!

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.