在 Windows PrintForm Visual Basic 方法提供了一种可以打印与客户区的窗体方法。 但是,重新实现不允许您控制大小或在打印的输出的比例,或打印该非客户端区域 (标题和边框) 窗体。
代码下例打印整个的窗体中使用 Windows API 函数,并提供了一种方法控制输出的大小。 此方法也可用于打印仅与客户区特定的大小与控件来使文本或要为窗体的图像在同一页上打印的其他图形在打印表单的位置。 该方法是同样适用于打印项目中的所有窗体。
注意: 此示例将无法正常工作在 PostScript 打印机。 为了使本示例正常工作,打印机都必须使用标准的非 PostScript 激光打印机配置 (如 PCL / HP)。
将 Windows API 函数 bitblt,StretchBlt、 CreateCompatibleDC、 DeleteDC、 SelectObject 和转义允许更好地的位置和大小打印出的表单控制比重新实现方法。 在由两部分组成的进程中整个窗体的图像由一个不可见的图片使用 bitblt 捕获,并转换永久位图使用 AutoRedraw 属性。 然后将图片打印使用打印图片控件 (其边框为通过查询 Microsoft 知识库中以下单词找到一单独文章) 的方法:
此方法效果最大化的窗体以及任何较小的窗体。 使用 GetSystemMetrics 允许处理不同的窗口的边框样式通过查询大小以像素为单位) 的 Windows 标准边框的视频驱动程序传递给它的常规过程。
下面此示例需要一个表单与不可见的图片控件。
示例
<script type="text/javascript"></script>
- 将以下代码添加到新的项目中窗体的 General Declarations 级别中:
注意: 下面的所有声明语句必须都是在一行每个。
DefInt A-ZDeclare 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 = 4 Const SM_CXBORDER = 5 Const SM_CYBORDER = 6 Const SM_CXDLGFRAME = 7 Const SM_CYDLGFRAME = 8 Const SM_CXFRAME = 32 Const SM_CYFRAME = 33 Const TWIPS = 1 Const PIXEL = 3 Const NILL = 0& Const SRCCOPY = &HCC0020 Const NEWFRAME = 1 Dim 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/FormName Property for the above objects instead of the Name property.)
- 将以下代码添加到 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, 2535 Picture1.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), , BF Picture1.AutoRedraw = 0 ' Toggle off. ' Make sure the temporary workspace picture is invisible. Picture2.visible = 0 End Sub
- 将以下代码添加到窗体的常规过程级别中:
Sub FormPrint (localname As Form) ' Display cross. screen.MousePointer = 2 ' Calculate ratio between ScaleMode twips and ScaleMode pixel. localname.ScaleMode = PIXEL ModeRatio = localname.height / localname.ScaleHeight localname.ScaleMode = TWIPS XOffset = (localname.width - localname.ScaleWidth) / ModeRatio YOffset = (localname.height - localname.ScaleHeight) / ModeRatio CapSize% = 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.BorderStyle Case 0 ' None. XOffset = 0 YOffset = 0 Case 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.hDC X% = 0: Y% = 0 nWidth% = Picture2.ScaleWidth nHeight% = 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.hDC XSrc% = -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 = PIXEL Printer.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 = 11 hMemoryDC% = 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 following is 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.EndDoc ApiError% = Escape(Printer.hDC, NEWFRAME, 0, NILL, NILL) ' Reset MousePointer to default. Screen.MousePointer = 1 End Sub
- 将以下代码添加到 Double_Click 事件:
Sub Form_DblClick () FormPrint Form1 End Sub
- 保存项目后, 运行此示例。
双击该窗体来调用该 FormPrint 过程。 将打印作为参数传递到 FormPrint 任何窗体。 bitblt 会将到将图片控件的图像,然后 StretchBlt 将其传送到该打印机将打印 bitblt 已传输的图像的 DC。
(可选),可以使用 StretchBlt 打印之前将放文本或图片 (Form1.Picture2) 中的图形,或直接打印到使用 Printer.Print 或 printer.line 页。 如果您通过调整 StretchBlt 的第二个和第三个参数选择,后者的方法,您可跟窗体中的图像在同一页上已打印的内容。