BrainDump
  Home arrow BrainDump arrow Page 2 - The Graphic Device Interface for the MFC
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

The Graphic Device Interface for the MFC
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-12

    Table of Contents:
  • The Graphic Device Interface for the MFC
  • Practice and Text
  • Practice and Text continued
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    The Graphic Device Interface for the MFC - Practice and Text
    (Page 2 of 4 )

    In this section, as I promised, we're going to learn how to write a particular text on the client area. Before we begin, let's clarify something. Each time you use a Device context, you should create it. This can be done by declaring a new device context that is linked to the client area you wish to use. Some of the functions that are addressed towards a message map already carry one of these control devices. As a result, in these cases you don't have to create it yourself.

    These are the functions that you will obviously and explicitly use like the WM_DRAW message present in any view class under the OnDraw name. The WM_DRAW message is dispatched towards the program at any time the window’s content needs to be updated — redrawn. If you passed through the first two segments of this article series, then you should have created the MFC_example named application. In its view class (CMFC_exampleView), the OnDraw function looks like this:


    void CMFC_exampleView::OnDraw(CDC* /*pDC*/)

    {

    CMFC_exampleDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    if (!pDoc)

    return;


    // TODO: add draw code for native data here

    }


    After this, the device creation is commented as the /*pDC*/ part, but after we remove this comment, we can use it without borders. Now we can call one of the many functions that a DC context has. For example, drawing a rectangle can be accomplished in the following line:

    pDC->Rectangle( 100, 120, 400, 500);

    To exemplify, we’ll create a rectangle from the upper right coordinates (100,120) and the lower right coordinates (400,500). The functions present in a DC are massive, so search for them in the MSDN help files or in an MFC bible. But if you want to use a DC in a function where it doesn’t comes so swiftly, you can declare it with the following line:

    CClientDC clientDC(this);

    We create a CClientDC device context that is called clientDC (you could also call it my_cat, but for standard coding and for easy understanding, this name suits it the most). This DC is assigned to the current view area. This is realized by calling it for the this pointer. The device can be initialized in other ways too, but this is the most useful because we don’t need to worry about its destruction. As a rule of C programming, all local variables are destroyed at the end of a function.

    Enough said! Let’s take a look at the attached code below. It contains many new things; each of them will be covered right after this code snippet.


    void CMFC_exampleView::OnLButtonDown(UINT nFlags, CPoint point)

    {

    // TODO: Add your message handler code here and/or call a // default

    CClientDC clientDC(this); // we create a DC that will be used

     

    COLORREF color = clientDC.GetTextColor();

    if(color == RGB (0, 0, 0)) // if the background color is black

    clientDC.SetTextColor( RGB(255,0,0)); //change it


    //we set the Background of the text to transparent

    //so windows will draw out only the text, ignoring the BK

    clientDC.SetBkMode( TRANSPARENT );

    //add some extra character depending upon X coordinates

    clientDC.SetTextCharacterExtra(point.x/10);

     


    //create a pen

    int width =3;

    CPen newPen (PS_SOLID, width, color);

    CPen* oldPen = clientDC.SelectObject( &newPen);

    //create a brush

    CBrush newBrush( HS_CROSS, RGB(0,0,255) );

    //select for usage the newly created Brush, replacing the old

    CBrush* oldBrush = clientDC.SelectObject( &newBrush);


    //Click = Dong at that coordinate

    clientDC.TextOutW( point.x, point.y,CString("Dong"));

    //and an ellipse just below our click

    clientDC.Ellipse( point.x + 30, point.y+ 50, point.x +100,point.y + 150);


    //change back tom the old Brush

    clientDC.SelectObject(oldBrush);

    clientDC.SelectObject(oldPen);


    CView::OnLButtonDown(nFlags, point);

    }


    The upper code writes out Dong where we left click and an ellipse right below it. If you don’t comprehend the code, then the next paragraphs were written for you. They hold some valuable extra information.

    More BrainDump Articles
    More By Gabor Bernat


       · Welcome my friend and thanks for reading my article. As previously mentioned I...
     

    BRAINDUMP ARTICLES

    - Multiple Service Contracts and Indigo
    - Cleaning Out Your Programs in XP
    - Handling Metadata with Indigo
    - Building Blocks for a WCF Service Web Site
    - Help! I Need Some Remote Assistance
    - Using Service Templates with Indigo
    - Windows XP Tips for Task Manager
    - Generating Clients and Services with Indigo
    - Vista SP1, A Review
    - Services and the WCF
    - VBScript: Final Date Functions
    - Creating Services with the WCF
    - The Resource View of the MFC
    - VBScript: More Fun with the Date Functions
    - Vista Price Cuts Dissected




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway