当前位置:文档之家› VB中打印机选择

VB中打印机选择

.(1)假设你的COMMONDIALOG控件叫CMDiag用如下代码可以使你选择的打印机成为默认打印机"Printer"On Error Resume NextCMDiag.PrinterDefault = TrueCMDiag.CancelError = TrueCMDiag.ShowPrinterIf Err.Number <> 0 Then Exit Sub(2)VB 里面,原本改变预设打印机的方法是:(假设安装有两种打印机(驱动程式))Set Printer = Printers(0) ' 将预设打印机设定成第一种打印机Set Printer = Printers(1) ' 将预设打印机设定成第二种打印机但实际上以上叙述有时候不会成功(原因不详),为了能够成功地改变预设打印机,以下是呼叫Windows API 的方法:( 此一解决方案适用於Windows 95,98)1. API 的宣告:Const HWND_BROADCAST = &HFFFF&Const WM_WININICHANGE = &H1APrivate Declare Function GetPro Lib "kernel32" Alias _"GetPro" (ByVal lpAppName As String, ByVal lpKeyName As _String, ByVal lpDefault As String, ByVal lpReturnedString As String, _ByVal nSize As Long) As LongPrivate Declare Function WritePro Lib "kernel32" Alias _"WritePro" (ByVal lpszSection As String, ByVal lpszKeyName _As String, ByVal lpszString As String) As LongPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" _(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long2. 程式范例:PrinterName = "您想设定的打印机名称"Dim S As String, length As Long, hKey As LongS = String(80, Chr(0))length = GetPro("devices", PrinterName, "", S, Len(S))S = Left(S, length)Call WritePro("windows", "device", PrinterName & "," & S)Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, &H7FFF&, ByVal "windows")至於改变NT 预设打印机的方法,则是改变登录资料库(Registry)打印机的设定,在登录资料库中纪录预设打印机的Value 是:HKEY_CURRENT_USER\Software\Microsoft\WindowsNT\CurrentVersion\Windows subkey 的Device value(3)VB中获取指定打印机的各种纸张类型及大小放入一个MSFlexGrid,名称为fgd1,列数为4'Option ExplicitPrivate Const DC_MAXEXTENT = 5Private Const DC_MINEXTENT = 4Private Const DC_PAPERNAMES = 16Private Const DC_PAPERS = 2Private Const DC_PAPERSIZE = 3Private Declare Function DeviceCapabilities Lib "winspool.drv" Alias "De viceCapabilitiesA" (ByVal lpDeviceName As String, ByVal lpPort As Strin g, ByVal iIndex As Long, lpOutput As Any, lpDevMode As Any) As Long Private Type POINTSx As Longy As LongEnd TypePrivate Sub Form_Load()Dim i As LongWith fgd1.Clear.FormatString = "^纸张编号|^纸张名称|^纸张长度|^纸张宽度"For i = 0 To .Cols - 1.ColWidth(i) = 1700Next i.AllowUserResizing = flexResizeColumns.Left = 0.Width = Me.ScaleWidthEnd WithGetPaperInfoEnd SubPrivate Sub GetPaperInfo()Dim i As Long, ret As LongDim Length As Integer, Width As IntegerDim PaperNo() As Integer, PaperName() As String, PaperSize() As POINTS '支持最大打印纸:ret = DeviceCapabilities(打印机名称, "LPT1", DC_MAXEXTENT, ByVal 0&, ByVal 0&)Length = ret \ 65536Width = ret - Length * 65536'lblMaxLength.Caption = Length'lblMaxWidth.Caption = Width'支持最小打印纸:ret = DeviceCapabilities(打印机名称, "LPT1", DC_MINEXTENT, ByVal 0&, ByVal 0&)Length = ret \ 65536Width = ret - Length * 65536'支持纸张种类数ret = DeviceCapabilities(打印机名称, "LPT1", DC_PAPERS, ByVal 0&, ByVal 0&)'纸张编号ReDim PaperNo(1 To ret) As IntegerCall DeviceCapabilities(打印机名称, "LPT1", DC_PAPERS, PaperNo(1), ByVal 0&)'纸张名称Dim arrPageName() As ByteDim allNames As StringDim lStart As Long, lEnd As LongReDim PaperName(1 To ret) As StringReDim arrPageName(1 To ret * 64) As ByteCall DeviceCapabilities(打印机名称, "LPT1", DC_PAPERNAMES, arrPageName(1), ByVal 0&)allNames = StrConv(arrPageName, vbUnicode)'loop through the string and search for the names of the papersi = 1DolEnd = InStr(lStart + 1, allNames, Chr$(0), vbBinaryCompare)If (lEnd > 0) And (lEnd - lStart - 1 > 0) ThenPaperName(i) = Mid$(allNames, lStart + 1, lEnd - lStart - 1)i = i + 1End IflStart = lEndLoop Until lEnd = 0'纸张尺寸ReDim PaperSize(1 To ret) As POINTSCall DeviceCapabilities(bo1.Text, "LPT1", DC_PAPERSIZE, PaperSize( 1), ByVal 0&)'显示在表格中For i = 1 To retfgd1.AddItem PaperNo(i) & vbTab & PaperName(i) & vbTab & Paper Size(i).y & vbTab & PaperSize(i).xNext i'移除第一个空行fgd1.Row = 1fgd1.RemoveItem 1End SubPrivate Sub Form_Resize()With fgd1.Left = 0.Width = Me.ScaleWidth.Height = Me.ScaleHeight.Top = 0End WithEnd Sub(5)编写VB打印控制程序的几点心得摘要本文在总结作者实际开发经验的基础上,详细介绍了VB实现高分辨率打印方法的几点心得。

阐述了参数化绘图程序缩短打印程序开发时间的方法以及打印机缩放属性与窗体属性匹配使用的技巧,并总结了解决坐标定位、图形与其实际打印位置出现误差等问题的经验。

关键词 PrintForm 高分辨率打印参数化绘图缩放属性Some Experiences on VB Print ControlBai Yang, Wang PengComputer Science DepartmentUniversity of Information and EngineeringZhengzhou, ChinaAbstract: On the basis of our working experiences, we have given a detail description about VB print method of high resolution. We focus on how to shorten developing period with parameterized plot program, the match between the zoom attribute and the attribute of display window etc., which based on print program of Printer Object. In addition to this, we have introduced some experiences on dealing with position error that caused by the mismatch between font and graph on screen and that on printer.Keywords:PrintForm, High Resolution Print, parameterized plot ,zoom attribute1.简介Visual Basic(VB)给用户提供了可视化编程环境,因其简单易学、功能强大而得到了广泛的应用。

相关主题