我在一个winfrom(c#)窗体中画了一个表,要将这个表直接打印出来怎么作呢?
解决方案 »
- C#如何让鼠标右键出现复制把选择的内容复制?
- 高手在那里?关于局域网在线统计的问题
- 怎么使得窗口一直active
- 单击Datagrid某一行时,使其中某一列的值符给DropDownList
- 刚接触类,遇到一个比较肉的问题,请教大家
- 关于带有checkbox的ListView的显示问题,急!在线等...谢谢
- 定义一个方法怎么接收任意多个参数..?
- 如何得到WebBrowser控件里网页的源代码??
- 控制台应用程序的Program类问题
- 如何将InerBase的数据库转化为SQL Server 2000的数据库
- 关于同步和异步调用的问题。
- ************有关DataSet更新数据库的问题************
http://www.codeproject.com/dotnet/PrintingFormReport.asp
你也可以在codeproject搜索一下其他的例子,还有很多类似的
到http://www.webmis.com.cn/里找相关的打印资料,里面有直接对listview等控件的打印处理。
如果连接打不开,稍后尝试。
{
printPreviewDialog1.ShowDialog();
} void DrawForm(Graphics g, int resX, int resY)
{
g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
float scale = resX/ScreenResolution; foreach (Control c in Controls)
{
string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);
switch (strType)
{
case "Button":
Button b = (Button)c;
ControlPaint.DrawButton(g, ((Button)c).Left, ((Button)c).Top, ((Button)c).Width, ((Button)c).Height, ButtonState.Normal);
g.DrawString(b.Text, b.Font, new SolidBrush(b.ForeColor), b.Left + b.Width/2 - g.MeasureString(b.Text, b.Font).Width/2, b.Top + b.Height/2 - g.MeasureString("a", b.Font).Height/2, new StringFormat());
break;
case "TextBox":
// ControlPaint.FillReversibleRectangle( ((TextBox)c).Bounds, c.BackColor);
TextBox t = (TextBox)c; ControlPaint.DrawButton(g, t.Left, t.Top, t.Width, t.Height, ButtonState.Pushed );
g.FillRectangle(new SolidBrush(t.BackColor), t.Left+1, t.Top + 1, t.Width+2, t.Height -2);
g.DrawString(t.Text, t.Font, new SolidBrush(t.ForeColor), t.Left + 2, t.Top + t.Height/2 - g.MeasureString("a", t.Font).Height/2, new StringFormat());
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale) - 5, Border3DStyle.SunkenInner , Border3DSide.Bottom);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenInner , Border3DSide.Left);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale) - 5, (int)(t.Height*scale), Border3DStyle.SunkenInner , Border3DSide.Right);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), (int)(t.Width*scale), 5, Border3DStyle.SunkenInner , Border3DSide.Top);
//
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale)+1 - 5, Border3DStyle.SunkenOuter, Border3DSide.Bottom);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale)-1, (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter , Border3DSide.Left);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale)+1 - 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter , Border3DSide.Right);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale)-1, (int)(t.Width*scale), 5, Border3DStyle.SunkenOuter , Border3DSide.Top);// g.FillRectangle(new SolidBrush(t.BackColor) , t.Left+1, t.Top + 1, t.Width - 2, t.Height - 2);
break;
case "CheckBox":
// ControlPaint.FillReversibleRectangle( ((TextBox)c).Bounds, c.BackColor);
CheckBox cb = (CheckBox)c; if (cb.Checked)
ControlPaint.DrawCheckBox(g, cb.Left, cb.Top, cb.Height/2, cb.Height/2, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(g, cb.Left, cb.Top, cb.Height/2, cb.Height/2, ButtonState.Normal); g.DrawString(cb.Text, cb.Font, new SolidBrush(cb.ForeColor), cb.Right -cb.Height - g.MeasureString(cb.Text, cb.Font).Width , cb.Top, new StringFormat());
// float scale = resX/ScreenResolution;
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale) - 5, Border3DStyle.SunkenInner , Border3DSide.Bottom);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenInner , Border3DSide.Left);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale) - 5, (int)(t.Height*scale), Border3DStyle.SunkenInner , Border3DSide.Right);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), (int)(t.Width*scale), 5, Border3DStyle.SunkenInner , Border3DSide.Top);
//
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale)+1 - 5, Border3DStyle.SunkenOuter, Border3DSide.Bottom);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale)-1, (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter , Border3DSide.Left);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale)+1 - 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter , Border3DSide.Right);
// ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale)-1, (int)(t.Width*scale), 5, Border3DStyle.SunkenOuter , Border3DSide.Top); // g.FillRectangle(new SolidBrush(t.BackColor) , t.Left+1, t.Top + 1, t.Width - 2, t.Height - 2);
break;
} } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrinterResolution pr = e.PageSettings.PrinterResolution;
DrawForm(e.Graphics, pr.X, pr.Y);
} float ScreenResolution = 96.0f;
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
ScreenResolution = e.Graphics.DpiX;
} private void PrintMenu_Click(object sender, System.EventArgs e)
{
printDocument1.Print();
}
{
System.Windows.Forms.PrintPreviewDialog ppdialog=new PrintPreviewDialog();
((System.Windows.Forms.Form) ppdialog).WindowState=
System.Windows.Forms.FormWindowState.Maximized;
ppdialog.Document=this.printDocument1;
ppdialog.ShowDialog(this);
ppdialog.Close();
}private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PaintEventArgs myPaintArgs =
new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
this.InvokePaint(this.dataGrid1, myPaintArgs);
}
{
CaptureScreen();
printDocument1.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 CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.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, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}拖到窗体里一个Button1,在拖出一个printDocument控件,运行