vb中使用Xtreme Command Bars ActiveX Control 的IPrintView接口绘制图形
应用场景
Xtreme Command Bars ActiveX Control中的PrintView,可以通过API绘制任意图形和文字,Xtreme Command Bars ActiveX Control的PrintView只支持
rtf格式和XAML两种输入基本输入源的。
CreateMarkupPrintView Creates an IPrintView object from the supplied XAML Markup string.
CreateRichEditPrintView Creates an IPrintView object from the supplied RTF string.
感谢 : Soyokaze在 http://topic.csdn.net/u/20081016/08/e5189330-4fec-4287-9009-47e681723ea3.html 里的代码
无名氏 在 http://zj1.51.net/book/show.php?type=vbtip&id=1099050675里的代码
如下代码,实现剪贴板里的图片的PrintView控件输出
1.图像输入
Public Function CopyEntirePicture(ByRef objFrom As Object) As Boolean
Dim lhDC As Long
Dim lhBMP As Long
Dim lhBMPOld As Long
'在内存中建立一个指向我们将要复制对象的DC:
lhDC = CreateCompatibleDC(objFrom.hdc)
If (lhDC <> 0) Then
'建立一张指向将要复制对象的位图:
lhBMP = CreateCompatibleBitmap(objFrom.hdc, objFrom.ScaleWidth \ Screen.TwipsPerPixelX, objFrom.ScaleHeight \ Screen.TwipsPerPixelY)
If (lhBMP <> 0) Then
'把位图选入我们刚才建立的DC中,并贮存原先在那里的老位图:
lhBMPOld = SelectObject(lhDC, lhBMP)
'把objFrom的内容复制到建立的位图里:
BitBlt lhDC, 0, 0, objFrom.ScaleWidth \ Screen.TwipsPerPixelX, objFrom.ScaleHeight \ Screen.TwipsPerPixelY, objFrom.hdc, 0, 0, SRCCOPY
'恢复DC中的内容:
SelectObject lhDC, lhBMPOld
'现在把位图装入剪贴板:
EmptyClipboard
OpenClipboard 0
SetClipboardData CF_BITMAP, lhBMP
CloseClipboard
'我们在这里不用删除建立的位图——
'它现在属于剪贴板,当剪贴板变化时,Windows将为我们删除它。
End If
'清除刚才建立的DC:
DeleteObject lhDC
End If
End Function
2.图像输出
Implements IPrintView
Option Explicit
Private Declare Function GetDIBits Lib "gdi32" ( _
ByVal aHDC As Long, _
ByVal hBitmap As Long, _
ByVal nStartScan As Long, _
ByVal nNumScans As Long, _
lpBits As Any, _
lpBI As BITMAPINFO, _
ByVal wUsage As Long) _
As Long
Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Const BI_RGB = 0&
Private Const BI_RLE4 = 2&
Private Const BI_RLE8 = 1&
Private Const BI_BITFIELDS = 3&
Private Const DIB_RGB_COLORS = 0
Private Declare Function StretchDIBits Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal wSrcWidth As Long, ByVal wSrcHeight As Long, lpBits As Any, lpBitsInfo As BITMAPINFO, ByVal wUsage As Long, ByVal dwRop As Long) As Long
Private Declare Function GetIconInfo Lib "user32" (ByVal hIcon As Long, piconinfo As ICONINFO) As Long
Private Type ICONINFO
fIcon As Long
xHotspot As Long
yHotspot As Long
hbmMask As Long
hbmColor As Long
End Type
Private Declare Function GetObjectType Lib "gdi32" (ByVal hgdiobj As Long) As Long
Private Const OBJ_BITMAP = 7
Private Const OBJ_BRUSH = 2
Private Const OBJ_FONT = 6
Private Const OBJ_PAL = 5
Private Const OBJ_PEN = 1
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Type Size
cx As Long
cy As Long
End Type
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const DT_CENTER = &H1
Private Const DT_SINGLELINE = &H20
Private Sub IPrintView_BeginPrinting(ByVal hdc As Long, ByVal hAttribDC As Long, ByVal PrintInfo As XtremeCommandBars.PrintInfo)
End Sub
Private Sub IPrintView_EndPrinting(ByVal hdc As Long, ByVal hAttribDC As Long, ByVal PrintInfo As XtremeCommandBars.PrintInfo)
End Sub
Private Sub IPrintView_PrepareDC(ByVal hdc As Long, ByVal hAttribDC As Long, ByVal PrintInfo As XtremeCommandBars.PrintInfo)
End Sub
Private Sub IPrintView_PreparePrinting(ByVal PrintInfo As XtremeCommandBars.PrintInfo)
PrintInfo.MaxPage = 1
End Sub
Private Sub IPrintView_PrintPage(ByVal hdc As Long, ByVal hAttribDC As Long, ByVal PrintInfo As XtremeCommandBars.PrintInfo)
If (PrintInfo.CurrentPage = 1) Then
Dim r As RECT
r.Left = PrintInfo.PrintRectLeft
r.Top = PrintInfo.PrintRectTop
r.Right = PrintInfo.PrintRectRight
r.Bottom = PrintInfo.PrintRectBottom
Dim MyPic As Picture '定义Picture对象
Set MyPic = Clipboard.GetData(vbCFBitmap)
Dim tBmpInfo As BITMAPINFO
Dim tSize As Size
Dim hBmp As Long
Dim byBits() As Byte
Dim nbPerLine As Long
hBmp = MyPic.Handle
Call GetImageSize(hBmp, tSize)
'取得 Bmp 像素位
With tBmpInfo.bmiHeader
.biSize = Len(tBmpInfo.bmiHeader)
.biWidth = tSize.cx
.biHeight = tSize.cy
.biPlanes = 1
.biBitCount = 24
.biCompression = BI_RGB
End With
nbPerLine = (tSize.cx * 3 + 3) And &HFFFFFFFC
ReDim byBits(nbPerLine - 1, tSize.cy - 1) As Byte
Call GetDIBits(hdc, hBmp, 0, tSize.cy, byBits(0, 0), tBmpInfo, DIB_RGB_COLORS)
Call StretchDIBits(hdc, 0, 0, tSize.cx, tSize.cy, 0, 0, tSize.cx, tSize.cy, byBits(0, 0), tBmpInfo, DIB_RGB_COLORS, SRCCOPY)
End If
End Sub
Private Sub GetImageSize(ByVal hObject As Long, tSize As Size)
Dim tBMP As BITMAP
Dim tIcon As ICONINFO
If GetObjectType(hObject) = OBJ_BITMAP Then
Call GetObject(hObject, LenB(tBMP), tBMP)
ElseIf GetIconInfo(hObject, tIcon) Then
Call GetObject(tIcon.hbmMask, LenB(tBMP), tBMP)
End If
tSize.cx = tBMP.bmWidth
tSize.cy = tBMP.bmHeight
End Sub