C#中如何打印当前窗体并进行打印预览
网上那个看不到预览效果所以别拿那个给我看了。谁能帮我解决我把剩下的所有分都送出去,谢谢各位了。对了最后有注释。winform下的如何打印窗体...
网上那个看不到预览效果所以别拿那个给我看了。谁能帮我解决我把剩下的所有分都送出去,谢谢各位了。对了最后有注释。
winform 下的如何打印窗体 展开
winform 下的如何打印窗体 展开
展开全部
把下面的代码加在<form id="form1" runat="server">的下面.....加在HTML的<form ...>下面
<OBJECT id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0" VIEWASTEXT></OBJECT>
<div align="center">
<input onclick="document.all.WebBrowser.ExecWB(6,1)" type="button" value="打印">
<input onclick="document.all.WebBrowser.ExecWB(8,1)" type="button" value="页面设置">
<input onclick="document.all.WebBrowser.ExecWB(7,1)" type="button" value="打印预览">
<input onclick="javascript:window.close();" type="button" value="关闭">
示例一:以表格形式打印DataTable内的数据
//定义全局变量count,储存当前打印的行数
int count=0;
//定义一个方法,接收一个DataTable类型参数及PrintDocument的PrintPage事件传入的参数e以方便操作
private voidPrintTable(DataTable dt, System.Drawing.Printing.PrintPageEventArgse)
{
//取得对应的Graphics对象
Graphicsg = e.Graphics;
//获得相关页面X坐标、Y坐标、打印区域宽度、长度
intx = e.PageSettings.Margins.Left;
inty = e.PageSettings.Margins.Top;
intwidth = e.PageSettings.PaperSize.Width -e.PageSettings.Margins.Left- e.PageSettings.Margins.Right;
intheight = e.PageSettings.PaperSize.Height -e.PageSettings.Margins.Top -e.PageSettings.Margins.Bottom;
//定义打印字体
Fontfont = new Font("宋体",15);
//rowCount是除去打印过的行数后剩下的行数
introwCount = dt.Rows.Count-count;
//maxPageRow是当前设置下该页面可以打印的最大行数
intmaxPageRow=height/(int)font.GetHeight();
//因为是表格,先画一条水平直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y), new Point(x +(dt.Columns.Count) * 135, y));
//再画出表格各列的列标题
for(int i = 0; i < dt.Columns.Count; i++)
{
stringhead = dt.Columns[i].ColumnName;
g.DrawString(head,font, Brushes.Black, x + i * 135, y);
}
//画完标题,再画一条直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (int)font.GetHeight()), newPoint(x + (dt.Columns.Count) * 135, y +(int)font.GetHeight()));
//判断,如果剩下的行数小于可打印的最大行数,则执行下列代码
if(maxPageRow >=rowCount)
{
//当前行数小于Table内总行数时,循环
while(count < dt.Rows.Count)
{
//内循环打印Table内行数据
intcolumnCount = 0;
while(columnCount < dt.Columns.Count)
{
stringtemp = dt.Rows[count][columnCount].ToString();
//打印每个单元格内的数据
g.DrawString(temp,font, Brushes.Black, x + columnCount * 135, y + (count %maxPageRow) * (int)font.GetHeight() +(int)font.GetHeight());
//打印完一行后,继续打印一条直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x +(dt.Columns.Count) * 135, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()));
columnCount++;
}
count++;
}
//所有数据打印完毕后,打印垂直直线
for (int i = 0; i <= dt.Columns.Count; i++)
{
g.DrawLine(newPen(Brushes.Black), new Point(x + i * 135, y), new Point(x + i *135, y + rowCount * (int)font.GetHeight() +(int)font.GetHeight()));
}
}
//判断,如果剩下的行数大于可打印的最大行数,则执行下列代码
else
{
do
{
//与上面类似,注意下面while的条件
intcolumnCount = 0;
while(columnCount < dt.Columns.Count)
{
stringtemp = dt.Rows[count][columnCount].ToString();
//打印每个单元格
g.DrawString(temp,font, Brushes.Black, x + columnCount * 135, y + (count %maxPageRow) * (int)font.GetHeight() +(int)font.GetHeight());
//打印水平直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x +(dt.Columns.Count)*135, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()));
columnCount++;
}
count++;
}while ((count % maxPageRow >0));
//打印垂直直线
for(int i = 0; i <= dt.Columns.Count;i++ )
{
g.DrawLine(newPen(Brushes.Black), new Point(x + i * 135, y), new Point(x + i *135, y + height + (int)font.GetHeight()));
}
}
//指定HasMorePages值,如果页面最大行数小于剩下的行数,则返回true(还有),否则返回false
if(maxPageRow<rowCount)
{
e.HasMorePages= true;
}
else
{
e.HasMorePages= false;
count= 0;
}
}
示例二:文本打印
//设置全局变量保存截取字符串位置
int sub=0;
private void printText(stringtext, System.Drawing.Printing.PrintPageEventArgs e)
{
//取得Graphics实例
Graphics g = e.Graphics;
//获得相关点坐标、长度、宽度
int x = e.PageSettings.Margins.Left;
int y = e.PageSettings.Margins.Right;
int width =e.PageSettings.PaperSize.Width-e.PageSettings.Margins.Left-e.PageSettings.Margins.Right;
int height =e.PageSettings.PaperSize.Height-e.PageSettings.Margins.Top-e.PageSettings.Margins.Bottom;
//设置字体
Font font=new Font("宋体",15);
//这个方法后面讲
g.MeasureString(text.Substring(sub), font, new SizeF(width,height-10), new StringFormat(), out charnum, out line);
//打印string
g.DrawString(text.Substring(sub), font, Brushes.Black, newRectangleF(x, y, width, height), newStringFormat());
//设置截取位置
sub += charnum;
//设置HasMorePage属性
if (sub < this.txtText.Text.Length)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
sub=0;
}
}
调用打印方法:
//打印预览
private voidbutton2_Click(object sender, EventArgs e)
{
//注意指定其Document属性
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();
}
//打印
private voidbutton1_Click(object sender, EventArgs e)
{
//同样注意指定Document属性
this.printDialog1.Document = this.printDocument1;
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
//触发PrintDocument的PrintPage事件
this.printDocument1.Print();
}
}
<OBJECT id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0" VIEWASTEXT></OBJECT>
<div align="center">
<input onclick="document.all.WebBrowser.ExecWB(6,1)" type="button" value="打印">
<input onclick="document.all.WebBrowser.ExecWB(8,1)" type="button" value="页面设置">
<input onclick="document.all.WebBrowser.ExecWB(7,1)" type="button" value="打印预览">
<input onclick="javascript:window.close();" type="button" value="关闭">
示例一:以表格形式打印DataTable内的数据
//定义全局变量count,储存当前打印的行数
int count=0;
//定义一个方法,接收一个DataTable类型参数及PrintDocument的PrintPage事件传入的参数e以方便操作
private voidPrintTable(DataTable dt, System.Drawing.Printing.PrintPageEventArgse)
{
//取得对应的Graphics对象
Graphicsg = e.Graphics;
//获得相关页面X坐标、Y坐标、打印区域宽度、长度
intx = e.PageSettings.Margins.Left;
inty = e.PageSettings.Margins.Top;
intwidth = e.PageSettings.PaperSize.Width -e.PageSettings.Margins.Left- e.PageSettings.Margins.Right;
intheight = e.PageSettings.PaperSize.Height -e.PageSettings.Margins.Top -e.PageSettings.Margins.Bottom;
//定义打印字体
Fontfont = new Font("宋体",15);
//rowCount是除去打印过的行数后剩下的行数
introwCount = dt.Rows.Count-count;
//maxPageRow是当前设置下该页面可以打印的最大行数
intmaxPageRow=height/(int)font.GetHeight();
//因为是表格,先画一条水平直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y), new Point(x +(dt.Columns.Count) * 135, y));
//再画出表格各列的列标题
for(int i = 0; i < dt.Columns.Count; i++)
{
stringhead = dt.Columns[i].ColumnName;
g.DrawString(head,font, Brushes.Black, x + i * 135, y);
}
//画完标题,再画一条直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (int)font.GetHeight()), newPoint(x + (dt.Columns.Count) * 135, y +(int)font.GetHeight()));
//判断,如果剩下的行数小于可打印的最大行数,则执行下列代码
if(maxPageRow >=rowCount)
{
//当前行数小于Table内总行数时,循环
while(count < dt.Rows.Count)
{
//内循环打印Table内行数据
intcolumnCount = 0;
while(columnCount < dt.Columns.Count)
{
stringtemp = dt.Rows[count][columnCount].ToString();
//打印每个单元格内的数据
g.DrawString(temp,font, Brushes.Black, x + columnCount * 135, y + (count %maxPageRow) * (int)font.GetHeight() +(int)font.GetHeight());
//打印完一行后,继续打印一条直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x +(dt.Columns.Count) * 135, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()));
columnCount++;
}
count++;
}
//所有数据打印完毕后,打印垂直直线
for (int i = 0; i <= dt.Columns.Count; i++)
{
g.DrawLine(newPen(Brushes.Black), new Point(x + i * 135, y), new Point(x + i *135, y + rowCount * (int)font.GetHeight() +(int)font.GetHeight()));
}
}
//判断,如果剩下的行数大于可打印的最大行数,则执行下列代码
else
{
do
{
//与上面类似,注意下面while的条件
intcolumnCount = 0;
while(columnCount < dt.Columns.Count)
{
stringtemp = dt.Rows[count][columnCount].ToString();
//打印每个单元格
g.DrawString(temp,font, Brushes.Black, x + columnCount * 135, y + (count %maxPageRow) * (int)font.GetHeight() +(int)font.GetHeight());
//打印水平直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x +(dt.Columns.Count)*135, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()));
columnCount++;
}
count++;
}while ((count % maxPageRow >0));
//打印垂直直线
for(int i = 0; i <= dt.Columns.Count;i++ )
{
g.DrawLine(newPen(Brushes.Black), new Point(x + i * 135, y), new Point(x + i *135, y + height + (int)font.GetHeight()));
}
}
//指定HasMorePages值,如果页面最大行数小于剩下的行数,则返回true(还有),否则返回false
if(maxPageRow<rowCount)
{
e.HasMorePages= true;
}
else
{
e.HasMorePages= false;
count= 0;
}
}
示例二:文本打印
//设置全局变量保存截取字符串位置
int sub=0;
private void printText(stringtext, System.Drawing.Printing.PrintPageEventArgs e)
{
//取得Graphics实例
Graphics g = e.Graphics;
//获得相关点坐标、长度、宽度
int x = e.PageSettings.Margins.Left;
int y = e.PageSettings.Margins.Right;
int width =e.PageSettings.PaperSize.Width-e.PageSettings.Margins.Left-e.PageSettings.Margins.Right;
int height =e.PageSettings.PaperSize.Height-e.PageSettings.Margins.Top-e.PageSettings.Margins.Bottom;
//设置字体
Font font=new Font("宋体",15);
//这个方法后面讲
g.MeasureString(text.Substring(sub), font, new SizeF(width,height-10), new StringFormat(), out charnum, out line);
//打印string
g.DrawString(text.Substring(sub), font, Brushes.Black, newRectangleF(x, y, width, height), newStringFormat());
//设置截取位置
sub += charnum;
//设置HasMorePage属性
if (sub < this.txtText.Text.Length)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
sub=0;
}
}
调用打印方法:
//打印预览
private voidbutton2_Click(object sender, EventArgs e)
{
//注意指定其Document属性
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();
}
//打印
private voidbutton1_Click(object sender, EventArgs e)
{
//同样注意指定Document属性
this.printDialog1.Document = this.printDocument1;
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
//触发PrintDocument的PrintPage事件
this.printDocument1.Print();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//直接连接打印机打印 这是一个按纽事件你加上去试试 希望对你有所帮助
private void button1_Click(object sender, EventArgs e)
{
PrintDocument MyPrintDoc = new PrintDocument();
MyPrintDoc.PrintPage += new PrintPageEventHandler(MyPrintDoc_PrintPage);
MyPrintDoc.Print();
}
protected void MyPrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
String drawString = "Sample Text";
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
float x = 150.0F;
float y = 50.0F;
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.NoWrap;
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
e.HasMorePages = false;
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument MyPrintDoc = new PrintDocument();
MyPrintDoc.PrintPage += new PrintPageEventHandler(MyPrintDoc_PrintPage);
MyPrintDoc.Print();
}
protected void MyPrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
String drawString = "Sample Text";
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
float x = 150.0F;
float y = 50.0F;
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.NoWrap;
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
e.HasMorePages = false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |