Printing a form - define printable areas

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!!!

  • Verified Answer

    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.
    
  • 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.
  • 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.