Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Device Context Utilities

0.00/5 (No votes)
24 Jan 2001 2  
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 ;)

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


    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