VB 控制picturebox内容适应A4纸张大小,横向纵向调整,适合打印输出和图形输出一致!
发布网友
发布时间:2022-04-23 00:45
我来回答
共3个回答
热心网友
时间:2023-10-09 12:39
设置打印机的默认纸张是a4,在vb中就可以通过printer对象可以取得a4纸张大小,可以记录下来,供以后使用;
下边是我写的一段利用picturebox做的打印预览程序,仅供参考!
Set flg = Oform.mOform.flg
iNowPage = 1
iRows = flg.Rows - 1
'1 获取用户的设置
sngMargin(0) = CDbl(Oform.txtM(0)) * mCcmToTwips '将厘米换算成缇
sngMargin(1) = CDbl(Oform.txtM(1)) * mCcmToTwips '将厘米换算成缇
sngFontSize = gaUserfont(1).Size * mCPointToTwips * mntimes
sngTblM = gaUserfont(0).Size * mntimes * mCPointToTwips + sngMargin(0) '页眉高度
s_getColIndex '取出全部打印列(flg中的col)
s_GetMergeCols '取出合并列的合并次数
s_CalcColsWidth '计算列的打印宽度
'2 判断纸张大小是否足以打下报表
'If sngTblWid > Printer.ScaleWidth Then
If sngTblWid > Printer.ScaleWidth Then
Screen.MousePointer = 0
If MsgBox("部分内容已超出页面范围。是否继续?", vbQuestion + vbYesNo, "万能打印") = vbNo Then Exit Sub
End If
'3 计算每页最多可打印的行数((页高—上下空白)/行高—1)
iMaxRecsPerPage = Int((Printer.ScaleHeight - sngTblM - sngMargin(1)) / (1.5 * sngFontSize) - 1)
If CInt(iRows / iMaxRecsPerPage) - Int(iRows / iMaxRecsPerPage) = 1 Then
iPages = CInt(iRows / iMaxRecsPerPage)
Else
iPages = CInt(iRows / iMaxRecsPerPage) + 1
End If
UpDown1.Max = iPages
'4.开始打印
With Pic1
'.ScaleHeight = Printer.ScaleHeight
' .ScaleWidth = Printer.ScaleWidth
Frame1.Width = Me.ScaleWidth + 500
.Height = Printer.ScaleHeight * mntimes
.Width = Printer.ScaleWidth * mntimes
.ScaleHeight = Printer.ScaleHeight
.ScaleWidth = Printer.ScaleWidth
.Left = 0
.Top = 600
Hsb.Min = 0
If .Width <= Hsb.Width Then
Hsb.Visible = False
Else
Hsb.Visible = True
Hsb.Max = .Width - Hsb.Width
Hsb.Value = (.Width - Hsb.Width) / 2
End If
Vsb.Min = 540
If .Height + 600 <= Me.ScaleHeight Then
Vsb.Visible = False
Else
Vsb.Visible = True
Vsb.Max = .Height - Vsb.Height
End If
' Me.Height = Me.ScaleHeight * 0.75
' Me.Width = Me.ScaleWidth * 0.75
For iC = 1 To Val(Oform.txtCopies) '对份数循环
For iNowPage = miPages To iPages '对页范围循环
'4-1 打印表头
' 不能用Set Printer.Font = gaUserfont(0),打印机字体可能被某些隐含过程改变,从而影响到userfont
With .Font
.name = gaUserfont(0).name
.Size = gaUserfont(0).Size * mntimes
.Bold = gaUserfont(0).Bold
.Italic = gaUserfont(0).Italic
.Underline = gaUserfont(0).Underline
.Strikethrough = False
End With
sTmp = Oform.txtTitle & "(" & Trim(Oform.mOform.Tag) & ")"
.CurrentX = sngLMargin + (sngTblWid - Pic1.TextWidth(sTmp)) / 2
.CurrentY = sngMargin(0) - sngFontSize * 2
Pic1.Print sTmp
'4-2 在表格的右上部打印"打印日期"
With .Font
.name = gaUserfont(1).name
.Size = gaUserfont(1).Size * mntimes
.Bold = gaUserfont(1).Bold
.Italic = gaUserfont(1).Italic
.Underline = gaUserfont(1).Underline
.Strikethrough = False
End With
sStr = "打印时间: " & Now()
.CurrentX = sngLMargin + sngTblWid - Pic1.TextWidth(sStr) - 200
.CurrentY = sngTblM - sngFontSize * 1.5
Pic1.Print sStr
'在页底边中间打印页码
.Font.Size = gaUserfont(1).Size * mntimes
.CurrentX = .ScaleWidth / 2
.CurrentY = .ScaleHeight - sngMargin(1)
Pic1.Print "第" & CStr(iNowPage) & "页"
'4-3 打印列标题
.DrawWidth = 1
'4-3-1划上边线
.CurrentX = sngLMargin
.CurrentY = sngTblM - sngFontSize / 4
Pic1.Line -Step(sngTblWid, 0) '划上边线
'4-3-2划左边线
.CurrentX = sngLMargin
.CurrentY = sngTblM - sngFontSize / 4
Pic1.Line -Step(0, 1.5 * sngFontSize) '划左边线
tmpX = sngLMargin
tmpY = sngTblM
.CurrentY = tmpY
With .Font
.name = gaUserfont(1).name
.Size = gaUserfont(1).Size * mntimes
.Bold = gaUserfont(1).Bold
.Italic = gaUserfont(1).Italic
.Underline = gaUserfont(1).Underline
.Strikethrough = False
End With
For iA = 0 To miCols - 1
.CurrentY = tmpY
If miMergeTimes(iA) = 1 Then
' If miAlignment(iA) = flexAlignLeftCenter Then
' .CurrentX = tmpX + sngFontSize / 4
' Else
' .CurrentX = tmpX + sngColWid(iA) - Pic1.TextWidth(Oform.lstChosenFlds.List(iA)) - sngFontSize / 4
' If .CurrentX < tmpX Then .CurrentX = tmpX + sngFontSize / 4
' End If
Else
.CurrentX = tmpX + (sngColWid(iA) * miMergeTimes(iA) - Pic1.TextWidth(Oform.lstChosenFlds.List(iA))) / 2 - sngFontSize / 4
End If
'输出数据
sStr = Trim(Oform.lstChosenFlds.List(iA))
'截取尾部的数字
If IsNumeric(Right(sStr, 1)) Then sStr = Left(sStr, Len(sStr) - 1)
If IsNumeric(Right(sStr, 1)) Then sStr = Left(sStr, Len(sStr) - 1)
.CurrentX = tmpX + (sngColWid(iA) * miMergeTimes(iA) - Printer.TextWidth(sStr)) / 2 - sngFontSize / 4
If .CurrentX < tmpX Then .CurrentX = tmpX
Pic1.Print sStr
'确定位置
.CurrentX = tmpX
.CurrentY = tmpY + sngFontSize * 1.25
'划下边线
Pic1.Line -Step(sngColWid(iA) * miMergeTimes(iA), 0)
tmpX = tmpX + sngColWid(iA) * miMergeTimes(iA)
.CurrentX = tmpX
.CurrentY = tmpY - sngFontSize / 4
'划右边线
Pic1.Line -Step(0, sngFontSize * 1.5)
Next
'4-4打印数据
tmpY = tmpY + sngFontSize * 1.25
tmpX = sngLMargin
.CurrentX = tmpX
.CurrentY = tmpY
iFRow = (iNowPage - 1) * iMaxRecsPerPage + 1 '第一行的行数
iLRow = iNowPage * iMaxRecsPerPage '最后一行的行数
'如果最后行数大于最大行取最大行为最后行
iLRow = IIf(iLRow > iRows, iRows, iLRow)
For iNowRow = iFRow To iLRow
tmpX = sngLMargin
.CurrentX = tmpX
.CurrentY = tmpY
For iCycle = 0 To miCols - 1
iNowcol = miColIndex(iCycle)
.CurrentX = tmpX
.CurrentY = tmpY
For iMerge = 1 To miMergeTimes(iCycle)
'划左边线
.CurrentX = tmpX
.CurrentY = tmpY
Pic1.Line -Step(0, 1.5 * sngFontSize)
'打印数据
If flg.ColAlignment(iNowcol) = flexAlignRightCenter Then
.CurrentX = tmpX + sngColWid(iCycle) * iMerge - .TextWidth(flg.TextMatrix(iNowRow, iNowcol))
If .CurrentX < tmpX Then .CurrentX = tmpX
Else
.CurrentX = tmpX + sngFontSize / 4
End If
.CurrentY = tmpY + sngFontSize * 0.25
sStr = flg.TextMatrix(iNowRow, iNowcol)
If IsNumeric(sStr) Then If Left(sStr, 1) = "." Then sStr = "0" & sStr
If sStr = "0" Then sStr = ""
If iNowRow < 4 Then
If IsNumeric(sStr) Then
Pic1.Print sStr
Else
If sStr <> sTmp Then Pic1.Print sStr
End If
Else
Pic1.Print sStr
End If
sTmp = sStr
'划下边线
.CurrentY = tmpY + sngFontSize * 1.5
.CurrentX = tmpX
Pic1.Line -Step(sngColWid(iCycle), 0)
'划右边线
tmpX = tmpX + sngColWid(iCycle)
.CurrentX = tmpX
.CurrentY = tmpY
'划右边线
Pic1.Line -Step(0, sngFontSize * 1.5)
iNowcol = iNowcol + 1
Next
Next
tmpY = tmpY + sngFontSize * 1.5
.CurrentY = tmpY
Next
.Top = 600
Exit For
Next
Exit For
Next
End With
热心网友
时间:2023-10-09 12:39
Private Sub Command1_Click()
Dim i As Integer
i = Me.ScaleMode
Me.ScaleMode = 7 '厘米作为单位
Picture1.Move 0, 0, 21, 29.7 'A4纸的尺寸
Me.ScaleMode = i
End Sub
热心网友
时间:2023-10-09 12:39
下载csximage.ocx