Option Explicit
Type POINTAPI
X As Long
Y As Long
End Type
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type LOGPEN
lopnStyle As Long
lopnWidth As POINTAPI
lopnColor As Long
End Type
Type ICONINFO
fIcon As Long
xHotspot As Long
yHotspot As Long
#If VBA7 Then
hbmMask As LongPtr
hbmColor As LongPtr
#Else
hbmMask As Long
hbmColor As Long
#End If
End Type
Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
#If VBA7 Then
bmBits As LongPtr
#Else
bmBits As Long
#End If
End Type
Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Type uPicDesc
Size As Long
Type As Long
#If VBA7 Then
hPic As LongPtr
hPal As LongPtr
#Else
hPic As Long
hPal As Long
#End If
End Type
#If VBA7 Then
#If Win64 Then
Declare PtrSafe Function SetWindowLong Lib "USER32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#Else
Declare PtrSafe Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#End If
#Else
Declare Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If
#If VBA7 Then
Declare PtrSafe Function GetDC Lib "USER32" (ByVal hWnd As LongPtr) As LongPtr
Declare PtrSafe Function GetNextWindow Lib "USER32" Alias "GetWindow" (ByVal hWnd As LongPtr, ByVal wFlag As Long) As LongPtr
Declare PtrSafe Function ReleaseDC Lib "USER32" (ByVal hWnd As LongPtr, ByVal hdc As LongPtr) As Long
Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Declare PtrSafe Function GetActiveWindow Lib "USER32" () As LongPtr
Declare PtrSafe Function GetCursorPos Lib "USER32" (lpPoint As POINTAPI) As Long
Declare PtrSafe Function SelectObject Lib "gdi32" (ByVal hdc As LongPtr, ByVal hObject As LongPtr) As LongPtr
Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
Declare PtrSafe Function LineTo Lib "gdi32" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long) As Long
Declare PtrSafe Function MoveToEx Lib "gdi32" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long, lpPoint As Any) As Long
Declare PtrSafe Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Declare PtrSafe Function DeleteDC Lib "gdi32" (ByVal hdc As LongPtr) As Long
Declare PtrSafe Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As LongPtr
Declare PtrSafe Function FillRgn Lib "gdi32" (ByVal hdc As LongPtr, ByVal hRgn As LongPtr, ByVal hBrush As LongPtr) As Long
Declare PtrSafe Function CreatePenIndirect Lib "gdi32" (lpLogPen As LOGPEN) As LongPtr
Declare PtrSafe Function CreateIconIndirect Lib "USER32" (piconinfo As ICONINFO) As LongPtr
Declare PtrSafe Function SetCursor Lib "USER32" (ByVal hCursor As LongPtr) As LongPtr
Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long) As Long
Declare PtrSafe Function SetPixel Lib "gdi32" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
Declare PtrSafe Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As LongPtr, ByVal nCount As Long, lpObject As Any) As Long
Declare PtrSafe Function CloseClipboard Lib "USER32" () As Long
Declare PtrSafe Function OpenClipboard Lib "USER32" (ByVal hWnd As LongPtr) As Long
Declare PtrSafe Function EmptyClipboard Lib "USER32" () As Long
Declare PtrSafe Function SetClipboardData Lib "USER32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GetClipboardData Lib "USER32" (ByVal wFormat As Long) As LongPtr
Declare PtrSafe Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As LongPtr) As LongPtr
Declare PtrSafe Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As LongPtr, ByVal nWidth As Long, ByVal nHeight As Long) As LongPtr
Declare PtrSafe Function BitBlt Lib "gdi32" (ByVal hDestDC As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As LongPtr, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Declare PtrSafe Function FloodFill Lib "gdi32" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
Declare PtrSafe Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As LongPtr
Declare PtrSafe Function CallWindowProc Lib "USER32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As LongPtr, ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Declare PtrSafe Function DestroyIcon Lib "USER32" (ByVal hIcon As LongPtr) As Long
Declare PtrSafe Function ScreenToClient Lib "USER32" (ByVal hWnd As LongPtr, lpPoint As POINTAPI) As Long
Declare PtrSafe Function InvalidateRect Lib "USER32" (ByVal hWnd As LongPtr, ByVal lpRect As Long, ByVal bErase As Long) As Long
Declare PtrSafe Function GetWindowRect Lib "USER32" (ByVal hWnd As LongPtr, lpRect As RECT) As Long
Declare PtrSafe Function OleCreatePictureIndirect Lib "oleAut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As LongPtr, IPic As IPicture) As LongPtr
Declare PtrSafe Function IsClipboardFormatAvailable Lib "USER32" (ByVal wFormat As Integer) As Long
Public lPrevFrmProc As LongPtr
Private hCursor As LongPtr
#Else
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Declare Function CreatePenIndirect Lib "gdi32" (lpLogPen As LOGPEN) As Long
Declare Function CreateIconIndirect Lib "user32" (piconinfo As ICONINFO) As Long
Declare Function SetCursor Lib "user32" (ByVal hCursor 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 GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function EmptyClipboard Lib "user32" () As Long
Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (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 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 FloodFill Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function OleCreatePictureIndirect Lib "oleAut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
Public lPrevFrmProc As Long
Private hCursor As Long
#End If
Public Const PS_SOLID = 0
Public Const PS_DASH = 1
Public Const PS_DOT = 2
Public Const PS_DASHDOT = 3
Public Const PS_DASHDOTDOT = 4
Public Const GWL_WNDPROC = (-4)
Public Const GW_CHILD = 5
Public Const SRCCOPY = &HCC0020
Private Const WM_EXITSIZEMOVE = &H232
Private Const WM_MOVING = &H216
Private Const WM_SETREDRAW = &HB
Private Const CF_BITMAP = 2
Private Const PICTYPE_BITMAP = 1
Private Const S_OK = &H0
#If VBA7 Then
Public Function FrmCallBack(ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
#Else
Public Function FrmCallBack(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If
Select Case Msg
Case WM_MOVING
SendMessage hWnd, WM_SETREDRAW, ByVal 0, 0
Case WM_EXITSIZEMOVE
SendMessage hWnd, WM_SETREDRAW, ByVal 1, 0
End Select
FrmCallBack = CallWindowProc(lPrevFrmProc, hWnd, Msg, wParam, ByVal lParam)
End Function
#If VBA7 Then
Sub ExtractPictureTo(ByVal hWnd As LongPtr, ByVal ToRangeOrFile As Variant)
Dim hPtr As LongPtr
Dim hdc As LongPtr
Dim hDCMem As LongPtr
Dim hBitmap As LongPtr
#Else
Sub ExtractPictureTo(ByVal hWnd As Long, ByVal ToRangeOrFile As Variant)
Dim hPtr As Long
Dim hdc As Long
Dim hDCMem As Long
Dim hBitmap As Long
#End If
Dim IID_IDispatch As GUID
Dim uPicinfo As uPicDesc
Dim IPic As IPicture
Dim tWndRect As RECT
Dim tPt As POINTAPI
Dim oShp As Shape
Dim lWidth As Long
Dim lHeight As Long
On Error GoTo Xit
GetWindowRect hWnd, tWndRect
With tWndRect
lWidth = .Right - .Left
lHeight = .Bottom - .Top
tPt.X = .Left
tPt.Y = .Top
ScreenToClient hWnd, tPt
.Left = tPt.X
.Top = tPt.Y
End With
hdc = GetDC(hWnd)
hDCMem = CreateCompatibleDC(hdc)
hBitmap = CreateCompatibleBitmap(hdc, lWidth, lHeight)
If hBitmap <> 0 Then
Call SelectObject(hDCMem, hBitmap)
Call BitBlt(hDCMem, 0, 0, lWidth, lHeight, hdc, tWndRect.Left, tWndRect.Top, SRCCOPY)
Call OpenClipboard(0)
Call EmptyClipboard
Call SetClipboardData(CF_BITMAP, hBitmap)
Call DeleteDC(hDCMem)
Call ReleaseDC(hWnd, hdc)
If IsClipboardFormatAvailable(CF_BITMAP) <> 0 Then
hPtr = GetClipboardData(CF_BITMAP)
CloseClipboard
If hPtr <> 0 Then
With IID_IDispatch
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
With uPicinfo
.Size = Len(uPicinfo)
.Type = PICTYPE_BITMAP
.hPic = hPtr
.hPal = 0
End With
If OleCreatePictureIndirect(uPicinfo, IID_IDispatch, True, IPic) = S_OK Then
If TypeName(ToRangeOrFile) = "Range" Then
stdole.SavePicture IPic, Environ("temp") & "\IMG.bmp"
Set oShp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, _
CallByName(ToRangeOrFile, "Left", VbGet), CallByName(ToRangeOrFile, "Top", VbGet), 100, 50)
With oShp.Fill
.Visible = msoTrue
.UserPicture Environ("temp") & "\IMG.bmp"
End With
Kill Environ("temp") & "\IMG.bmp"
Else
stdole.SavePicture IPic, ToRangeOrFile
End If
End If
End If
End If
End If
Xit:
CloseClipboard
End Sub
#If VBA7 Then
Public Sub ChangeCursor(ByVal DC As LongPtr, ByVal W As Integer, ByVal h As Integer, ByVal Rubbing As Boolean)
Dim hDCMem As LongPtr
Dim hFillBrush As LongPtr
Dim hPrevCursor As LongPtr
Dim hBitmap As LongPtr
Dim hRgn As LongPtr
Dim hAndMaskBitmap As LongPtr
Dim hXORMaskBitmap As LongPtr
Dim hAndMaskDC As LongPtr
Dim hXorMaskDC As LongPtr
Dim lOldAndMaskBmp As LongPtr
Dim lOldXorMaskBmp As LongPtr
#Else
Public Sub ChangeCursor(ByVal DC As Long, ByVal W As Integer, ByVal h As Integer, ByVal Rubbing As Boolean)
Dim hDCMem As Long
Dim hFillBrush As Long
Dim hPrevCursor As Long
Dim hBitmap As Long
Dim hRgn As Long
Dim hAndMaskBitmap As Long
Dim hXORMaskBitmap As Long
Dim hAndMaskDC As Long
Dim hXorMaskDC As Long
Dim lOldAndMaskBmp As Long
Dim lOldXorMaskBmp As Long
#End If
Dim lColor As Long
Dim X As Long, Y As Long
Dim tBMP As BITMAP
Dim tIcoInfo As ICONINFO
Static bRubbing As Boolean
If hCursor <> 0 And bRubbing = Rubbing Then hPrevCursor = SetCursor(hCursor): bRubbing = Rubbing: Exit Sub
bRubbing = Rubbing
Call DestroyCursor
hDCMem = CreateCompatibleDC(DC)
hBitmap = CreateCompatibleBitmap(DC, W, h)
If hBitmap <> 0 Then
GetObjectAPI hBitmap, LenB(tBMP), tBMP
DeleteObject SelectObject(hDCMem, hBitmap)
DeleteObject hBitmap
If Rubbing Then
hFillBrush = CreateSolidBrush(vbRed)
DeleteObject SelectObject(hDCMem, hFillBrush)
hRgn = CreateEllipticRgn(0, 0, tBMP.bmWidth, tBMP.bmHeight)
FillRgn hDCMem, hRgn, hFillBrush
DeleteObject hRgn
lColor = vbRed
Else
hFillBrush = CreateSolidBrush(RGB(176, 196, 222))
DeleteObject SelectObject(hDCMem, hFillBrush)
FloodFill hDCMem, 0, 0, RGB(176, 196, 222)
lColor = RGB(176, 196, 222)
End If
DeleteObject hFillBrush
hAndMaskDC = CreateCompatibleDC(hDCMem)
hXorMaskDC = CreateCompatibleDC(hDCMem)
hAndMaskBitmap = CreateCompatibleBitmap(hDCMem, tBMP.bmWidth, tBMP.bmHeight)
hXORMaskBitmap = CreateCompatibleBitmap(hDCMem, tBMP.bmWidth, tBMP.bmHeight)
lOldAndMaskBmp = SelectObject(hAndMaskDC, hAndMaskBitmap)
lOldXorMaskBmp = SelectObject(hXorMaskDC, hXORMaskBitmap)
For X = 0 To tBMP.bmWidth
For Y = 0 To tBMP.bmHeight
If GetPixel(hDCMem, X, Y) <> lColor Then
SetPixel hAndMaskDC, X, Y, RGB(255, 255, 255)
SetPixel hXorMaskDC, X, Y, RGB(0, 0, 0)
Else
SetPixel hAndMaskDC, X, Y, RGB(0, 0, 0)
SetPixel hXorMaskDC, X, Y, lColor
End If
Next Y
Next X
SelectObject hAndMaskDC, lOldAndMaskBmp
SelectObject hXorMaskDC, lOldXorMaskBmp
With tIcoInfo
.fIcon = False
.xHotspot = tBMP.bmWidth / 2
.yHotspot = tBMP.bmWidth / 2
.hbmMask = hAndMaskBitmap
.hbmColor = hXORMaskBitmap
End With
hCursor = CreateIconIndirect(tIcoInfo)
hPrevCursor = SetCursor(hCursor)
DeleteObject hAndMaskBitmap
DeleteObject hXORMaskBitmap
DeleteObject lOldAndMaskBmp
DeleteObject lOldXorMaskBmp
DeleteDC hAndMaskDC
DeleteDC hXorMaskDC
End If
DeleteDC hDCMem
End Sub
#If VBA7 Then
Public Sub OnCanvasClick(ByVal hWnd As LongPtr, ByVal DC As LongPtr, ByVal PenStyle As Integer, ByVal PenWidth As Integer, ByVal PenColor As Long, ByVal CanvasColor As Long, ByVal Erasing As Boolean)
Dim hRgn As LongPtr
Dim hBrush As LongPtr
Dim hPen As LongPtr
#Else
Public Sub OnCanvasClick(ByVal hWnd As Long, ByVal DC As Long, ByVal PenStyle As Integer, ByVal PenWidth As Integer, ByVal PenColor As Long, ByVal CanvasColor As Long, ByVal Erasing As Boolean)
Dim hRgn As Long
Dim hBrush As Long
Dim hPen As Long
#End If
Dim tPt As POINTAPI
Dim tPen As LOGPEN
With tPen
.lopnColor = IIf(Erasing, CanvasColor, PenColor)
.lopnStyle = PenStyle
.lopnWidth.X = PenWidth
.lopnWidth.Y = PenWidth
End With
hPen = CreatePenIndirect(tPen)
SelectObject DC, hPen
GetCursorPos tPt
ScreenToClient hWnd, tPt
With tPen
hRgn = CreateEllipticRgn(tPt.X - (.lopnWidth.X / 2), tPt.Y - (.lopnWidth.Y / 2), tPt.X + (.lopnWidth.X / 2), tPt.Y + (.lopnWidth.Y / 2))
hBrush = CreateSolidBrush(.lopnColor)
End With
FillRgn DC, hRgn, hBrush
DeleteObject hBrush
DeleteObject hRgn
DeleteObject hPen
End Sub
Public Sub DestroyCursor(Optional ByVal Dummy As Boolean)
DestroyIcon hCursor
hCursor = 0
End Sub