BrainDump
  Home arrow BrainDump arrow The Graphic Device Interface for the MFC
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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
     
     
    ADVERTISEMENT


    The Graphic Device Interface for the MFC


    (Page 1 of 4 )

    Here we are at the third article. After revising the way Windows works through two of the most used Visual Studio tools for MFC programmers, it is time to learn something concrete. Throughout this article, the object discussed will be the GDI (Graphic Device Interface). Here we can expect to learn about the following things: printing text to the client area, some mouse input-related information, and the CPen and CBrush classes.

    The world is our property. We can do anything with a little sacrifice and devotion. I can say that if you've arrived here, then you are already one step closer to achieving your goals and dreams. However, if you haven’t read the first two segments, I strongly recommend suspending this reading session and searching for the articles that describe the basic things required to continue our journey in the world of the Microsoft Foundation Class library They were also published here in the ASP Free, so finding them shouldn’t be a problem.

    As promised, we will get some real MFC code. But before that, we need to go over the concept of a device: how it works, and how we can use it to our benefit. It will only be the basic knowledge. Advanced devices are beyond the purpose of this article.

    General Concepts of the GDI

    Windows is a graphic operating system, so it shouldn’t surprise you that everything is drawn to the screen, even the text. To simplify this process, Windows has implemented a library of functions that allow for different usage of the “graphical pictures” in a multitude of ways. As everything is an image for Windows, you must call for every modification on the screen in one of the GDIs.

    Beyond the functions required for transferring the data to the screen, a GDI interface also holds a set of objects for the drawing process. This is made up of the following components:

    Device context – A set of attributes that define the surface on which the images and the objects are shown.

    Pen – Object for driving a line.

    Brush – An object for filling a contour present on the screen.

    Font – Doha, Character style…

    Bitmap – One object containing an image is coming up.

    Palette – Another object containing a set of colors that will be used for showing an image on the screen.

    All of these help create the final image drawn to your screen. Therefore, we can conclude that a device context is a collection of attributes that will, eventually, determine the way images are shown within a window. A DC (device context) has lots of attributes, but most of them aren’t related to your trouble because when you get the DC, Windows automatically initializes them.

    Of course, you can change them manually. For example, the color of the Pen will always be black. If you want to write text with a purple color, you will have to change this attribute. It can easily be done because, as I said before, all is created for you. Should you want to customize its particularities, then that’s your only task.

    A DC must always have a full set of instruments; you can’t eliminate some instruments from it. But you can switch between them. So when you want to go back to the default GDI object, all you do is change it with the default instrument. Also a DC represents the borders of a specific window. Each time you draw outside of it, Windows automatically makes a fast resize to avoid this “drawing outside the box” scenario. This way you are limited only to drawing in the client area, not outside of it.

    This entire GDI story is integrated in a series of classes for the sole purpose of simplifying its usage. The biggest of them is the CDC class. The MFC library derives a few classes from it:

    CClientDC – Context for assuring access to the client area of a window. This should be used as a response to any of the messages, excluding one: the WM_PAINT message.

    CPaintDC - The reason for the prior exception is this class, as this was designed for that individual message, so it must be used only for it.

    CMetaFileDC - A context that holds a metafile and is under Windows. It is a file that contains a list of commands for the fast reproduction of a picture.

    CWindowDC – The context that gives you access to the entire window, not just the client area.

    We’ve just finished the necessary theoretical knowledge! Should you have trouble understanding the DCs, have no fear, as everything will become clear with the example presented on the next page; we’ll learn to write text on the client area.

    More BrainDump Articles
    More By Gabor Bernat


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

    BRAINDUMP ARTICLES

    - Firefox on Windows Mobile
    - Microsoft`s Sneaky Firefox Add-On Installati...
    - Microsoft`s Answer to Google Search - Bing
    - Doing Statistical Analysis with MS Excel
    - Extracting Google-Indexed Web Site Pages Usi...
    - Advanced String Manipulation Using MS Excel
    - Using Goal Seek and Solver in MS Excel
    - Internet Explorer 8: A Hands-on View
    - Tools for Beginning Game Developers
    - XAMPP: Tips for Running an Apache/MySQL Serv...
    - Improving Your Visual Studio Workspace
    - A Look at Microsoft`s Mobile Operating System
    - Microsoft Internet Explorer 8: Mixed Reactio...
    - Configuring WSUS 3.0 on Windows Server 2008
    - Migrating to Windows Server 2008





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT