如何打印硬盘上的一个文件? 有个html或者txt 在 C:\abc.html(txt) 处,我想写个winform直接打印出该文件,请问是否可行?代码如何写?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void mnuFileOpen_Click(object sender, System.EventArgs e) { if (dlgOpen.ShowDialog()==DialogResult.OK) { StreamReader sr=null; try { sr=new StreamReader(dlgOpen.FileName,Encoding.Default,true); txtDoc.Text=sr.ReadToEnd(); } catch { MessageBox.Show("打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error); } finally { if (sr!=null) sr.Close(); } } } private void mnuFormatFont_Click(object sender, System.EventArgs e) { dlgFont.Font=txtDoc.Font; if (dlgFont.ShowDialog()==DialogResult.OK) { txtDoc.Font=dlgFont.Font; } } private void pdoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { //MessageBox.Show("开始打印啦"); } private void pdoc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { //MessageBox.Show("打印结束"); } private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Graphics g=e.Graphics; float lineHeight=txtDoc.Font.GetHeight(g); int linesPerPage=(int)(e.MarginBounds.Height/lineHeight); int count=0; //本页已打印行数 while (count<linesPerPage && totalLines<txtDoc.Lines.Length) { g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y+lineHeight*count); count++; totalLines++; } if (totalLines<txtDoc.Lines.Length) { e.HasMorePages=true; } else { e.HasMorePages=false; totalLines=0; } } private void mnuFilePrint_Click(object sender, System.EventArgs e) { if (dlgPrinterSetup.ShowDialog()==DialogResult.OK) { pdoc.Print(); //开始执行打印 } } private void mnuFilePrintPreview_Click(object sender, System.EventArgs e) { ppd.ShowDialog(); } private void mnuFilePageSetup_Click(object sender, System.EventArgs e) { Margins oldMargins=dlgPageSetup.PageSettings.Margins; dlgPageSetup.PageSettings.Margins=new Margins((int)(oldMargins.Left*2.54),(int)(oldMargins.Right*2.54),(int)(oldMargins.Top*2.54),(int)(oldMargins.Bottom*2.54)); if (dlgPageSetup.ShowDialog()==DialogResult.Cancel) { dlgPageSetup.PageSettings.Margins=oldMargins; } } }} http://msdn.microsoft.com/zh-cn/library/9yfktb35(VS.80).aspx这个也可以参考! 你是说print到screen?还是要用printer? 认真看了下代码,如果目标文件是个 html ,那么好像就会打印html源代码出来吧?是否可以打印html布局页面出来呢? Guyschaos,请问是否有办法解决呢?当然最好不要解析 html 放到 webbrowser 内,然后 webbrowser1.Print() 就成功了,非常方便,超级感谢 Guyschaos,亲个,。。 C#怎么声明导出函数 DLL 小弟求帮助,写了一个求质数的程序,可无法运行 关于皮肤IrisSkin2的使用问题 求.Net的一个控件名称 生日!! 关于强类型DataSet的问题 如何完成对Treeview控件中选择框CheckBox的全选处理 请问怎样查看.net自带的类的源代码? 记事本 那里有《《C#与模式》》下载 谁能给我个winmm.dll里面可引用的函数及说明啊? 如何我的应用程序打补丁
{
if (dlgOpen.ShowDialog()==DialogResult.OK)
{
StreamReader sr=null;
try
{
sr=new StreamReader(dlgOpen.FileName,Encoding.Default,true);
txtDoc.Text=sr.ReadToEnd();
}
catch
{
MessageBox.Show("打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
if (sr!=null) sr.Close();
}
}
} private void mnuFormatFont_Click(object sender, System.EventArgs e)
{
dlgFont.Font=txtDoc.Font;
if (dlgFont.ShowDialog()==DialogResult.OK)
{
txtDoc.Font=dlgFont.Font;
}
} private void pdoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//MessageBox.Show("开始打印啦");
} private void pdoc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//MessageBox.Show("打印结束");
} private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g=e.Graphics;
float lineHeight=txtDoc.Font.GetHeight(g);
int linesPerPage=(int)(e.MarginBounds.Height/lineHeight);
int count=0; //本页已打印行数 while (count<linesPerPage && totalLines<txtDoc.Lines.Length)
{
g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y+lineHeight*count);
count++;
totalLines++;
} if (totalLines<txtDoc.Lines.Length)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
totalLines=0;
}
} private void mnuFilePrint_Click(object sender, System.EventArgs e)
{
if (dlgPrinterSetup.ShowDialog()==DialogResult.OK)
{
pdoc.Print(); //开始执行打印
}
}
private void mnuFilePrintPreview_Click(object sender, System.EventArgs e)
{
ppd.ShowDialog();
} private void mnuFilePageSetup_Click(object sender, System.EventArgs e)
{
Margins oldMargins=dlgPageSetup.PageSettings.Margins;
dlgPageSetup.PageSettings.Margins=new Margins((int)(oldMargins.Left*2.54),(int)(oldMargins.Right*2.54),(int)(oldMargins.Top*2.54),(int)(oldMargins.Bottom*2.54));
if (dlgPageSetup.ShowDialog()==DialogResult.Cancel)
{
dlgPageSetup.PageSettings.Margins=oldMargins;
}
}
}
}
这个也可以参考!
是否可以打印html布局页面出来呢?