Highlighted
Absent Member.
Absent Member.
374 views

Printing a form - define printable areas

Jump to solution

Hello! New to VC programming!

I need guidance on a project.

I created a windows form with labels, textboxes, radios, checkboxes etc.

 

I have put a print button, printDialog, printDocument. I set the method of the print button to appear the print dialog menu. I have stuck in this: How to define what to print from the form? For example, If I want to print whatever I see, is there any method. 

Thank you in advance for your help!!!

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Printing a form - define printable areas

Jump to solution

The following is a simple example which shows how to print the content of the current form to the default printer. This uses Windows Forms and the CopyFromScreen method.

 

      $set ilusing"System.Drawing.Printing"
       class-id testformprint.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       working-storage section.
       01 printDocument1 type PrintDocument value new PrintDocument.
	   01 memoryImage type Bitmap.
       method-id NEW.
       procedure division.

           invoke self::InitializeComponent
		   attach method printDocument1_PrintPage to printDocument1::PrintPage
           goback.

       end method.

       method-id CaptureScreen private.
       procedure division.

           declare myGraphics as type Graphics = self::CreateGraphics
           declare s as type Size = self::Size
           set memoryImage = new Bitmap(s::Width, s::Height, myGraphics)
           declare memoryGraphics as type Graphics = type Graphics::FromImage(memoryImage)
           invoke memoryGraphics::CopyFromScreen(self::Location::X, self::Location::Y, 0, 0, s)
           goback.

       end method.

       method-id printDocument1_PrintPage private.
       procedure division using by value sender as object, e as type PrintPageEventArgs.
       
           invoke e::Graphics::DrawImage(memoryImage, 0, 0)
           goback.

       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.

		   invoke CaptureScreen
           invoke printDocument1::Print

       end method.
       end class.

View solution in original post

3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Printing a form - define printable areas

Jump to solution

The following is a simple example which shows how to print the content of the current form to the default printer. This uses Windows Forms and the CopyFromScreen method.

 

      $set ilusing"System.Drawing.Printing"
       class-id testformprint.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       working-storage section.
       01 printDocument1 type PrintDocument value new PrintDocument.
	   01 memoryImage type Bitmap.
       method-id NEW.
       procedure division.

           invoke self::InitializeComponent
		   attach method printDocument1_PrintPage to printDocument1::PrintPage
           goback.

       end method.

       method-id CaptureScreen private.
       procedure division.

           declare myGraphics as type Graphics = self::CreateGraphics
           declare s as type Size = self::Size
           set memoryImage = new Bitmap(s::Width, s::Height, myGraphics)
           declare memoryGraphics as type Graphics = type Graphics::FromImage(memoryImage)
           invoke memoryGraphics::CopyFromScreen(self::Location::X, self::Location::Y, 0, 0, s)
           goback.

       end method.

       method-id printDocument1_PrintPage private.
       procedure division using by value sender as object, e as type PrintPageEventArgs.
       
           invoke e::Graphics::DrawImage(memoryImage, 0, 0)
           goback.

       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.

		   invoke CaptureScreen
           invoke printDocument1::Print

       end method.
       end class.

View solution in original post

Highlighted
Absent Member.
Absent Member.

RE: Printing a form - define printable areas

Jump to solution
It worked as a screenshot. I think it helps. And in this example how can I define the printable area? For example I don't want to print the menu bar on my windows form, but the elements below.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Printing a form - define printable areas

Jump to solution

Since you are passing the X and Y coordinates of the screen area to the CopyFromScreen method you will need to adjust these to reflect the real starting position within the form, in this case the height so that it begins after the menu. You also need to adjust the height in the size parameter to the memoryImage.

There may be a better way to do this and I hope someone jumps in if they know one but here is an example that will print the area after the toolStrip containing menu items: Just the one method has changed.

 

 

       method-id CaptureScreen private.
       procedure division.

           declare myGraphics as type Graphics = self::CreateGraphics
           declare s as type Size = self::Size
		   set s::Height to s::Height - menuStrip1::PointToScreen(type Point::Empty)::Y + menuStrip1::Height
           set memoryImage = new Bitmap(s::Width, s::Height, myGraphics)
           declare memoryGraphics as type Graphics = type Graphics::FromImage(memoryImage)
           invoke memoryGraphics::CopyFromScreen(self::Location::X ,
               menuStrip1::PointToScreen(type Point::Empty)::Y + menuStrip1::Height, 0, 0, s)
           goback.
0 Likes
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.