Click here to Skip to main content
15,885,881 members
Articles / Desktop Programming / MFC
Article

VB Events in C++

Rate me:
Please Sign up or sign in to vote.
1.78/5 (10 votes)
7 Jun 20032 min read 67.4K   22   7
How to use events from Visual Basic ActiveX controls in C++

This article will take you through step-by-step using events from a VB ActiveX control...

Introduction

Step 1: Setting up the Project

Start up Microsoft Visual C++ and start up a new MFC AppWizard(exe) project and call it vbEvents. Set the type to multiple documents. Keep clicking next until you get to step 6 of 6. In the listbox, click CVbEventsView and change the base class to CFormView, then click finished and then OK.

Step 2: Adding the ActiveX control part 1

In the menu bar, go to Project -> Add to Project -> Components and Controls. In the box click Registered ActiveX Controls and choose any control you want to add. For this example, we will use Microsoft FlexGrid Control, version 6.0. Click OK and only check the first box (we don't need the others for this example) Then click OK. Then close out of the Add components and controls window.

Step 2: Adding the ActiveX control part 2

Go to the resource view, then go to dialogs and open up IDD_VBEVENTS_FORM. Delete off the text box and add in the Microsoft Flexigrid control, resize it to the blue line in all directions. Press Ctrl-w to bring up the classwizard and go to the Member Variables tab. In the dropdown box, select CVbEventsView, click Add Variable and name it m_grid leaving everything else how it is, then click OK. You should be able to compile your project and see the ActiveX control in the window.

Step 3: Assigning an Event

Open the classwizzard, and click on the Message Maps tab. In the classname box, choose CVbEventsView, in object id, choose IDC_MSFLEXGRID1. Then in Messages, choose the event you want (in this case Click), then click OK.

In the CVbEventsView::OnClickMsflexgrid1() function, add

MessageBox("Grid:Clicked");

Then compile your code and watch the event when you click.

Step 4: Cleaning Up

At the top of vbEventsView.h and vbEventsView.h, add

#define CFormView CView

Delete from CVbEventsView.cpp, line 68.

ResizeParentToFit();

Also you need to find this on line 35.

CVbEventsView::CVbEventsView()
    : CView(CVbEventsView::IDD)

and remove

: CView(CVbEventsView::IDD)

Also, right click on CVbEventView and click add virtual function. Then click on Draw(), then add. Right click on CVbEventView and click add Windows message handler. In the list box, click CVbEventView and choose WM_CREATE, click add and edit.

In CVbEventView:OnCreate, add

RECT rect;
rect.top = rect.left = 0;
rect.right=rect.bottom = 50;

m_grid.Create("CMSFlexGrid",
   "Grid", WS_CHILDWINDOW, 
   rect,
   this,IDC_MSFLEXGRID1);
    
m_grid.ShowWindow(SW_SHOW);

Right click on CVbEventView and click add Windows message handler. In the list box, click CVbEventView and choose WM_SIZE, click add and edit. In CVbEventView:OnSize add,

m_grid.SetWindowPos(NULL,0,0,cx,cy,SWP_NOZORDER );

And there you go. This method can be modified for other Activex control or different events.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIt would be nice [modified] Pin
Pandele Florin3-Jul-06 5:41
Pandele Florin3-Jul-06 5:41 
Question"VB Events in C++"??? Pin
Travis Smith27-Jul-05 10:08
Travis Smith27-Jul-05 10:08 
GeneralLindseyE Pin
LindseyE10-Jun-03 5:49
LindseyE10-Jun-03 5:49 
GeneralB-a-a-a-d Pin
Chopper8-Jun-03 22:47
Chopper8-Jun-03 22:47 
GeneralRe: B-a-a-a-d Pin
Mikel8-Jun-03 23:28
Mikel8-Jun-03 23:28 
GeneralRe: B-a-a-a-d Pin
Chopper8-Jun-03 23:36
Chopper8-Jun-03 23:36 
GeneralRe: B-a-a-a-d Pin
Mikel9-Jun-03 23:33
Mikel9-Jun-03 23:33 

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.