vb.net打印指定文件 vb怎么指定打印机打印

vb.net Document_PrintPag 打印

我帮你把最后一部分的语句顺序调换一下。你试一试

创新互联是一家专业提供青原企业网站建设,专注与成都网站设计、网站制作H5场景定制、小程序制作等业务。10年已为青原众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

sub button1_click() '---执行打印

Dim pd As PrintDocument = New PrintDocument

pd.PrinterSettings = PrintDialog1.PrinterSettings

If _PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

for i=0 to 1 '这样可以两次截图

CaptureScreen() '--执行前面自定义函数截图

AddHandler pd.PrintPage, AddressOf Document_PrintPage

pd.Print()

Threading.Thread.sleep(100) ‘ 再加上一个间隔

next

end sub

VB.NET或C#如何调用某个打印机(例如"Microsoft XPS Document Writer")的“打印首选项”?

实现打印功能的核心是PrintDocument类这个类属于System.Drawing.Printing名字空间这个类封装了当前的打印设置页面设置以及所

有的与打印有关的事件和方法

这个类包括以下几个属性 事件 和方法

1、PrinterSettings 属性

存放打印机的设置信息这个属性不需要程序员设置因为它是由打印对话框获取的

2、PrintCountroller 属性

控制打印过程

3、DefaultPageSettings 属性

存放页面设置信息 打印纸大小方向等也不需要程序员设置因为它是由页面设置对话框获取的

4、DocumentName 属性

指定文档名称,出现在打印机状态窗口中

1。 BeginPrint事件

在打印之前发出

2. PrintPage事件

每打印一页是发出,事件接受一个PrintPageEventArgs参数该参数封装了打印相关的信息

PrintPageEventArgs参数有很多重要的属性

1 Cancel 取消打印

2 Graphics 页面的绘图对象

3 HasMorePages 是否还有要打印的页面

Print 方法 该方法没有参数 调用它将按照当前设置开始打印

若实现打印功能首先构造PrintDocument对象添加打印事件

PrintDocument printDocument;

private void InitializeComponent()

{

...

printDocument=new PrintDocument();

printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);

...

}

实现打印事件功能

打印和绘图类似都是调用Graphics 类的方法进行画图 不同的是一个在显示器上一个在打印纸上并且打印要进行一些复杂的计算

如换行 分页等。

private void printDocument_PrintPage(object sender,PrintPageEventArgs e)

{

StringReader lineReader = new StringReader(textBox.Text);

Graphics g = e.Graphics; //获得绘图对象

float linesPerPage = 0; //页面的行号

float yPosition = 0; //绘制字符串的纵向位置

int count = 0; //行计数器

float leftMargin = e.MarginBounds.Left; //左边距

float topMargin = e.MarginBounds.Top; //上边距

string line = null; 行字符串

Font printFont = this.textBox.Font; //当前的打印字体

SolidBrush myBrush = new SolidBrush(Color.Black);//刷子

linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数

//逐行的循环打印一页

while(count linesPerPage ((line=lineReader.ReadLine()) != null))

{

yPosition = topMargin + (count * printFont.GetHeight(g));

g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());

count++;

}

如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会

自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置

if(line != null)

e.HasMorePages = true;

else

e.HasMorePages = false;

}

打印设置,构造打印对话框 将对话框中设置的Document属性赋给printDocument这样会将用户的设置自动保存到printDocument

的PrinterSettings属性中

protected void FileMenuItem_PrintSet_Click(object sender,EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

printDialog.ShowDialog();

}

页面设置和打印预览与打印设置原理相同都是构造对话框将用户在对话框中的设置保存到相应的类的属性中

protected void FileMenuItem_PageSet_Click(object sender,EventArgs e)

{

PageSetupDialog pageSetupDialog = new PageSetupDialog();

pageSetupDialog.Document = printDocument;

pageSetupDialog.ShowDialog();

}

打印预览

protected void FileMenuItem_PrintView_Click(object sender,EventArgs e)

{

PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();

printPreviewDialog.Document = printDocument;

try

{

printPreviewDialog.ShowDialog();

}

catch(Exception excep)

{

MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

打印就可以直接调用printDocument的Print()方法因为用户可能在打印之前还要再更改打印设置所以

在这里再次显示打印设置对话框

protected void FileMenuItem_Print_Click(object sender,EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

lineReader = new StringReader(textBox.Text);

if (printDialog.ShowDialog() == DialogResult.OK)

{

try

{

printDocument.Print();

}

catch(Exception excep)

{

MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);

printDocument.PrintController.OnEndPrint(printDocument,new PrintEventArgs());

}

}

}

总结打印的过程是

1 在应用程序窗体初始化时构造PrintDocument对象 添加 printDocument 的 PrintPage 方法

2 实现PrintPage方法 4 在用户的单击事件中调用 printDocument 的 Print方法实现打印功能

在这中间可能要用到 PrintDialog PrintPreviewDialog PageSetupDialog 设置和查看打印效

VS2005如何用VB.NET代码实现打印功能

有个PrintDocument控件,可以实现打印。。。

MSDN原话:

使用 PrintDocument 组件

涉及 PrintDocument 组件的两种主要情况是:

简单的打印作业,如打印单个文本文件。在这种情况下,应将 PrintDocument 组件添加到 Windows 窗体,然后在 PrintPage 事件处理程序中添加打印文件的编程逻辑。 该编程逻辑应以使用 Print 方法打印文档结束。

此方法向打印机发送一个 Graphics 对象,该对象包含在 PrintPageEventArgs 类的 Graphics 属性中。

有关如何使用 PrintDocument 组件打印文本文档的示例,请参见

如何:打印 Windows 窗体中的多页文本文件。

更为复杂的打印作业,如想要重新使用已编写的打印逻辑的情况。

在这种情况下,应从 PrintDocument 组件派生一个新组件,并重写

(请参见 Visual Basic 的 重写或 C# 的 重写) PrintPage 事件。

将 PrintDocument 组件添加到窗体后,它出现在 Windows 窗体设计器底部的栏中

VB中用什么代码打印文件啊,语句怎么写啊,

VB中的几种打印方法

1. 采用Visual Basic 提供的简单打印函数PrintForm 方法。应用程序窗体的PrintForm 方法时,Visual Basic 把窗体的位图送到当前打印机。该方法的优点在于它几乎不需要任何编程,但也有很大缺陷。最为突出的是当低分辨率图形在高分辨率打印机上打印时,其结果令人无法满意,会产生锯齿。下面代码将在打印机上打印窗体。

Private Sub Command1_Click() ' 用PrintForm 打印

Me.PrintForm ' 打印窗体的可见区域

End Sub

2. 用Printer 对象进行高分辨率输出。但要想产生复杂的打印输出,编程较为烦琐。Printer 对象代表系统缺省的打印机,Printer 对象支持许多由窗体和图形框所支持的属性和方法,三种对象都有画线和画方框。应用程序可用以下代码在Printer 对象上画出一平方英寸的方框。它离左上角二英寸。注意:打印机以twips 来测量距离。每英寸有1440 个twips。

Printer.Line(2 *1440,2 *1440) -Step(1440,1440), ,B

打印机、从窗体和图形框都有Circle、PaintPicture、Print、Pset、TextHeight、TextWidth 方法。使用这些方法,应用程序可以为打印机生成高分辨率输出。

打印文本直接用Print 方法,见下列代码:

Printer.Print "Hello,China ComputerWorld!" ' 打印字符串

Printer 对象还有一些窗体和图形框都没有方法:

NewPage 告诉打印机,程序对当前输出页的发送已经结束,Printer 对象应开始新的一页。

EndDoc 告诉VB,程序创建文档结束,VB 应将它发送到物理打印机上打印。

KillDoc 取消当前打印作业。应用程序应该终止由EndDoc 和KillDoc 所设定的每个打印作业。

Zoom 属性用于定义打印输出的缩放因子。

Copies 属性用于定义打印的副本数目。

3. 采用直接将数据传送打印机的方法进行打印输出。有两种方法将数据送往打印机。第一种是用Print #方法,就像将数据写入一个文件一样。另一种方法写端口,但不是送文本,而是送特定的PCL 语言,PCL 表示打印控制语言,它是一种特殊语言,用转义代码来控制打印机的具体动作。因为此方法太烦琐,本文不做太多介绍。

4. 如果你在编程时用到了RichTextBox 控制,那么你可以使用该控件的SelPrint 方法来打印,使用非常简单。下面一段代码即用RichTextBox 控件的SelPrint 方法来完成打印。

Private Sub Command3_Click() 'SelPrint 方法

CommonDialog1.Flags=cdlPDReturnDC +cdlPDNoPageNums

If RTF1.SelLength = 0 Then

'RTF1 为窗体的RichTextBox 控制

CommonDialog1.Flags = CommonDialog1.Flags

+cdlPDAllPages

Else

CommonDialog1.Flags = CommonDialog1.Flags +

cdlPDSelection

End If

CommonDialog1.CancelError = True

On Error Resume Next

CommonDialog1.ShowPrinter

If Err.Number = cdlCancel Then Exit Sub

If Err.Number 0 Then

Beep

MsgBox "Error printing file."

&vbCrLf +Err.Description, vbOKOnly +vbExclamation,

“Printing Error!"

Exit Sub

End If

Printer.Print ""

RTF1.SelPrint CommonDialog1.hDC

' 打印RTF1 控件的可见区域

End Sub

上面代码先进行打进设置,再进行打印。如果不需要设置,采用下面代码更为简单:

RTF1.SelPrint Printer.hDC

' 打印RTF1 控件的可见区域

5. 可以在VB 中调用Word 97 提供的OLE 自动化服务,利用Word 97 强大的打印功能来完成VB 打印,笔者认为这是最令人满意的方法。下面代码说明VB 如何与Word 集成。

Private Sub Command4_Click() ' 调用Word 打印

Dim objWord As Object

Const CLASSOBJECT = "Word.Application"

On Error GoTo objError

Set objWord = CreateObject(CLASSOBJECT)

objWord.Visible = True

objWord.Documents.Add

With objWord

.ActiveDocument.Paragraphs.Last.Range.Bold = False

.ActiveDocument.Paragraphs.Last.Range.Font.Size =20

.ActiveDocument.Paragraphs.Last.Range.Font.Name =

"黑体"

.ActiveDocument.Paragraphs.Last.Range.Font.ColorIndex==4

.ActiveDocument.Paragraphs.Last.Range.Text =

"我是计算机世界读者!"

End With

Clipboard.Clear

Clipboard.SetText

"通过剪切板向WORD 传送数据!"

objWord.Selection.Paste

objWord.PrintPreview = True ' 预览方式

'objWord.PrintOut' 执行打印

'objWord.Quit' 退出Word

Exit Sub

objError:

If Err 429 Then

MsgBox Str $(Err) &Error $

Set objWord = Nothing

' 不能创建Word 对象则退出

Exit Sub

Else

Resume Next

End If

End Sub

6. 用VC 编制DLL 模块完成打印。在VB 中调用该模块,用混合编程方法进行打印输出。因涉及VC 编程比较烦琐,这里不再讨论。

请问谁知道怎么用vb.net 打印.log日志文件, 用代码写的,谢谢了,急用

Dim sw As StreamWriter = New StreamWriter(“c:\xxxxx.log”, True) 'true是指以追加的方式打开指定文件

For i = 0 To j

temp = i.ToString

sw.WriteLine(temp)

sw.Flush()

Next

sw.Close()

sw = Nothing

vb.net如何实现打印DataGridView1里的内容,求源码

使用 PrintDocument 控件的 Print() 方法可以打印指定对象中的内容,参考代码如下:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PrintDocument1.Print()

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)

DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))

e.Graphics.DrawImage(bm, 0, 0)

End Sub


文章标题:vb.net打印指定文件 vb怎么指定打印机打印
网页网址:http://scyanting.com/article/doopdjd.html