Newbie to printing

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am trying to learn how to use how to print in a windows form. I am
using the components PrintDialog and the PrintDocument. I am
succeding to show the dialog box, and to pass paper from the printer.

Obviously no data is being printed. How do I pass data to the
PrintDocument object and what type of data does it accept.


Can someone help me out, because I am a newbie in such a thing.
Thanks in Advance
 
public class PrintScreen
{
public PrintScreen()
{
//
// TODO: Add constructor logic here
//
}
public PrintScreen(System.Windows.Forms.Form frm)
{
CapturePrintScreen(frm);
System.Drawing.Printing.PrintDocument prtDoc1 = new
System.Drawing.Printing.PrintDocument();
prtDoc1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler(prtDoc1_PrintPage);
System.Windows.Forms.PageSetupDialog pg = new
System.Windows.Forms.PageSetupDialog();
pg.Document = prtDoc1;
pg.PageSettings.Landscape = true;
prtDoc1.Print();
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hdcSrc
, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;

private void CapturePrintScreen(System.Windows.Forms.Form frm)
{
Graphics mygraphics = frm.CreateGraphics();
Size s = frm.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, frm.ClientRectangle.Width, frm.ClientRectangle.Height,
dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void prtDoc1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
}

call this class from an event:
new PrintScreen(this)
 
Hi,
I tried the code given, and set the new(this) in a click event of a
button. When I runned the application, the button was printed.

In my application, I am displaying datagrids, which are binded with a
datasource. What I need to print is these datagrids.

How can this be done.
Thanks in Advance
 
xarky d_best said:
In my application, I am displaying datagrids, which are binded with a
datasource. What I need to print is these datagrids.

If you won't be stressed by all the .net printing stuff you can give a try
to my report engine :)

You can design your data report or build it at runtime.

See the link in signature
 
A PrintDocument subclass can take any datasource that you want to send
it but you have to write the code to paginate and display that data. I
normally use an XmlDocument to hold my print data but that is because I have
another use for the XML (sending documents between locations).
You could just write some code to utilize the information bound to the
grid and print it as desired.

Ron Allen
 
Back
Top