VB打印txt,问题请教,在线等
VB打印txt,问题请教,在线等我用的是RichTextBox,先把TXT内容导入Richtextbox,再进行打印.
代码如下:
RichTxtPrint.LoadFile "C:\1.txt"
RichTxtPrint.SelPrint Printer.hdc
但是这样有个问题,我执等到 RichTxtPrint.SelPrint Printer.hdc
这一句时,会弹出一个对话框出来,提示保存什么的,我想在打印的时候不让这个
框显示出来,而直接打印,怎么弄.
谢谢啦 --------------------编程问答-------------------- 好像有个API,
SetErrorMode,可以屏蔽大部分系统提示。
--------------------编程问答--------------------
UINT OldMode = SetErrorMode(0); //得到缺省设置
SetErrorMode(OldMode & !SEM_FAILCRITICALERRORS);//设置处理方式
'SHFormatDrive (m_hWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
SetErrorMode(OldMode); // 恢复缺省设置
试试 --------------------编程问答-------------------- 不很具体,
能否详细一点,提示的不是错误的提示,而是提示保存.mdi扩展名的东西,单击保
存后,会跳出打印预览类似的, --------------------编程问答-------------------- 不很具体,
能否详细一点,提示的不是错误的提示,而是提示保存.mdi扩展名的东西,单击保
存后,会自动打印,现在我就是要去掉这个对话框,直接打印,怎么弄:
我也用了另外一种方法:
ShellExecute(Me.hwnd, "print", App.Path + "\defectOutput.txt", vbNullString, vbNullString, SW_HIDE)
不过也还是有上面提到的对话框...... --------------------编程问答-------------------- 那就直接操作打印机吧。 --------------------编程问答-------------------- '添加 Command1 RichTextBox1
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_GETLINECOUNT = &HBA
Const EM_GETLINE = &HC4
Dim linecnt&, i&
Private Sub Form_Load()
RichTextBox1.LoadFile "c:\1.txt", 1
End Sub
Private Sub Command1_Click()
linecnt = TextBoxLineCnt(RichTextBox1)
For i = 0 To linecnt - 1
Printer.Print Getlinetext(RichTextBox1, i)
Next i
Printer.EndDoc
End Sub
Public Function Getlinetext(RichTextBox1 As RichTextBox, ByVal ntx As Long) As String
Dim str1(255) As Byte, str2$
str1(0) = 255
i = SendMessage(RichTextBox1.hwnd, EM_GETLINE, ntx, str1(0))
If i = 0 Then
Getlinetext = ""
Else
str2 = StrConv(str1, vbUnicode)
Getlinetext = Left(str2, InStr(1, str2, Chr(0)) - 1)
End If
End Function
Public Function TextBoxLineCnt(ct1 As RichTextBox) As Long
TextBoxLineCnt = SendMessage(ct1.hwnd, EM_GETLINECOUNT, 0, 0)
End Function
--------------------编程问答-------------------- cbm666,你好!你的方法跟我上面的
ShellExecute(Me.hwnd, "print", App.Path + "\defectOutput.txt", vbNullString, vbNullString, SW_HIDE)
方法功能差不多,都还是会跳出一个对话框,让你保存扩展名为:.mdi的对话框,单击保存,就可以打印了.
这是怎么回事,总去不掉,我只想在单击button打印的时候,这个对话框不跳出来,直接打印. --------------------编程问答-------------------- Private Sub Command1_Click()
linecnt = TextBoxLineCnt(RichTextBox1)
SendKeys Chr(13) '加上这个试看
For i = 0 To linecnt - 1
Printer.Print Getlinetext(RichTextBox1, i)
Next i
Printer.EndDoc
End Sub
--------------------编程问答-------------------- 如不行的话改用这方式
Private WithEvents Timer1 As Timer, Label1 As Label
Private Sub Form_Load()
Set Timer1 = Controls.Add("vb.timer", "Timer1")
Timer1.Interval = 1000 '或 200 500 2000 自己试
Timer1.Enabled = False
SendKeys Chr(13)
End Sub
Private Sub Command1_Click()
linecnt = TextBoxLineCnt(RichTextBox1)
Timer1.Enabled = True
For i = 0 To linecnt - 1
Printer.Print Getlinetext(RichTextBox1, i)
Next i
Printer.EndDoc
End Sub
Private Sub Timer1_Timer()
SendKeys Chr(13)
Timer1.Enabled = False
End Sub
--------------------编程问答-------------------- SendKeys (Chr(13)) --------------------编程问答-------------------- 安装钩子捕获他的弹出 --------------------编程问答-------------------- cbm666,你好
Private WithEvents Timer1 As Timer, Label1 As Label
Private Sub Form_Load()
Set Timer1 = Controls.Add("vb.timer", "Timer1")
Timer1.Interval = 1000 '或 200 500 2000 自己试
Timer1.Enabled = False
SendKeys Chr(13)
End Sub
Private Sub Command1_Click()
linecnt = TextBoxLineCnt(RichTextBox1)
Timer1.Enabled = True
For i = 0 To linecnt - 1
Printer.Print Getlinetext(RichTextBox1, i)
Next i
Printer.EndDoc
End Sub
Private Sub Timer1_Timer()
SendKeys Chr(13)
Timer1.Enabled = False
End Sub
这个捕获button :保存 是可以的.它会自动保存在"我的文档"下面生成一个一个的扩展名为.mdi的文件,
我每打印完一个文档,我怎样自动删掉这些文件呢?请再帮忙 --------------------编程问答-------------------- Private Sub Command1_Click()
aa = Environ("userprofile") & "\My Documents\???.mdi"
If Dir(aa) <> "" Then Kill aa
End Sub
备注:如果是多个 .mid一次全删的话, 则先枚举Environ("userprofile") & "\My Documents 文件夹下所有的*.mid
再下个命令, shell del....一次全干完, 需要这要的话再回帖我帮你写.
补充:VB , 基础类