Click here to Skip to main content
Click here to Skip to main content
Go to top

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



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

    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 
        {
        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 <A href="http://www.codeproject.com/gdi/#GetDesktopImage">GetDesktopImage</A>( ... );
        static void PaintRect( ... );
        static void <A href="http://www.codeproject.com/gdi/#DrawText">DrawText</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#PaintHatch">PaintHatch</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#Draw3DRect">Draw3DRect</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#LinearGradient">LinearGradient</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#RadialGradient">RadialGradient</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#DrawElectricity">DrawElectricity</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#makeGlass">MakeGlass</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#PaintBitmap">PaintBitmap</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#PaintDesktop">PaintDesktop</A>( ... );
        static void DrawLine( ... );
        static void <A href="http://www.codeproject.com/gdi/#DrawSineWave">DrawSineWave</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#TileBitmap">TileBitmap</A>( ... );
        static void <A href="http://www.codeproject.com/gdi/#BlitBitmap">BlitBitmap</A>( ... );
    
        // Other
        CDCUtils();
        virtual ~CDCUtils();
    
        };
    
        

    PaintHatch()

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

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

    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

    Ditto.

    COLORREF crFontColour

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

    long FontWeight

    Specify either FW_BOLD, FW_NORMAL, etc.


    Draw3DRect()

    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)


    LinearGradient()

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

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


    RadialGradient()

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

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

    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.


    BlitBitmap()

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


    PaintBitmap()

    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.


    MakeGlass()

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

    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

    Share

    About the Author

    Jason Troitsky

    United Kingdom United Kingdom
    No Biography provided

    Comments and Discussions

     
    QuestionMore recent version? PinmemberLike2Byte26-Jun-08 12:49 
    AnswerRe: More recent version? PinmemberLike2Byte8-Jul-08 11:40 
    QuestionCan I capture the image of background window? PinmembercottonAwind10-Jul-07 21:59 
    Generalbug in rotation... PinmemberTim Stubbs1-Dec-05 0:23 
    GeneralCapturing Image of Minimized Window PinmemberIslamianFalcon30-Nov-05 1:32 
    GeneralGood Work Pinmemberjibesh24-Feb-05 0:52 
    GeneralDrawText Function PinmemberCharlieC25-May-04 11:03 
    QuestionHow can I change a widow default cdc Pinmemberbroadoceans21-Dec-03 16:36 
    AnswerRe: How can I change a widow default cdc Pinmembermeouvn24-Mar-05 21:19 
    Generalif( m_article.IsScrolling() ) PineditorNishant S30-Jun-03 16:10 
    GeneralRe: if( m_article.IsScrolling() ) PinmemberJ.G. Hattingh30-Jun-03 22:54 
    GeneralWell, first you think: lame ass collection of useless %$#*...Then... PinmemberBriball21-Jan-03 11:51 
    Generalat first I thought: lame ass circumlocution...Then... PinmemberJ.G. Hattingh22-Jan-03 1:58 
    GeneralMouse dragging of bitmaps PinmemberDave EE8-Sep-02 10:31 
    GeneralRe: Mouse dragging of bitmaps PinmemberJ.G. Hattingh8-Sep-02 23:26 
    GeneralRe: Mouse dragging of bitmaps Pinmembersengchai17-Aug-03 22:02 
    GeneralGreat article Pinmembersrinivas vaithianathan8-Jul-02 14:30 
    GeneralRotation about an arbitary point. PinmemberPabitra6-Jun-02 1:03 
    GeneralFlicker when OnSize!!! PinmemberAnonymous12-Mar-02 19:04 
    GeneralLinearGradient has visual bands PinmemberLin Song4-Mar-02 12:18 
    GeneralNothing but flashing snow! PinmemberTodd Howard6-Dec-01 12:07 
    GeneralGreat... Pinmembersimus23-Aug-01 22:48 
    GeneralGood job... Pinmemberdswigger22-Aug-01 12:21 
    GeneralWay cool - but one strange feature... PinmemberTim Deveaux3-Feb-01 13:09 
    GeneralDesign issue PinmemberThales31-Jan-01 7:54 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
    Web04 | 2.8.140905.1 | Last Updated 25 Jan 2001
    Article Copyright 2001 by Jason Troitsky
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid