using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PrintForm
{
public class PrintForm
{
Form form;
Bitmap bmp;
int x=0; int y=0;
int width=0; int height=0;
public PrintForm(Form form) {this.form = form;}
public void Print() {Print(0, 0);}
public void Print(int x, int y)
{
this.x = x; this.y = y;
width = form.ClientSize.Width;
height = form.ClientSize.Height;
bmp = GetFormBitmap();
//create new PrintDocument object
//and assign a method for the PrintPage event
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//Graphics object for the current printer
e.Graphics.DrawImage(bmp, x, y, width, height);
}
private Bitmap GetFormBitmap()
{ //creates new Bitmap object containing
//the image of the form
const int SRCCOPY = 0xcc0020;
//create Graphics objects for the form
Graphics gg = form.CreateGraphics();
IntPtr formhdc = gg.GetHdc();
//create new Bitmap object
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
IntPtr bmphdc = g.GetHdc();
//copy the bitmap data of the form to the new Bitmap
BitBlt(bmphdc, 0, 0, width, height,
formhdc, 0, 0, SRCCOPY);
//the hdc of Bitmap object must be released
//before sending this object to print
g.ReleaseHdc(bmphdc);
gg.ReleaseHdc(formhdc);
return bmp;
}
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest,
int nYDest, int nWidth, int nHeight,
IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
}
}
|