BrainDump
  Home arrow BrainDump arrow Using Controls in the Microsoft Foundation...
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

Using Controls in the Microsoft Foundation Class Library
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-03-19

    Table of Contents:
  • Using Controls in the Microsoft Foundation Class Library
  • Common Controls
  • Image Handling
  • Image Handling continued

  • 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


    Using Controls in the Microsoft Foundation Class Library


    (Page 1 of 4 )

    Welcome to the fourth episode of our journey into the world of the MFC library. This part will be dedicated to the introduction of standard controls, common controls, input message handling from the keyboard, and, of course, some image handling as well.

    We'll create an application that will include all of these in it. I will start with the standard and common controls in the first section. The second will cover message handling from the keyboard, and in the last section you will learn how to work with images. For the image part, we will cover the basic format of MFC, the bitmap images (.bmp extension).

    With the agenda in front of you, the journey can begin. I hope you will like reading it at least as much as I liked writing it. At the end off this article, your appetite for programming in MFC will be sky high.

    Standard Controls

    Windows without controls is like a hunter without a weapon. It simply isn't useful. Windows' interactivity is based on it. If you see static text, introduce some data into a box, push down a button, choose an option in a radio button or from a combo box, you are using the standard controls. Some may be more complex than others, but they all rely on the same concepts. We can say without a doubt that if you want to program software in Windows, you'll definitely need them. It's crucial that you understand them, so listen up carefully.

    We'll try a simpler one for educational purposes - the Edit control. The edit control is represented in MFC by the CEdit class. They are used for getting text (answers) from the user. But a box of this is handier than that. It works just like a mini Notepad, as it can have more lines, it can be fully edited by the user, or it can use the Clipboard for text copying. If you are still wondering about this box, then start up the search companion, and the box where you are asked to enter the searched file name is what we are trying to create.

    In the process of creation, we start by declaring a CEdit type variable. Through this we'll modify and control the edit box. It is recommended that you declare this as a member of a Dialog/View class so you can refer to it any time. It begins like this:

    CEdit _edit;

    At the start of the program, we create the control. Just before the window of an application is created, a WM_CREATE message will be sent by Windows, so we shall integrate this process into the OnCreate function.

    int CMFC_exampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)

    {

    if (CView::OnCreate(lpCreateStruct) == -1)

    return -1;


    // TODO: Add your specialized creation code here

     

    _edit.Create( ES_AUTOHSCROLL | WS_VISIBLE | WS_BORDER,

    CRect(50,100,200,120),this,IDC_EDITBOX);


    return 0;

    }


    The first parameter list is made from the style specifications of the edit control. ES_AUTOHSCROLL is for the automatic scroll when the edit control is filled. The WS_VISIBLE and WS_BORDER will let us actually see our created control. There are many WindowStyles or EditStyles, but for more information about them, follow up in the MSDN.

    The second parameter is a CRect (basically for int type in a struct) type. This is where we'll designate the size and the position of the window. The fifth parameter assigns the control to the current view, while the final argument specifies the edit control's ID (we previously defined an IDC_EDITBOX as follows: #define IDC_EDITBOX 150).

    As soon as you start up the program, the result pops up in front of your eyes. Now that we created the application, we should implement a purpose for it.

    The dispatched message on a content modification is EN_CHANGE. So now we must manually implement this message into the message map. Additional messages to the box can be implemented in the same way. This consists of the following steps:

    First declare the message in the message map. The first parameter is for the edit controls ID, and the second is for the name of the function that will handle the message:


    ON_EN_CHANGE(IDC_EDITBOX,OnEditChange)


    We shall declare the function in the header file as follows:


    afx_msg void OnEditChange();


    Just one more step to go. Write the function, add in the implementation file the following code:



    void CMFC_exampleView::OnEditChange()


    {

    CString text;

    _edit.GetWindowTextW( text );

     

    _edit2.SetWindowTextW( text );

    }


    I added a little utility for our application by adding a second CEdit variable and assigning a secondary edit control to it. I made it read only and, as the upper code shows, when we modify the text in the first box, that text will be automatically displayed in the second box. I won't go into more detail on this subject, just open the attached project and by looking into the CMFC_exampleView class, you should comprehend all of it. As a note, I have deleted the code that we included in the previous part, so it won't confuse you.

    More BrainDump Articles
    More By Gabor Bernat


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

    BRAINDUMP ARTICLES

    - Microsoft, NSF Open Cloud Computing to Scien...
    - Windows 7 Grabs One-Tenth of Market
    - Windows Mobile 7
    - Commands in WPF
    - Routing Events in WPF
    - Property Value Inheritance and More WPF Conc...
    - Important New Concepts in WPF
    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek