Using the VSPrinter Control > Visual C++ Topics > Using VSPrinter in ATL projects |
Using the VSPrinter control in ATL is not much different than using it in MFC projects. You can still use Wizards and a rich set of low-level support classes. What you don't get is the higher-level classes, document/view architecture, and other amenities offered by MFC.
To use the VSPrinter control in ATL projects, you will normally follow these steps:
Create a new ATL COM project of type "Executable".
Select the New ATL Object option in the Insert menu, select the Miscellaneous object type, then choose Dialog. Pick any name for the dialog.
Go to the resource editor, open the dialog, right-click on it, select Insert ActiveX control, and pick the VSPrinter control from the list (if the grid is not on the list, it hasn't been registered on your computer). You may also want to set the dialog's ClipChildren property to True to make it repaint more smoothly.
Right-click on the control and select the Events option. Then select the grid control from the list on the right and the list on the left will show all the events available for the control. Select the ones you want to handle by double-clicking them, and click OK when you are done. This will automatically insert an #import statement into the dialog header file.
You may want to edit the #import statement and leave only the no_namespace qualifier, otherwise only a very thin wrapper will be generated.
Now the control is on the form, but you can't talk to it yet. To get a pointer to the control, open the dialog header file and edit the OnInitDialog function so it looks like this:
Example Title |
Copy Code
|
---|---|
IVSPrinterPtr m_spPrinter; // pointer to the control CAxWindow m_wndPrinter; // pointer to the host window LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { m_wndPrinter = GetDlgItem(IDC_VSPRINTER1); // get host window m_wndPrinter.QueryControl(&m_spPrinter); // get control m_wndPrinter.SetFocus(); // activate control AtlAdviseSinkMap(this, true); // hook up events return 1; // Let the system set the focus } |
This code declares a pointer to the control and one to the control's host window. When the dialog initiates, the code makes m_wndPrinter point to the host window and queries it for the contained control, which is stored in the m_spPrinter variable. From now on, you may move and resize the control through its host window (m_wndPrinter) and access the control's properties and methods through the control pointer (m_spPrinter).
The only thing missing is the code that displays the dialog. That needs to be added to the project's main cpp file. Here's the code that you will need:
Example Title |
Copy Code
|
---|---|
#include "MyProject_i.c" #include "MyDlg.h" // add this line extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { CMyDlg dlg; // create the dialog dlg.DoModal(); // show the dialog //MSG msg; // comment these lines out //while (GetMessage(&msg, 0, 0, 0)) // DispatchMessage(&msg); } |
That's about it. You could clean up this project by removing the references to the idl and rgs files, which it doesn't need (it is just an EXE, not a COM server). See the samples on the distribution CD to find out what changes are necessary.