Click here to Skip to main content
15,886,799 members
Articles / Desktop Programming / MFC
Article

ButtonControl

Rate me:
Please Sign up or sign in to vote.
1.58/5 (15 votes)
3 Oct 2006CPOL3 min read 55.1K   509   31   6
A simple button control showing the use of a property page.

Sample Image - ActiveXButtonControl.jpg

Introduction

Creating and Calling The Active X Controls

 <o:p>

Let’s see how one can develop an ActiveX Control for a button.

 <o:p>

Open the .NET editor and go to File Menu.

Select New Project option from the file menu.

This pops up a dialog.

In the project type go and in the Visual C++ Project option select MFC.

In the right hand side option of Templates select MFC Active X Controls.

Give the name as ButtonControl and press OK.

This pops up a dialog of MFC Active X Control Wizard.

Just have a look at the default settings in the left hand panel and go to Control Settings.

 <o:p>

In the control settings dialog select BUTTON from the Create Control based on compbo box and select the check box for Flicker Free.

Accept all other default settings and press Finish.

In the Class View you can now see the three classes that are generated.

 <o:p>

1)      The Application class CButtonControlApp.

2)      The Control Class CButtonControlCtrl.

3)      The Property Page Class CButtonControlPropPage.

 <o:p>

Open the Resource Editor and the IDD_PROPPAGE_BUTTONCONTROL dialog.

Remove the caption that is already placed on the dialog and from the Toolbox add one edit box and a button on the dialog. Change the caption of the button to whatever you want.

Right click on the edit box and add a variable to it.

Again open the resource editor and double click the button that you have already placed on it.

This adds a event handler for the click of the button.

Place the lines of code in this function

 <o:p>

void CButtonControlPropPage::OnBnClickedButton1()<o:p>

{<o:p>

      CString name;<o:p>

      m_edit.GetWindowText(name);<o:p>

      AfxMessageBox ("Hello! "+name);<o:p>

}

 <o:p>

Open the CButtonControlCtrl class and go to OnOcmCommand function.

Change the code of the function as follows:

 <o:p>

LRESULT CButtonControlCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)<o:p>

{<o:p>

#ifdef _WIN32<o:p>

      WORD wNotifyCode = HIWORD(wParam);<o:p>

#else<o:p>

      WORD wNotifyCode = HIWORD(lParam);<o:p>

#endif<o:p>

 <o:p>

      // TODO: Switch on wNotifyCode here.<o:p>

      CDialog *dlg=new CButtonControlPropPage ;<o:p>

      dlg->Create (IDD_PROPPAGE_BUTTONCONTROL);<o:p>

      dlg->ShowWindow (SW_SHOW);<o:p>

      return 0;<o:p>

}<o:p>

Build the project.

If you are in a bit of hurry you can go to tools menu and choose ActiveX Control Test Conainer. Go to Edit menu and from the menu select the Insert New Control Option where you can spot the control we just created by the name of  ButtonControl Control.

When you click ok our button is visible on the screen. You can see the effect on clicking on it.

This is all for creating the Active X control and adding the handler’s on that.

 <o:p>

For more patient programmer’s who want to call this control in their application.

 <o:p>

Now create another MFC Dialog based project and give it the name as ButtonControlCall.

In the resource editor open the dialog and right click anywhere on it.

This opens a pop up.

Select Insert Active X Control from the dialog.

In the dialog box that pops up select the ButtonControl Control option and this adds your button control on the dialog which you can place anywhere in the dialog.

 <o:p>

Now build the project.

Press Ctrl+F5 to run the project.

On the screen you can see a dialog with OK,Cancel and one more button that is our ActiveX Button.

 <o:p>

Try clicking the button.

This shows two controls on the existing dialog. One is the edit box and second is the button that we had placed on that dialog.

Insert your name in the text box and press the button.

A message box is popped up that shows

Hello! XYZ if you entered your string as XYZ in the edit box

 <o:p>

That’s all guys.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
He's been programming with C/C++ as the main stream(VC++(6.0/7.1),WIN32,MFC,COM,ATL). Prior to this he was working on Java and did a lot of work in Java-Swing and JSP. What makes him stand apart is his passion for work.
He helps those who seek for it.
Loves to work in team or as an individual and always has a word of advice or two.
The following is what he says about "What an individual should do to SUCCEED?"
One must analyze oneself. It is one of those virtues that encourages an individual to perform well and succeed.
This is a Organisation (No members)


Comments and Discussions

 
GeneralExcellent! Pin
cristitomi17-Mar-07 3:58
cristitomi17-Mar-07 3:58 
Generalsource code Pin
rixwan30-Jun-06 0:47
rixwan30-Jun-06 0:47 
GeneralRe: source code Pin
_AnsHUMAN_ 30-Jun-06 1:03
_AnsHUMAN_ 30-Jun-06 1:03 
GeneralRe: source code Pin
Rajesh R Subramanian3-Oct-06 20:25
professionalRajesh R Subramanian3-Oct-06 20:25 
GeneralRe: source code Pin
_AnsHUMAN_ 3-Oct-06 20:44
_AnsHUMAN_ 3-Oct-06 20:44 
Generalfail to download Pin
essawy11-May-06 7:17
essawy11-May-06 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.