VB中打印机选择
- 格式:pdf
- 大小:200.31 KB
- 文档页数:11
(1)
假设你的COMMONDIALOG控件叫CMDiag
用如下代码可以使你选择的打印机成为默认打印机"Printer"OnErrorResumeNext
CMDiag.PrinterDefault=True
CMDiag.CancelError=True
CMDiag.ShowPrinter
IfErr.Number<>0ThenExitSub
(2)VB里面,原本改变预设打印机的方法是:(假设安装有两种打印机(驱动程式))
SetPrinter=Printers(0)'将预设打印机设定成第一种打印机
SetPrinter=Printers(1)'将预设打印机设定成第二种打印机
但实际上以上叙述有时候不会成功(原因不详),为了能够成功地改变预设打印机,
以下是呼叫WindowsAPI的方法:(此一解决方案适用於Windows95,98)
1.API的宣告:
ConstHWND_BROADCAST=&HFFFF&
ConstWM_WININICHANGE=&H1A
PrivateDeclareFunctionGetProfileStringLib"kernel32"Alias_
"GetProfileStringA"(ByVallpAppNameAsString,ByVallpKeyNameAs_
String,ByVallpDefaultAsString,ByVallpReturnedStringAsString,_
ByValnSizeAsLong)AsLong
PrivateDeclareFunctionWriteProfileStringLib"kernel32"Alias_
"WriteProfileStringA"(ByVallpszSectionAsString,ByVallpszKeyName_
AsString,ByVallpszStringAsString)AsLong
PrivateDeclareFunctionSendMessageLib"user32"Alias"SendMessageA"_
(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,_
lParamAsAny)AsLong
2.程式范例:
PrinterName="您想设定的打印机名称"
DimSAsString,lengthAsLong,hKeyAsLong
S=String(80,Chr(0))
length=GetProfileString("devices",PrinterName,"",S,Len(S))
S=Left(S,length)
CallWriteProfileString("windows","device",PrinterName&","&S)CallSendMessage(HWND_BROADCAST,WM_WININICHANGE,&H7FFF&,ByVal"windows")
至於改变NT预设打印机的方法,则是改变登录资料库(Registry)打印机的设定,
在登录资料库中纪录
预设打印机的Value是:HKEY_CURRENT_USER
\Software\Microsoft\WindowsNT\CurrentVersion\Windowssubkey的Devicevalue
(3)VB中获取指定打印机的各种纸张类型及大小
放入一个MSFlexGrid,名称为fgd1,列数为4
'OptionExplicit
PrivateConstDC_MAXEXTENT=5
PrivateConstDC_MINEXTENT=4
PrivateConstDC_PAPERNAMES=16
PrivateConstDC_PAPERS=2
PrivateConstDC_PAPERSIZE=3
PrivateDeclareFunctionDeviceCapabilitiesLib"winspool.drv"Alias"De
viceCapabilitiesA"(ByVallpDeviceNameAsString,ByVallpPortAsStrin
g,ByValiIndexAsLong,lpOutputAsAny,lpDevModeAsAny)AsLong
PrivateTypePOINTS
xAsLong
yAsLong
EndType
PrivateSubForm_Load()
DimiAsLong
Withfgd1
.Clear
.FormatString="^纸张编号|^纸张名称|^纸张长度|^纸张宽度"
Fori=0To.Cols-1
.ColWidth(i)=1700
Nexti
.AllowUserResizing=flexResizeColumns
.Left=0
.Width=Me.ScaleWidth
EndWith
GetPaperInfo
EndSub
PrivateSubGetPaperInfo()
DimiAsLong,retAsLong
DimLengthAsInteger,WidthAsInteger
DimPaperNo()AsInteger,PaperName()AsString,PaperSize()AsPOINTS
'支持最大打印纸:ret=DeviceCapabilities(打印机名
称,"LPT1",DC_MAXEXTENT,ByVal0&,ByVal0&)
Length=ret\65536
Width=ret-Length*65536
'lblMaxLength.Caption=Length
'lblMaxWidth.Caption=Width
'支持最小打印纸:
ret=DeviceCapabilities(打印机名
称,"LPT1",DC_MINEXTENT,ByVal0&,ByVal0&)
Length=ret\65536
Width=ret-Length*65536
'支持纸张种类数
ret=DeviceCapabilities(打印机名
称,"LPT1",DC_PAPERS,ByVal0&,ByVal0&)
'纸张编号
ReDimPaperNo(1Toret)AsInteger
CallDeviceCapabilities(打印机名
称,"LPT1",DC_PAPERS,PaperNo(1),ByVal0&)
'纸张名称
DimarrPageName()AsByte
DimallNamesAsString
DimlStartAsLong,lEndAsLong
ReDimPaperName(1Toret)AsString
ReDimarrPageName(1Toret*64)AsByte
CallDeviceCapabilities(打印机名
称,"LPT1",DC_PAPERNAMES,arrPageName(1),ByVal0&)
allNames=StrConv(arrPageName,vbUnicode)
'loopthroughthestringandsearchforthenamesofthepapers
i=1
Do
lEnd=InStr(lStart+1,allNames,Chr$(0),vbBinaryCompare)
If(lEnd>0)And(lEnd-lStart-1>0)Then
PaperName(i)=Mid$(allNames,lStart+1,lEnd-lStart
-1)
i=i+1
EndIf
lStart=lEnd
LoopUntillEnd=0
'纸张尺寸
ReDimPaperSize(1Toret)AsPOINTS
CallDeviceCapabilities(Form2.Combo1.Text,"LPT1",DC_PAPERSIZE,PaperSize(
1),ByVal0&)
'显示在表格中Fori=1Toret
fgd1.AddItemPaperNo(i)&vbTab&PaperName(i)&vbTab&Paper
Size(i).y&vbTab&PaperSize(i).x
Nexti
'移除第一个空行
fgd1.Row=1
fgd1.RemoveItem1
EndSub
PrivateSubForm_Resize()
Withfgd1
.Left=0
.Width=Me.ScaleWidth
.Height=Me.ScaleHeight
.Top=0
EndWith
EndSub
(5)编写VB打印控制程序的几点心得
摘要本文在总结作者实际开发经验的基础上,详细介绍了VB实现高分辨率打印方法
的几点心得。阐述了参数化绘图程序缩短打印程序开发时间的方法以及打印机缩
放属性与窗体属性匹配使用的技巧,并总结了解决坐标定位、图形与其实际打印
位置出现误差等问题的经验。
关键词PrintForm高分辨率打印参数化绘图缩放属性
SomeExperiencesonVBPrintControlBaiYang,WangPengComputerScienceDepartmentUniversityofInformationandEngineeringZhengzhou,China
Abstract:Onthebasisofourworkingexperiences,wehavegivenadetaildescriptionaboutVBprintmethodofhighresolution.Wefocusonhowtoshortendevelopingperiodwithparameterizedplotprogram,thematchbetweenthezoomattributeandtheattributeofdisplaywindowetc.,whichbasedonprintprogramofPrinterObject.Inadditiontothis,wehaveintroducedsomeexperiencesondealingwithpositionerrorthatcausedbythemismatchbetweenfontandgraphonscreenandthatonprinter.
Keywords:PrintForm,HighResolutionPrint,parameterizedplot,zoomattribute
1.简介