Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VB Events in C++

0.00/5 (No votes)
7 Jun 2003 1  
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