您的位置:首页 > 编程语言 > VB

如何打印整个 VB 窗体和控制打印大小

2009-03-17 21:49 621 查看
 在 Windows PrintForm Visual Basic 方法提供了一种可以打印与客户区的窗体方法。 但是,重新实现不允许您控制大小或在打印的输出的比例,或打印该非客户端区域 (标题和边框) 窗体。代码下例打印整个的窗体中使用 Windows API 函数,并提供了一种方法控制输出的大小。 此方法也可用于打印仅与客户区特定的大小与控件来使文本或要为窗体的图像在同一页上打印的其他图形在打印表单的位置。 该方法是同样适用于打印项目中的所有窗体。注意: 此示例将无法正常工作在 PostScript 打印机。 为了使本示例正常工作,打印机都必须使用标准的非 PostScript 激光打印机配置 (如 PCL / HP)。 将 Windows API 函数 bitblt,StretchBlt、 CreateCompatibleDC、 DeleteDC、 SelectObject 和转义允许更好地的位置和大小打印出的表单控制比重新实现方法。 在由两部分组成的进程中整个窗体的图像由一个不可见的图片使用 bitblt 捕获,并转换永久位图使用 AutoRedraw 属性。 然后将图片打印使用打印图片控件 (其边框为通过查询 Microsoft 知识库中以下单词找到一单独文章) 的方法:CreateCompatibleDC此方法效果最大化的窗体以及任何较小的窗体。 使用 GetSystemMetrics 允许处理不同的窗口的边框样式通过查询大小以像素为单位) 的 Windows 标准边框的视频驱动程序传递给它的常规过程。下面此示例需要一个表单与不可见的图片控件。

示例

将以下代码添加到新的项目中窗体的 General Declarations 级别中:注意: 下面的所有声明语句必须都是在一行每个。DefInt A-Z
Declare Function BitBlt Lib "gdi" (ByVal hDestDC, ByVal X, ByVal Y,ByVal nWidth, ByVal nHeight, ByVal hSrcDC, ByVal XSrc,ByVal YSrc, ByVal dwRop&)Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC)Declare Function SelectObject Lib "GDI" (ByVal hDC, ByVal hObject)Declare Function StretchBlt Lib "GDI" (ByVal hDC, ByVal X, ByVal Y,ByVal nWidth, ByVal nHeight, ByVal hSrcDC, ByVal XSrc,ByVal YSrc, ByVal nSrcWidth, ByVal nSrcHeight, ByVal dwRop&)Declare Function DeleteDC Lib "GDI" (ByVal hDC)Declare Function Escape Lib "GDI" (ByVal hDC, ByVal nEscape,ByVal nCount, lplnData As Any, lpOutData As Any)Declare Function GetSystemMetrics Lib "User" (ByVal nIndex)Const SM_CYCAPTION = 4Const SM_CXBORDER = 5Const SM_CYBORDER = 6Const SM_CXDLGFRAME = 7Const SM_CYDLGFRAME = 8Const SM_CXFRAME = 32Const SM_CYFRAME = 33Const TWIPS = 1Const PIXEL = 3Const NILL = 0&Const SRCCOPY = &HCC0020Const NEWFRAME = 1Dim ModeRatio, XOffset, YOffset As Integer
在设计时设置下列属性:
   Control         Property      Setting-------         --------      -------Form1           Name          Form1 (default)Form1.Picture1  Name          Picture1 (default)Form1.Picture2  Name          Picture2 (default)Form1.File1     Name          File1 (default)(In Visual Basic version 1.0 for Windows, set the CtlName/FormNameProperty for the above objects instead of the Name property.)
可以将任何其他控件添加到窗体打印。 如果在运行时绘制图片控件,一定其 AutoRedraw 属性设置为 True,以便图形将进行传输的 Windows API 调用 bitblt 和最终打印 StretchBlt。将以下代码添加到 Form 1 的 Form _ Load 过程中:
Sub Form_Load ()' Size the form explicitly to match parameters of StretchBlt.' Or use design time size to set coordinates.Form1.Move 1095, 1200, 8070, 5280' Size two example controls.File1.Move 4080, 120, 2775, 2535Picture1.Move 240, 120, 2775, 2535' Put up a caption to indicate how to print the form.Form1.Caption = "Double Click to Print Form And Text"' The following *optional* code illustrates creating a persistent' bitmap that will successfully StretchBlt to the printer.Picture1.AutoRedraw = -1  ' Create persistent bitmap of picture' contents.Picture1.Line (0, 0)-(Picture1.ScaleWidth / 2,Picture1.ScaleHeight / 2), , BFPicture1.AutoRedraw = 0   ' Toggle off.' Make sure the temporary workspace picture is invisible.Picture2.visible = 0End Sub
将以下代码添加到窗体的常规过程级别中:
Sub FormPrint (localname As Form)' Display cross.screen.MousePointer = 2' Calculate ratio between ScaleMode twips and ScaleMode pixel.localname.ScaleMode = PIXELModeRatio = localname.height / localname.ScaleHeightlocalname.ScaleMode = TWIPSXOffset = (localname.width - localname.ScaleWidth) / ModeRatioYOffset = (localname.height - localname.ScaleHeight) / ModeRatioCapSize% = GetSystemMetrics(SM_CYCAPTION) ' The height of the caption.' The size of the fixed single border:FudgeFactor% = GetSystemMetrics(SM_CYBORDER)' The fudgefactor is due to inevitable mapping errors when converting' logical pixels to screen pixels. This example is coded for 640X480' screen resolution. For 800X600, remove the fudgefactor.' For other resolutions, tweak for perfection!Select Case localname.BorderStyleCase 0      ' None.XOffset = 0YOffset = 0Case 1      ' Fixed Single.XOffset = GetSystemMetrics(SM_CXBORDER)YOffset = GetSystemMetrics(SM_CYBORDER) + CapSize% - FudgeFactor%Case 2      ' Sizeable.XOffset = GetSystemMetrics(SM_CXFRAME)YOffset = GetSystemMetrics(SM_CYFRAME) + CapSize% - FudgeFactor%Case 3      ' Fixed Double.XOffset = GetSystemMetrics(SM_CXDLGFRAME) + FudgeFactor%YOffset = GetSystemMetrics(SM_CYDLGFRAME) + CapSize%End Select' Size the picture to the size of the form's non-client (complete)' area.Picture2.Move 0, 0, localname.Width, localname.Height' NOTE: Bitblt requires coordinates in pixels.Picture2.ScaleMode = PIXEL' Clear Picture property of any previous BitBlt image.Picture2.Picture = LoadPicture("")' -1 equals true: Must Have This!!!Picture2.AutoRedraw = -1' Assign information of the destination bitmap.hDestDC% = Picture2.hDCX% = 0: Y% = 0nWidth% = Picture2.ScaleWidthnHeight% = Picture2.ScaleHeight' Assign information of the source bitmap.' Source is entire client area of form (plus non-client area)' XOffset and YOffset settings depend on the BorderStyle chosen for' the form.hSrcDC% = localname.hDCXSrc% = -XOffset: YSrc% = -YOffset' Show transition to BitBlt by changing MousePointer.Screen.MousePointer = 4' Assign the SRCCOPY constant to the Raster operation.dwRop& = SRCCOPY' The following statement must appear on one line.Suc% = BitBlt(hDestDC%, X%, Y%, nWidth%, nHeight%, hSrcDC%, XSrc%,YSrc%, dwRop&)' Start the StretchBlt process now.' Assign persistent bitmap to Picture property:Picture2.Picture = Picture2.Image' StretchBlt requires pixel coordinates.Picture2.ScaleMode = PIXELPrinter.ScaleMode = PIXEL' * The following is an example of mixing text with StretchBlt.Printer.Print "This is a test of adding text and bitmaps "Printer.Print "This is a test of adding text and bitmaps "Printer.Print "This is a test of adding text and bitmaps "' * If no text is printed in this procedure,' * then you must add minimum: Printer.Print " "' * to initialize Printer.hDC.' Now display hour glass for the StretchBlt to printer.screen.MousePointer = 11hMemoryDC% = CreateCompatibleDC(Picture2.hDC)hOldBitMap% = SelectObject(hMemoryDC%, Picture2.Picture)' You adjust the vertical stretch factor of the form in the' argument "Printer.ScaleHeight - 1000":ApiError% = StretchBlt(Printer.hDC, 0, 192,Printer.ScaleWidth - 300, Printer.ScaleHeight - 1000,hMemoryDC%, 0, 0, Picture2.ScaleWidth,Picture2.ScaleHeight, SRCCOPY)  ' concatenate above' The second parameter above allows for text already printed: modify' accordingly.hOldBitMap% = SelectObject(hMemoryDC%, hOldBitMap%)ApiError% = DeleteDC(hMemoryDC%)' * The followinga95cis an example of mixing text with StretchBlt.' Set the printer currentY to allow for the size of the StretchBlt' image. (This is relative to size of form and stretch factors chosen)Printer.currentY = 2392 ' In Twips.Printer.Print "This is for text after the StretchBlt"Printer.Print "This is for text after the StretchBlt"Printer.Print "This is for text after the StretchBlt"Printer.EndDocApiError% = Escape(Printer.hDC, NEWFRAME, 0, NILL, NILL)' Reset MousePointer to default.Screen.MousePointer = 1End Sub
将以下代码添加到 Double_Click 事件:
Sub Form_DblClick ()FormPrint Form1End Sub
保存项目后, 运行此示例。双击该窗体来调用该 FormPrint 过程。 将打印作为参数传递到 FormPrint 任何窗体。 bitblt 会将到将图片控件的图像,然后 StretchBlt 将其传送到该打印机将打印 bitblt 已传输的图像的 DC。(可选),可以使用 StretchBlt 打印之前将放文本或图片 (Form1.Picture2) 中的图形,或直接打印到使用 Printer.Print 或 printer.line 页。 如果您通过调整 StretchBlt 的第二个和第三个参数选择,后者的方法,您可跟窗体中的图像在同一页上已打印的内容。
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息