name22

Desktop Magnifying Glass (Mousehook + StretchBlt Example)

Nov 17th, 2012
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WinAPI.au3>
  2. #include <Constants.au3>
  3. #include <GUIConstants.au3>
  4. #include <WindowsConstants.au3>
  5.  
  6. ; -Author: name22 (www.autoit.de)
  7.  
  8. Opt("GUIOnEventMode")
  9.  
  10. Global Const $iFlag = BitOR($SWP_NOSIZE, $SWP_NOZORDER), $tagMSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo"
  11. Global $hWnd_Magnifier, $hWnd_Desktop, $hDC_Window, $hDC_Desktop
  12. Global $hDll_GDI32 = DllOpen("gdi32.dll"), $hDll_User32 = DllOpen("user32.dll")
  13.  
  14. Global $iGUIWidth = 100
  15. Global $iGUIHeight = 100
  16. Global $iCaptureWidth = 50
  17. Global $iCaptureHeight = 50
  18.  
  19. $hWnd_Magnifier = GUICreate("", $iGUIWidth, $iGUIHeight, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
  20. GUISetState()
  21.  
  22. $hWnd_Desktop = _WinAPI_GetDesktopWindow()
  23. $hDC_Window = _WinAPI_GetDC($hWnd_Magnifier)
  24. $hDC_Desktop = _WinAPI_GetDC($hWnd_Desktop)
  25. DllCall($hDll_GDI32, "INT", "SetStretchBltMode", "HANDLE", $hDC_Window, "INT", 3)
  26.  
  27. $hMouseProc = DllCallbackRegister("_MouseProc", "int", "int;ptr;ptr")
  28. $hMouseHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hMouseProc), _WinAPI_GetModuleHandle(0), 0)
  29.  
  30. OnAutoItExitRegister("_Shutdown")
  31. GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
  32. HotKeySet("{ESC}", "_Exit")
  33.  
  34. _ReDraw(_WinAPI_GetMousePosX(), _WinAPI_GetMousePosY())
  35.  
  36. While Sleep(1000)
  37. WEnd
  38.  
  39. Func _MouseProc($nCode, $wParam, $lParam)
  40.     If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hMouseHook, $nCode, $wParam, $lParam)
  41.  
  42.     Local $iEvent = BitAND($wParam, 0xFFFF)
  43.     If $iEvent = $WM_MOUSEMOVE Then
  44.         Local $tMHook = DllStructCreate($tagMSLLHOOKSTRUCT, $lParam)
  45.         _ReDraw(DllStructGetData($tMHook, "X"), DllStructGetData($tMHook, "Y"))
  46.     EndIf
  47.  
  48.     Return _WinAPI_CallNextHookEx($hMouseHook, $nCode, $wParam, $lParam)
  49. EndFunc
  50.  
  51. Func _ReDraw($iX_Mouse, $iY_Mouse)
  52.     Local $iX_MouseTmp = $iX_Mouse
  53.     Local $iY_MouseTmp = $iY_Mouse
  54.     If $iX_MouseTmp < 0 Then $iX_MouseTmp = 0
  55.     If $iX_MouseTmp > 1920 - $iGUIWidth - $iCaptureWidth / 2 - 2 Then $iX_MouseTmp = 1920 - $iGUIWidth - $iCaptureWidth / 2 - 2
  56.     If $iY_MouseTmp < 0 Then $iY_MouseTmp = 0
  57.     If $iY_MouseTmp > 1080 - $iGUIHeight - $iCaptureHeight / 2 - 2 Then $iY_MouseTmp = 1080 - $iGUIHeight - $iCaptureHeight / 2 - 2
  58.     DllCall($hDll_User32, "BOOL", "SetWindowPos", "HWND", $hWnd_Magnifier, "HWND", 0, "INT", $iX_MouseTmp + $iCaptureWidth / 2 + 2, "INT", $iY_MouseTmp + $iCaptureHeight / 2 + 2, "INT", 0, "INT", 0, "UINT", $iFlag)
  59.     DllCall($hDll_GDI32, "INT", "StretchBlt", "HWND", $hDC_Window, "INT", 0, "INT", 0, "INT", $iGUIWidth, "INT", $iGUIHeight, "HWND", $hDC_Desktop, "INT", $iX_Mouse - $iCaptureWidth / 2, "INT", $iY_Mouse - $iCaptureHeight / 2, "INT", $iCaptureWidth, "INT", $iCaptureHeight, "DWORD", $SRCCOPY)
  60. EndFunc   ;==>_ReDraw
  61.  
  62. Func _Exit()
  63.     Exit
  64. EndFunc   ;==>_Exit
  65.  
  66. Func _Shutdown()
  67.     _WinAPI_UnhookWindowsHookEx($hMouseHook)
  68.     DllCallbackFree($hMouseProc)
  69.     _WinAPI_ReleaseDC($hWnd_Magnifier, $hDC_Window)
  70.     _WinAPI_ReleaseDC($hWnd_Desktop, $hDC_Desktop)
  71.     DllClose($hDll_GDI32)
  72.     DllClose($hDll_User32)
  73. EndFunc
  74.  
  75.  
  76. ; #FUNCTION# ====================================================================================================================
  77. ; Name...........: _WinAPI_StretchBlt
  78. ; Description....: Copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap
  79. ;                  to fit the dimensions of the destination rectangle, if necessary.
  80. ; Syntax.........: _WinAPI_StretchBlt ( $hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iRop )
  81. ; Parameters.....: $hDestDC     - Handle to the destination device context.
  82. ;                  $iXDest      - The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
  83. ;                  $iYDest      - The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
  84. ;                  $iWidthDest  - The width, in logical units, of the destination rectangle.
  85. ;                  $iHeightDest - The height, in logical units, of the destination rectangle.
  86. ;                  $hSrcDC      - Handle to the source device context.
  87. ;                  $iXSrc       - The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
  88. ;                  $iYSrc       - The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
  89. ;                  $iWidthSrc   - The width, in logical units, of the source rectangle.
  90. ;                  $iHeightSrc  - The height, in logical units, of the source rectangle.
  91. ;                  $iRop        - The raster-operation code. These codes define how the color data for the source rectangle is
  92. ;                                 to be combined with the color data for the destination rectangle to achieve the final color.
  93. ;                                 This parameter must be 0 or one of the following values.
  94. ;
  95. ;                                 $BLACKNESS
  96. ;                                 $CAPTUREBLT
  97. ;                                 $DSTINVERT
  98. ;                                 $MERGECOPY
  99. ;                                 $MERGEPAINT
  100. ;                                 $NOMIRRORBITMAP
  101. ;                                 $NOTSRCCOPY
  102. ;                                 $NOTSRCERASE
  103. ;                                 $PATCOPY
  104. ;                                 $PATINVERT
  105. ;                                 $PATPAINT
  106. ;                                 $SRCAND
  107. ;                                 $SRCCOPY
  108. ;                                 $SRCERASE
  109. ;                                 $SRCINVERT
  110. ;                                 $SRCPAINT
  111. ;                                 $WHITENESS
  112. ;
  113. ; Return values..: Success      - 1.
  114. ;                  Failure      - 0 and sets the @error flag to non-zero.
  115. ; Author.........: Yashied
  116. ; Modified.......:
  117. ; Remarks........: The system stretches or compresses the bitmap according to the stretching mode currently set in the
  118. ;                  destination device context.
  119. ; Related........:
  120. ; Link...........: @@MsdnLink@@ StretchBlt
  121. ; Example........: Yes
  122. ; ===============================================================================================================================
  123.  
  124. Func _WinAPI_StretchBlt($hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iRop)
  125.  
  126.     Local $Ret  = DllCall('gdi32.dll', 'int', 'StretchBlt', 'hwnd', $hDestDC, 'int', $iXDest, 'int', $iYDest, 'int', $iWidthDest, 'int', $iHeightDest, 'hwnd', $hSrcDC, 'int', $iXSrc, 'int', $iYSrc, 'int', $iWidthSrc, 'int', $iHeightSrc, 'dword', $iRop)
  127.  
  128.     If (@error) Or (Not $Ret[0]) Then
  129.         Return SetError(1, 0, 0)
  130.     EndIf
  131.     Return 1
  132. EndFunc   ;==>_WinAPI_StretchBlt
RAW Paste Data