ComponentOne VSView 8.0
Using VSPrinter in MFC projects

To use the VSPrintercontrol in MFC projects, you will normally follow these steps:

  1. Create a new dialog-based MFC project, making sure to leave checked the ActiveX Controls option in the Wizard.

  2. Go to the resource editor, open the dialog, right-click on it, select Insert ActiveX control, and pick the VSPrintercontrol from the list (if the control is not on the list, it hasn't been registered on your computer).

  3. Hold down the CTRL key and double-click on the control. This will cause Developer Studio to generate wrapper classes through which you can interact with the control. When the wrapper classes are ready, select a name for the control (for example, m_Printer).

  4. From now on, things are pretty much the same as in VB. You can right-click on the control to implement event handlers, and access the controls properties and methods through the m_Printer variable. Most properties are exposed through GetPropertyName and SetPropertyName member functions. Unfortunately, enumerated values get translated into longs instead of their proper enumeration symbols, but that is a relatively minor inconvenience. (And one that can be avoided, read on).

If you look at the CVSPrinter wrapper generated by Developer Studio, you will see that the class is derived from CWnd. This means you can manipulate the control as if it were a regular window. Move, size or hide the control using the CWnd methods. The wrapper class also has a handy Createfunction that lets you create new instances of the control. For example, if you add this declaration to the main dialog's header file:

Example Title
Copy Code
class CMyDlg : public Cdialog

{

  // Construction

  public:

    CMyDlg(CWnd* pParent = NULL);   // standard constructor

    CVSPrinter m_PrinterDynamic;    // VSPrinter control

    ...

You can create the control by adding the following code to the dialog's OnInitDialog function:

Example Title
Copy Code
BOOL CMyDlg::OnInitDialog()

{

  // calculate size of the new VSPrinter control

  RECT rc;

  GetClientRect(&rc);

  InflateRect(&rc, -5, -5);

 

  // create the VSPrinter control and

  // attach it to the dialog

  m_PrinterDynamic.Create(NULL, WS_VISIBLE, rc, this, 100);

 

  return TRUE;

}

The problem with this second approach is that you have to hook up the event handlers manually. You can do this by copying the code created by the Wizard for the first control (it involves using several macros to define an "event sink"). Hooking up the events manually is not difficult, but it is a tedious and error-prone process. Unless you have a good reason to create the controls dynamically, you should stick to the resource editor and the Wizard.

This covers most of what you need to know about using ActiveX controls in MFC. However, the following issues deserve additional explation: handling optional parameters, Picture properties and dual interfaces.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback