|
-
Mar 5th, 2018, 11:27 PM
#20
Fanatic Member
Re: [VB6] UPrinter - Unicode Printer Class
 Originally Posted by xxdoc123
Code:
'add by xxdoc'
Private Declare Function GdipGetImageHeight Lib "gdiplus.dll" (ByVal Image As Long, Height As Long) As Long
Private Declare Function GdipGetImageWidth Lib "gdiplus.dll" (ByVal Image As Long, Width As Long) As Long
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type RECTF
nLeft As Single
nTop As Single
nWidth As Single
nHeight As Single
End Type
Private Type COLORMATRIX
m(0 To 4, 0 To 4) As Single
End Type
Private Enum ColorAdjustType
ColorAdjustTypeDefault = 0
ColorAdjustTypeBitmap = 1
ColorAdjustTypeBrush = 2
ColorAdjustTypePen = 3
ColorAdjustTypeText = 4
ColorAdjustTypeCount = 5
ColorAdjustTypeAny = 6
End Enum
Private Enum ColorMatrixFlags
ColorMatrixFlagsDefault = 0
ColorMatrixFlagsSkipGrays = 1
ColorMatrixFlagsAltGray = 2
End Enum
Private Const UnitPixel As Long = &H2&
Private Declare Function GdipGetImageBounds _
Lib "gdiplus.dll" (ByVal nImage As Long, _
srcRect As RECTF, _
srcUnit As Long) As Long
Private Declare Function GdipCreateFromHDC _
Lib "Gdiplus" (ByVal hdc As Long, _
hGraphics As Long) As Long
Private Declare Function GdipCreateImageAttributes _
Lib "Gdiplus" (ByRef imageattr As Long) As Long
Private Declare Function GdipSetImageAttributesColorMatrix _
Lib "Gdiplus" (ByVal imageattr As Long, _
ByVal ColorAdjust As ColorAdjustType, _
ByVal EnableFlag As Boolean, _
ByRef MatrixColor As COLORMATRIX, _
ByRef MatrixGray As COLORMATRIX, _
ByVal flags As ColorMatrixFlags) As Long
Private Declare Function GdipDrawImageRectRectI _
Lib "Gdiplus" (ByVal hGraphics As Long, _
ByVal hImage As Long, _
ByVal dstx As Long, _
ByVal dsty As Long, _
ByVal dstwidth As Long, _
ByVal dstheight As Long, _
ByVal srcx As Long, _
ByVal srcy As Long, _
ByVal srcwidth As Long, _
ByVal srcheight As Long, _
ByVal srcUnit As Long, _
Optional ByVal imageAttributes As Long = 0, _
Optional ByVal callback As Long = 0, _
Optional ByVal callbackData As Long = 0) As Long
Private Declare Function GdipDisposeImageAttributes _
Lib "Gdiplus" (ByVal imageattr As Long) As Long
Private Declare Function GdipDeleteGraphics _
Lib "Gdiplus" (ByVal hGraphics As Long) As Long
Private Declare Function GdiplusStartup Lib "gdiplus.dll" (Token As Long, gdipInput As GdiplusStartupInput, GdiplusStartupOutput As Long) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus.dll" (ByVal Token As Long)
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "gdiplus.dll" (ByVal hbm As Long, ByVal hpal As Long, ByRef pbitmap As Long) As Long
'Private Declare Function GdipCreateHBITMAPFromBitmap Lib "gdiplus.dll" (ByVal hbm As Long, ByVal hpal As Long, ByRef pbitmap As Long) As Long
Private Declare Function GdipCreateBitmapFromGraphics Lib "Gdiplus" (ByVal Width As Long, ByVal Height As Long, ByVal Graphics As Long, bitmap As Long) As Long
'add by xxdoc
Code:
Public Function RenderTo2(ByVal lhDC As Long, _
ByVal dstx As Long, _
ByVal dsty As Long, _
Optional ByVal dstwidth As Long, _
Optional ByVal dstheight As Long, _
Optional ByVal Alpha As Byte = 100) As Boolean
Dim PrevMM As Long
Dim hGraphics As Long
Dim hAttributes As Long
Dim dBrightness As Double
Dim dContrast As Double
Dim dAlpha As Double
Dim tMatrixColor As COLORMATRIX
Dim tMatrixGray As COLORMATRIX
Dim TR As RECTF
Dim c_lWidth As Long
Dim c_lHeight As Long
Dim printer2screenDPI As Single
If hBmp = WIN32_NULL Then Exit Function
printer2screenDPI = (GetDeviceCaps(lhDC, LOGPIXELSX) / GetDeviceCaps(GetDC(0), LOGPIXELSX))
' Call GdipGetImageBounds(hBmp, TR, UnitPixel)
' c_lWidth = TR.nWidth
' c_lHeight = TR.nHeight
' If dstWidth = 0 Then dstWidth = c_lWidth
' If dstHeight = 0 Then dstHeight = c_lHeight
' If srcWidth = 0 Then srcWidth = c_lWidth
' If srcHeight = 0 Then srcHeight = c_lHeight
If dstwidth = 0 Then dstwidth = Me.Width
If dstheight = 0 Then dstheight = Me.Height
dAlpha = ValidateValue(Alpha)
Dim bitmap As Long, bmW As Long, bmH As Long
'
GdipCreateBitmapFromHBITMAP hBmp, 0, bitmap
GdipGetImageWidth bitmap, bmW
GdipGetImageHeight bitmap, bmH
PrevMM = SetMapMode(lhDC, MM_TEXT)
If GdipCreateFromHDC(lhDC, hGraphics) = 0 Then
'sets the unit of measure for this Graphics object from UnitDisplay to UnitPixel
GdipSetPageUnit hGraphics, UnitPixel
GdipSetSmoothingMode hGraphics, SmoothingModeAntiAlias
With tMatrixColor
.m(0, 0) = 1
.m(1, 1) = 1
.m(2, 2) = 1
.m(4, 4) = 1
If Not dAlpha = 100 Then
.m(3, 3) = dAlpha
End If
End With
If GdipCreateImageAttributes(hAttributes) = 0 Then
If GdipSetImageAttributesColorMatrix(hAttributes, ColorAdjustTypeDefault, True, tMatrixColor, tMatrixGray, ColorMatrixFlagsDefault) = 0 Then
If GdipDrawImageRectRectI(hGraphics, bitmap, dstx / printer2screenDPI, dsty / printer2screenDPI, dstwidth / printer2screenDPI + 6, dstheight / printer2screenDPI + 6, 0, 0, bmW, bmH, UnitPixel, hAttributes) = 0 Then
RenderTo2 = True
End If
End If
Call GdipDisposeImageAttributes(hAttributes)
End If
Call GdipDeleteGraphics(hGraphics)
End If
SetMapMode lhDC, PrevMM
End Function
Not perfect,but can used in my printer and pdf printer well.
Private Declare Function GdipSetPageUnit Lib "gdiplus" _
(ByVal Graphics As Long, ByVal Unit As Long) As Long
GdipSetPageUnit hGraphics, UnitPixel
http://www.vbforums.com/showthread.p...Print-Preview)
UPrinter3.6.zip
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|