1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
| Private Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End Type
Private Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
#If Win32 Then
Private Const RASTERCAPS As Long = 38
Private Const RC_PALETTE As Long = &H100
Private Const SIZEPALETTE As Long = 104
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function CreateCompatibleDC Lib "GDI32" ( _
ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "GDI32" ( _
ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long _
) As Long
Private Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, _
ByVal iCapability As Long) As Long
Private Declare Function GetSystemPaletteEntries Lib "GDI32" ( _
ByVal hDC As Long, ByVal wStartIndex As Long, _
ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
Private Declare Function CreatePalette Lib "GDI32" ( _
lpLogPalette As LOGPALETTE) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, _
ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "GDI32" (ByVal hDCDest As Long, _
ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal XSrc As Long, _
ByVal YSrc As Long, ByVal dwRop As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As _
Long
Private Declare Function GetForegroundWindow Lib "USER32" () As Long
Private Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, _
ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "GDI32" ( _
ByVal hDC As Long) As Long
Private Declare Function GetWindowDC Lib "USER32" ( _
ByVal hWnd As Long) As Long
Private Declare Function GetDC Lib "USER32" ( _
ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "USER32" ( _
ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, _
ByVal hDC As Long) As Long
Private Declare Function GetDesktopWindow Lib "USER32" () As Long
Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" ( _
PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#ElseIf Win16 Then
Private Const RASTERCAPS As Integer = 38
Private Const RC_PALETTE As Integer = &H100
Private Const SIZEPALETTE As Integer = 104
Private Type RECT
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type
Private Declare Function CreateCompatibleDC Lib "GDI" ( _
ByVal hDC As Integer) As Integer
Private Declare Function CreateCompatibleBitmap Lib "GDI" ( _
ByVal hDC As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer) As Integer
Private Declare Function GetDeviceCaps Lib "GDI" ( _
ByVal hDC As Integer, ByVal iCapability As Integer) As Integer
Private Declare Function GetSystemPaletteEntries Lib "GDI" ( _
ByVal hDC As Integer, ByVal wStartIndex As Integer, _
ByVal wNumEntries As Integer, _
lpPaletteEntries As PALETTEENTRY) As Integer
Private Declare Function CreatePalette Lib "GDI" ( _
lpLogPalette As LOGPALETTE) As Integer
Private Declare Function SelectObject Lib "GDI" (ByVal hDC As Integer, _
ByVal hObject As Integer) As Integer
Private Declare Function BitBlt Lib "GDI" (ByVal hDCDest As Integer, _
ByVal XDest As Integer, ByVal YDest As Integer, _
ByVal nWidth As Integer, ByVal nHeight As Integer, _
ByVal hDCSrc As Integer, ByVal XSrc As Integer, _
ByVal YSrc As Integer, ByVal dwRop As Long) As Integer
Private Declare Function DeleteDC Lib "GDI" ( _
ByVal hDC As Integer) As Integer
Private Declare Function GetForegroundWindow Lib "USER" _
Alias "GetActiveWindow" () As Integer
Private Declare Function SelectPalette Lib "USER" ( _
ByVal hDC As Integer, ByVal hPalette As Integer, ByVal _
bForceBackground As Integer) As Integer
Private Declare Function RealizePalette Lib "USER" ( _
ByVal hDC As Integer) As Integer
Private Declare Function GetWindowDC Lib "USER" ( _
ByVal hWnd As Integer) As Integer
Private Declare Function GetDC Lib "USER" ( _
ByVal hWnd As Integer) As Integer
Private Declare Function GetWindowRect Lib "USER" ( _
ByVal hWnd As Integer, lpRect As RECT) As Integer
Private Declare Function ReleaseDC Lib "USER" (ByVal hWnd As Integer, _
ByVal hDC As Integer) As Integer
Private Declare Function GetDesktopWindow Lib "USER" () As Integer
Private Type PicBmp
Size As Integer
Type As Integer
hBmp As Integer
hPal As Integer
Reserved As Integer
End Type
Private Declare Function OleCreatePictureIndirect Lib "oc25.dll" ( _
PictDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Integer, IPic As IPicture) As Integer
#End If
' PrintPictureToFitPage - Prints a Picture object as big as possible.
' Prn - Destination Printer object.
' Pic - Source Picture object.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture)
Const vbHiMetric As Integer = 8
Dim PicRatio As Double
Dim PrnWidth As Double
Dim PrnHeight As Double
Dim PrnRatio As Double
Dim PrnPicWidth As Double
Dim PrnPicHeight As Double
'On Error GoTo PrintError
' Determine if picture should be printed in landscape or portrait and
' set the orientation
If Pic.Height >= Pic.Width Then
Prn.Orientation = vbPRORPortrait ' Taller than wide
Else
Prn.Orientation = vbPRORLandscape ' Wider than tall
End If
' Calculate device independent Width to Height ratio for picture
PicRatio = Pic.Width / Pic.Height
' Calculate the dimensions of the printable area in HiMetric
PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric)
PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric)
' Calculate device independent Width to Height ratio for printer
PrnRatio = PrnWidth / PrnHeight
' Scale the output to the printable area
If PicRatio >= PrnRatio Then
' Scale picture to fit full width of printable area
PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, _
Prn.ScaleMode)
Else
' Scale picture to fit full height of printable area
PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, _
Prn.ScaleMode)
End If
' Print the picture using the PaintPicture method
'Modif pour ajouter 10% de marge de chaque côté
' Prn.PaintPicture Pic, 0, 0, PrnPicWidth, PrnPicHeight
Prn.PaintPicture Pic, PrnPicWidth * 0.1, PrnPicHeight * 0.1, PrnPicWidth * 0.8, PrnPicHeight * 0.8
'On Error GoTo 0
'Exit Sub
'PrintError:
'If Err = 482 Then 'Erreur d'imprimante
' MsgBox "Printer error, please check connection with your printer", vbExclamation, "Error"
' Exit Sub
'Else
' MsgBox Err.Description, vbExclamation, "Error"
'End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CreateBitmapPicture - Creates a bitmap type Picture object from a bitmap and palette.
' hBmp - Handle to a bitmap.
' hPal - Handle to a Palette. Can be null if the bitmap doesn't use a palette.
' Returns - Returns a Picture object containing the bitmap.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#If Win32 Then
Public Function CreateBitmapPicture(ByVal hBmp As Long, _
ByVal hPal As Long) As Picture
Dim r As Long
#ElseIf Win16 Then
Public Function CreateBitmapPicture(ByVal hBmp As Integer, _
ByVal hPal As Integer) As Picture
Dim r As Integer
#End If
Dim Pic As PicBmp
' IPicture requires a reference to "Standard OLE Types"
Dim IPic As IPicture
Dim IID_IDispatch As GUID
' Fill in with IDispatch Interface ID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
' Fill Pic with necessary parts
With Pic
.Size = Len(Pic) ' Length of structure
.Type = vbPicTypeBitmap ' Type of Picture (bitmap)
.hBmp = hBmp ' Handle to bitmap
.hPal = hPal ' Handle to palette (may be null)
End With
' Create Picture object
r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
' Return the new Picture object
Set CreateBitmapPicture = IPic
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CaptureWindow - Captures any portion of a window.
' hWndSrc - Handle to the window to be captured.
' Client - If True, CaptureWindow captures from the client area of the window.
' If False, CaptureWindow captures from the entire window.
' LeftSrc, TopSrc, WidthSrc, HeightSrc - Specify the portion of the window to capture.
' Dimensions need to be specified in pixels.
' Returns - Returns a Picture object containing a bitmap of the specified portion of the window that was captured.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#If Win32 Then
Public Function CaptureWindow(ByVal hWndSrc As Long, _
ByVal Client As Boolean, ByVal LeftSrc As Long, _
ByVal TopSrc As Long, ByVal WidthSrc As Long, _
ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
#ElseIf Win16 Then
Public Function CaptureWindow(ByVal hWndSrc As Integer, _
ByVal Client As Boolean, ByVal LeftSrc As Integer, _
ByVal TopSrc As Integer, ByVal WidthSrc As Long, _
ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Integer
Dim hBmp As Integer
Dim hBmpPrev As Integer
Dim r As Integer
Dim hDCSrc As Integer
Dim hPal As Integer
Dim hPalPrev As Integer
Dim RasterCapsScrn As Integer
Dim HasPaletteScrn As Integer
Dim PaletteSizeScrn As Integer
#End If
Dim LogPal As LOGPALETTE
' Depending on the value of Client get the proper device context
If Client Then
hDCSrc = GetDC(hWndSrc) ' Get device context for client area
Else
hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire window
End If
' Create a memory device context for the copy process
hDCMemory = CreateCompatibleDC(hDCSrc)
' Create a bitmap and place it in the memory DC
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
' Get screen properties
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster capabilities
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette support
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of palette
' If the screen has a palette, make a copy and realize it
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
' Create a copy of the system palette
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
' Select the new palette into the memory DC and realize it
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If
' Copy the on-screen image into the memory DC
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
LeftSrc, TopSrc, vbSrcCopy)
' Remove the new copy of the on-screen image
hBmp = SelectObject(hDCMemory, hBmpPrev)
' If the screen has a palette, get back the palette that was selected
' in previously
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
' Release the device context resources back to the system
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
' Call CreateBitmapPicture to create a picture object from the bitmap
' and palette handles. Then return the resulting picture object.
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CaptureScreen - Captures the entire screen.
' Returns - Returns a Picture object containing a bitmap of the screen.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function CaptureScreen() As Picture
#If Win32 Then
Dim hWndScreen As Long
#ElseIf Win16 Then
Dim hWndScreen As Integer
#End If
' Get a handle to the desktop window
hWndScreen = GetDesktopWindow()
' Call CaptureWindow to capture the entire desktop, give the handle and
' return the resulting Picture object
Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, _
Screen.Width \ Screen.TwipsPerPixelX, _
Screen.Height \ Screen.TwipsPerPixelY)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CaptureForm - Captures an entire form including title bar and border.
' frmSrc - The Form object to capture.
' Returns - Returns a Picture object containing a bitmap of the entire form.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function CaptureForm(frmSrc As Form) As Picture
' Call CaptureWindow to capture the entire form given its window
' handle and then return the resulting Picture object
Set CaptureForm = CaptureWindow(frmSrc.hWnd, False, 0, 0, _
frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), _
frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CaptureClient - Captures the client area of a form.
' frmSrc - The Form object to capture.
' Returns- Returns a Picture object containing a bitmap of the form's client area.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function CaptureClient(frmSrc As Form) As Picture
' Call CaptureWindow to capture the client area of the form given its
' window handle and return the resulting Picture object
Set CaptureClient = CaptureWindow(frmSrc.hWnd, True, 0, 0, _
frmSrc.ScaleX(frmSrc.Frame(2).Width, frmSrc.ScaleMode, vbPixels), _
frmSrc.ScaleY(frmSrc.Frame(2).Height, frmSrc.ScaleMode, vbPixels))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CaptureActiveWindow - Captures the currently active window on the screen.
' Returns - Returns a Picture object containing a bitmap of the active window.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function CaptureActiveWindow() As Picture
#If Win32 Then
Dim hWndActive As Long
Dim r As Long
#ElseIf Win16 Then
Dim hWndActive As Integer
Dim r As Integer
#End If
Dim RectActive As RECT
' Get a handle to the active/foreground window
hWndActive = GetForegroundWindow()
' Get the dimensions of the window
r = GetWindowRect(hWndActive, RectActive)
' Call CaptureWindow to capture the active window given its handle and
' return the Resulting Picture object
Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, _
RectActive.Right - RectActive.Left, _
RectActive.Bottom - RectActive.Top)
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |