search c#.net

Chat With Me

คลิ๊กๆๆ...

Monday, March 16, 2009

Printing Text in Windows Forms (c#.net)

It is very common for Windows applications to print text. The Graphics class provides methods for drawing objects (graphics or text) to a device, such as a screen or printer.

To print text

  1. Add a PrintDocument component to your form.
  2. Right-click your form and choose View Code.
  3. In the PrintPage event handler, use the Graphics property of the PrintPageEventArgs class to instruct the printer on what kind of text to print.

    In the example below, the event handler is used to print the string "SampleText" in black in the Arial font beginning at the point 150, 125.

    // C#.net
    private void printDocument1_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.Graphics.DrawString("SampleText",
    new Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125);
    }

    Visual C#.net Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#.net
    this.printDocument1.PrintPage += new
    System.Drawing.Printing.PrintPageEventHandler
    (this.printDocument1_PrintPage);

0 comments:

Post a Comment

Bejeweled

SNAKE Game - Top 20 Challenge

Learn c#.net

THE PRIMITIVE GAME - คนป่ามหาโหด