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
| Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal Hdc As Long) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal Hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal Hdc As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal Hdc As Long, ByVal hObject As Long) As Long
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Declare Function GetPixel Lib "gdi32" (ByVal Hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function SetPixel Lib "gdi32" (ByVal Hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal Hdc As Long) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
BOTTOM As Long
End Type
Const SRCCOPY = &HCC0020
Public chemin_image As String
Sub captur_USERFORM(usf)
Dim ActiveHwnd As Long
Dim DeskHwnd As Long
Dim ForegroundHwnd As Long
Dim Hdc As Long
Dim hdcMem As Long
Dim RECT As RECT
Dim action As Long
Dim fwidth As Long, fheight As Long
Dim hBitmap As Long
'---------------------------------------------------
' determination du handle de la fentre active
'---------------------------------------------------
DeskHwnd = GetDesktopWindow()
ActiveHwnd = GetActiveWindow()
'---------------------------------------------------
'determination du rectangle de capture avec les coordonnée de la fentre active
'---------------------------------------------------
Call GetWindowRect(ActiveHwnd, RECT)
fwidth = (RECT.Right - RECT.Left)
fheight = (RECT.BOTTOM - RECT.Top)
'---------------------------------------------------
' determination du contexte HDC du desktop
'---------------------------------------------------
Hdc = GetDC(DeskHwnd)
hdcMem = CreateCompatibleDC(Hdc)
hBitmap = CreateCompatibleBitmap(Hdc, fwidth - 9, fheight - 24)
If hBitmap <> 0 Then
action = SelectObject(hdcMem, hBitmap)
action = BitBlt(hdcMem, 0, 0, fwidth - 9, fheight - 24, Hdc, RECT.Left + 4.5, RECT.Top + 24, SRCCOPY)
'---------------------------------------------
' vidage et mise en memoire de l'image bitmap dans le clipboard
'---------------------------------------------
action = OpenClipboard(0)
action = EmptyClipboard()
action = SetClipboardData(2, hBitmap)
action = CloseClipboard()
End If
chemin_image = Environ("userprofile") & "\Desktop\" & usf.Name & ".jpg"
With ActiveSheet
.Paste
Set shap = .Shapes(shapecount)
shap.Copy
.ChartObjects.Add(0, 0, shap.Width, shap.Height).Chart.Paste
.ChartObjects(1).Chart.Export Filename:=chemin_image, FilterName:="jpg"
.ChartObjects(1).Delete
shap.Delete
End With
'---------------------------------------------
' Clean up handles
'---------------------------------------------
action = DeleteDC(hdcMem)
action = ReleaseDC(DeskHwnd, Hdc)
End Sub |