Objective Grid : PART II Programmer’s Guide : Chapter 15 Advanced Concepts : Print, Print Preview
Print, Print Preview
The shared library Print.dll encapsulates the print and print preview functionality and can be linked to any type of application in which CGXGridWnd-based classes are used. Source code for this regular DLL is located in Samples\Grid\General\PrintPreview\Print.
Implementation
MFC provides print support for CView-derived classes only. To support CWnd-derived classes, use the Print.dll, which encapsulates print and print preview functionality. This DLL is an encapsulation of an SDI application containing a CGXGridView object that takes a CGXGridParam object as input.
Use these functions to access the print and preview capabilities in Print.dll:
 
BOOL Preview(HWND hWndParent, CGXGridParam* Param, UINT WM);
BOOL PrintWnd(HWND hWndParent, CGXGridParam* Param, UINT WM);
Set the value of the following Windows Message WM as follows:
For applications with an MFC dialog, use WM_CLOSE.
For applications that directly use a CGXGridWnd-based class on WinForms, use WM_USER.
For applications that use a Grid-based .NET control, use WM_USER+1.
The MainFrame object with CGXGridView is hidden if PrintWnd() is called.
Setting Print and Print Preview Functionality in a CGXGridWnd-Derived Grid
Include the header file from the DLL:
 
#include "..\\Print\\export.h"
Examples
Objective Grid supplies full MFC print and print preview support for CView-derived grids and additional regular DLL for print and print preview support for CWnd-derived grids. A dialog is supplied for manipulating page header and footer text. The Print DLL uses Print or Print Preview features in a similar manner for a CGXGridWnd-derived grid. However, in .NET applications, you'll need to incorporate additional message handling to close the application from the Print Preview UI. For more details, please refer to the Microsoft “Technical Note 30: Customizing Printing and Print Preview.”
Three samples, GridDlgPrint, GridFormPrint and WinFormControlPrint, demonstrate the various uses of Print.dll. Please review the source code of each of these samples to view the similar, but slightly different, source code implementations of Print.dll functionality.
The following example code demonstrates print preview functionality in a dialog, WinForm and .NET control on a WinForm.
Dialog
 
void CTestDlg::OnPrintPreview()
{
this->ShowWindow(SW_HIDE);
Preview( this->m_hWnd, m_wndGrid.GetParam(), WM_CLOSE);
}
 
void CTestDlg::OnPrint()
{
PrintWnd( this->m_hWnd, m_wndGrid.GetParam(), WM_CLOSE);
}
WinForm
System::Void Preview_Click(System::Object^ sender,
System::EventArgs^ e)
{
this->Hide();
Preview((HWND)Handle.ToInt32(), m_pGX->GetParam(), WM_USER);
}
System::Void Print_Click(System::Object^ sender,
System::EventArgs^ e)
{
PrintWnd((HWND)Handle.ToInt32(), m_pGX->GetParam(), WM_USER);
}
 
virtual void WndProc( Message %m ) override
{
if (m.Msg == WM_USER)
Application::Exit();
__super::WndProc (m );
}
.NET control
 
System::Void PrintPreview(IntPtr handle)
{
Preview((HWND)handle.ToInt32(),
m_pGX->GetParam(),
WM_USER + 1);
}
System::Void Print(IntPtr handle)
{
PrintWnd((HWND)handle.ToInt32(),
m_pGX->GetParam(),
WM_USER + 1);
}
 
// This is called on a WinForm containing
// .NET control with CGXGridWnd
protected override void WndProc (ref Message m )
{
if (m.Msg == WM_USER + 1)
Application.Exit();
base.WndProc (ref m );
}
Related Samples
Use of Print.dll is demonstrated in the sample <Install_dir>\Samples\Grid\Print Preview.
The solution file, Print.sln, includes the following projects:
Print – DLL with Print and Print Preview functionality
GridDlgPrint – Modified application from shipped sample <Install_dir>\Samples\Grid\Tutorial\DlgGrid.
WinFormPrint – Modified application from shipped sample <Install_dir>\Samples\Grid\Integration with .NET\GridForm
GridControl and WinFormControlPrint – Modified .NET control and application from the shipped sample <Install_dir>\Samples\Grid\Integration with .NET\GridControl
To test the sample, launch Visual Studio by clicking on the Print.sln build solution, then set one of application projects as the startup project and run the sample.
NOTE >> Note: By default, Print.dll is configured for dynamic linking only. The preprocessor definition, _PRINT, must be added to your application to properly link and use Print.dll functionality.