• Home >> Visual C / C++ >>  Button Controls
  • using button controls in an application


  • Rating : Excellent[0]   Very Good[0]   Average[0]   Below Average[0]   Poor[0]
  • Pub Date: 10/30/2008
  • admin [ View Profile ]

  • Abstract
  • ad>Download source files - 30 KbDownload demo application - 4 KbThis tutorial could well be the simplest windows program you couldever write.  All you need to understandthis tutorial is the basics of windows messaging.  This tutorial is about theCButton class, and how to get a simple button working.This tutorial simply takes input for two numbers and depending onwhat button you click, does the math.  ie. if you enter 1......

  • Introduction
  •  Sponsored Links
  • download demo application - 4 kb
  • introduction

    this tutorial could well be the simplest windows program you could ever write.  all you need to understand this tutorial is the basics of windows messaging.  this tutorial is about the cbutton class, and how to get a simple button working.

    this tutorial simply takes input for two numbers and depending on what button you click, does the math.  ie. if you enter 1 and 2, then click on 'plus' the answer will be 3.  pretty basic.

    to create this program first we need to get the framework laid out.  now 99.9% of the time, you will be using buttons via a dialog box so that is where we will start.  open up the appwizard and create a new project titled buttondemo.  just create a basic dialog box without any document / view architecture.

    then click on the resourcevew tab in the 'workspace' window.  proceed to edit the dialog box idd_buttondemo_dlg.  it will already contain the buttons 'ok' and 'cancel'.  delete the 'todo :' message and the 'cancel' button so we can get to work.

    just drag n' drop the buttons needed, in this example 'plus' and 'minus'.  then select and right click to edit their properties.  the mfc keeps track of these buttons by their unique id, a macro located in the "resource.h" file.  for code clarity, change the name of the id to id_button_add and modify the caption of the button.

    next, let's add the edit boxes to house the values to be added or subtracted, do this in a very similar manner to the way you added the buttons.  just drag and drop them in and then modify their id.  we will need an edit box for the left and right side of the equation as well as one to house the answer.  just for clarity to the end user, check the 'disable' property of the last edit box.  since we won't directly be able to specify the answer.  we will also add a static control for the equals sign.  just drop one in and then change the caption.

    now that our buttons and are in place we need to wire them into our application.  we do this via the class wizard.  (control + w)  click on the member variables tab and proceed to add the variables m_nx, m_ny, and m_nanswer.  all integers; these will hold the values inputted from the edit controls

    then click on the message maps tab.  and add a new function for the object id id_button_add and  id_button_subtract message bn_click.  this create a function is called whenever that button is clicked once.  denoted by onbuttonadd and onbuttonsubtract.

    now that we have created our buttons this is all the code we need to write to get this program working.  it is fairly self explanatory: what we did was create two functions to be called whenever the respective buttons were pressed.  the updatedata (bool) functions are used to manage the data in the edit boxes of the dialog.  updatadata (true), 'updates' the member variables linked to the edit boxes to whatever is the current value.  updatadata (false), updates the edit box to whatever our variable is.

    void cbuttondemodlg::onbuttonadd() 
    {
    	updatedata (true);
    	m_nanswer = m_nx + m_ny;
    	updatedata (false);
    }
    
    void cbuttondemodlg::onbuttonsubtract() 
    {
    	updatedata (true);
    	m_nanswer = m_nx - m_ny;
    	updatedata (false);
    }
    

    that is it!  all we need to do to implement a simple button.

  • RATE THIS ARTICLE :
  •  
    • Latest Comments:
      • Add a comment
      • Title:
      • Comment
      •  
    • Other popular Button Controls articles:
      • cool push menu button

        This article shows the use of a Push button with a drop down menu, similar to the one found in the Office 2000 suite. The code is encapsulated in a MFC class. The class itself is derived from a CButton class, by deriving from this class most of the button behavior is supplied. The main core of the c

      • an easy way to create transparent button

        I wanted to draw a button which is transparent to its back ground image and hence had gone through several articles about button styles - transparent buttons. They were all very tedious tasks of making a button transparent - erase button back ground, draw picture etc. I found an easy method. The ide

      • a shiny orb button in gdi+

        I was actually experimenting with different brush options to create a glassy orb, but it did not work .. But, I still liked the effect even though it wasn't really what I wanted .. So, I turned this into a button. Hopefully, someone may find this useful. When the world is going to WPF ... Using the

      • J# Browser Controls

        This short hands on article will show you how to convert Java Applets written for the Sun JDK 1.1.4 into J# Browser Controls. A J# Browser Control is the equivalent to the Java Applets in the .NET world. Before any clients can view any J# Browser Controls, they must first install these redists in th

      • wowbuttons with the gdidrawstream function

        ThemeButton is a CButton derived class. It is a very simple graphical button, and has special bitmaps representing five button states: normal, hot, pressed, disabled, and default. Buttons are drawn as Windows draws it's theme-changeable buttons. Each theme bitmap has a background color and it's tran

      • unicode owner draw button control

        When I was writing an application for an Arabic customer in C++, I noticed that the buttons/labels in my CDialog did not understand my Arabic text. This class allows you to write any Unicode text in the button control. (And you can use it as a direction for making the same for a static label, for in

      • transparent button with region and anti-aliased edges

        This is an example of how to create and use masked and semi-transparent bitmap buttons that can be overlaid on any background, including other bitmaps. The buttons are automatically alpha-blended (anti-aliased) with the background to create nice smooth edges, and provide region clipping to create va

      • transparant image button (bmp, gif, jpg...)

        *I'm not good at English, but if you can understand my story, isn't that enough? :)When I made this application, especially as dialog based, a lot of buttons were used. Buttons are very important things in my situation. So I decided to make an Image button source.As you know, there are many sources

      • cbutton with icon

        The idea was to have a small piece of code which makes it easy to create a button with an icon on it. I saw a lot of great code, but it was not exactly what I want. So I decided to create my own CIconButton-class. This class makes it easy to set an icon on a button.Simply add a button to your dialog

      • sbbutton

        SBButton control is a button control from a set of controls (Sysbytes Controls) I'm developing to use with an application I'm writing. This button control can be customized in almost anyway the user prefers. You can have different styles for different states of this control, e.g.: DefaultStyle, Mous

      About Us |Contact us |Site Map |Csharp |Visual C / C++ |Visual basic |Java |SQL |Linux / Unix |Ajax
      ©2007-2018 CodeCoolest.com. Ptolive.cn Asp.net source code All Rights Reserved.