Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Adding Print Preview/Screen Print Functionality to a Windows Form

Adding Print Preview/Screen Print Functionality to a Windows Form

The attached Visual COBOL project contains a .NET managed code Windows Forms application that demonstrates how to allow a user to click on a button and automatically display a Print Preview Dialog or to send the image of the current form to the default printer for printing.

$set ilusing"System.Text"
$set ilusing"System.Drawing.Printing"
$set ilusing"System.Windows.Forms"

class-id screenprintvc.Form1 is
   partial inherits type System.Windows.Forms.Form.

working-storage section.
01 PrintDoc1 type PrintDocument value new PrintDocument.
01 PPDlg1 type PrintPreviewDialog value new PrintPreviewDialog.
method-id NEW.
procedure division.
   invoke self::InitializeComponent
   set PPDlg1::Document to PrintDoc1
   set PrintDoc1::OriginAtMargins to true
   invoke PrintDoc1::add_PrintPage(new System.Drawing.Printing.PrintPageEventHandler (self::pDoc_PrintPage))
   goback.
end method.

method-id pDoc_PrintPage private.
local-storage section.
01 bmp type Bitmap.
procedure division using by value sender as object, e as type PrintPageEventArgs.
   set bmp to new type Bitmap(self::Width, self::Height)

  *>Takes the Snap of the Exact WindowForm size as Bitmap image
   invoke self::DrawToBitmap(bmp, new type Rectangle(0, 0, bmp::Width, bmp::Height))
   invoke e::Graphics::DrawImage(bmp, 0, 0)
end method.

method-id btn_PrintPreview_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
   invoke PPDlg1::ShowDialog
end method.

method-id btn_PrintPage_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
   invoke PrintDoc1::Print
end method.
end class.

 

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Version history
Revision #:
1 of 1
Last update:
‎2012-06-21 16:58
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.