Jump to content

Mouse Screen Capture


wolf9228
 Share

Recommended Posts

Change the name of the variable does not affect the operations code

But better to change

$BORDER_LF_RI_SIZE,$BORDER_TO_BO_SIZE

New script

Mouse_Capture2.zip

Mouse_Capture.au3

#include <WindowsConstants.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <GUIConstantsEx.au3>
#include <StructureConstants.au3>
#include <Constants.au3>
#include <WinAPI.au3>

Global $HHook , $WM_LBUTTON_TEST = 0 , $START_X , $START_Y , $MouseGui , _ 
$LF_RI_SIZE = 0 , $TO_BO_SIZE = 0 , $IMGE_L = 0 , $IMGE_T = 0,$IMGE_W = 0 _ 
,$IMGE_H = 0 , $hBitmap

 
 
Func Mouse_Capture($GUI = 0,$BORDER_LF_RI_SIZE = 3,$BORDER_TO_BO_SIZE = 3,$BORDCOLOR = 0xFF0000)
$LF_RI_SIZE = $BORDER_LF_RI_SIZE ;Global $LF_RI_SIZE,$TO_BO_SIZE
$TO_BO_SIZE = $BORDER_TO_BO_SIZE ;Global $LF_RI_SIZE,$TO_BO_SIZE
if $GUI <> 0 Then GUISetState(@SW_HIDE ,$GUI)
$MouseGui = GUICreate("", 1,1,1,1 ,BitOR($WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS), _ 
BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetBkColor($BORDCOLOR,$MouseGui)
$RegMouseProc = DllCallbackRegister("LowLevelMouseProc", "long", "int;wparam;lparam")
$HMod = _WinAPI_GetModuleHandle(0)
$HHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL,DllCallbackGetPtr($RegMouseProc),$HMod)
While 1
if $WM_LBUTTON_TEST = 2 Then
$hBitmap = BitmapCreate()
if $GUI <> 0 Then GUISetState(@SW_SHOW ,$GUI)
if $GUI <> 0 Then GUISwitch($GUI)
_WinAPI_UnhookWindowsHookEx($HHook)
DllCallbackFree($RegMouseProc)
$WM_LBUTTON_TEST = 0
ExitLoop
EndIf
WEnd
Return $hBitmap
EndFunc


Func LowLevelMouseProc($nCode,$wParam,$lParam)
If $nCode < 0 Then _
Return _WinAPI_CallNextHookEx($HHook, $nCode, $wParam, $lParam)

Select
Case $wParam == $WM_LBUTTONDOWN And $WM_LBUTTON_TEST = 0 
$POINT = DllStructCreate($tagPOINT,$lParam)
$START_X =  DllStructGetData($POINT,1)
$START_Y =  DllStructGetData($POINT,2)
$WM_LBUTTON_TEST = 1
Return 1

Case $wParam == $WM_MOUSEMOVE And $WM_LBUTTON_TEST = 1 
$POINT = DllStructCreate($tagPOINT,$lParam)
$MOVE_X = DllStructGetData($POINT,1)
$MOVE_Y = DllStructGetData($POINT,2)
if $START_X >= $MOVE_X Then
$L = $MOVE_X
$R = $START_X
Else
$R = $MOVE_X
$L = $START_X
EndIf
if $START_Y >= $MOVE_Y Then
$B = $START_Y
$T = $MOVE_Y
Else
$T = $START_Y
$B = $MOVE_Y
EndIf
$W = $R - $L
$H = $B - $T
;Global $IMGE_L = 0 , $IMGE_T = 0,$IMGE_W = 0 _$IMGE_H = 0
Global $IMGE_L = ($L + $LF_RI_SIZE),$IMGE_T = ($T + $TO_BO_SIZE),$IMGE_W = $W , $IMGE_H = $H
DrawGuiRgn($LF_RI_SIZE,$TO_BO_SIZE,$W,$H,$L,$T) ;Global $LF_RI_SIZE $TO_BO_SIZE
Case $wParam == $WM_LBUTTONUP And $WM_LBUTTON_TEST = 1 
GUIDelete($MouseGui)
$WM_LBUTTON_TEST = 2
Return 1

EndSelect

Return _WinAPI_CallNextHookEx($HHook, $nCode, $wParam, $lParam)
EndFunc
 

Func DrawGuiRgn($BORDER_LF_RI_SIZE,$BORDER_TO_BO_SIZE,$WIDTH,$HEIGHT,$LEFT,$TOP)
$W = $BORDER_LF_RI_SIZE * 2 + $WIDTH
$H = $BORDER_TO_BO_SIZE * 2 + $HEIGHT
_WinAPI_SetWindowPos($MouseGui,0,$LEFT,$TOP,$LEFT + $W,$TOP + $H,0)
Sleep(20)
$hREG1 = _WinAPI_CreateRectRgn(0,0,$W,$H)
$hREG2 = _WinAPI_CreateRectRgn($BORDER_LF_RI_SIZE,$BORDER_TO_BO_SIZE,$BORDER_LF_RI_SIZE + $WIDTH,$BORDER_TO_BO_SIZE + $HEIGHT)
$hREG3 = _WinAPI_CreateRectRgn(0, 0, 0, 0)
_WinAPI_CombineRgn($hREG3,$hREG1,$hREG2,$RGN_DIFF)
_WinAPI_DeleteObject($hREG1)
_WinAPI_DeleteObject($hREG2)
_WinAPI_SetWindowRgn($MouseGui,$hREG3)
_WinAPI_DeleteObject($hREG3)
GUISetState(@SW_SHOW,$MouseGui)
EndFunc

Func BitmapCreate()
;Global $IMGE_L = 0 , $IMGE_T = 0,$IMGE_W = 0 , $IMGE_H = 0
$DC = _WinAPI_GetDC(0)
$CompatibleDC = _WinAPI_CreateCompatibleDC($DC)
$hBitmap = _WinAPI_CreateCompatibleBitmap($DC,$IMGE_W,$IMGE_H)
_WinAPI_SelectObject($CompatibleDC, $hBitmap)
_WinAPI_BitBlt($CompatibleDC,0,0,$IMGE_W,$IMGE_H,$DC,$IMGE_L,$IMGE_T,$SRCCOPY)
Return $hBitmap
EndFunc

Mouse_Capture_Example

#Include <GDIPlus.au3>
#Include "Mouse_Capture.au3"
Global _
$MyDocsFolder1 = @MyDocumentsDir, _
$MyDocsFolder2 = @MyDocumentsDir, _
$MyDocsFolder3 = @MyDocumentsDir, _ 
$MyDocsFolder4 = @MyDocumentsDir, _ 
$A = 0 , $_B = 0 , $C = 0 , $D = 0 , $hBitmap
$GUI = GUICreate("My GUI")
$Button_1 = GUICtrlCreateButton("Mouse_Capture", 150, 100, 100)
$Button_2 = GUICtrlCreateButton("Save_Image",150, 200, 100)
GUISetState()
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Button_1
If ($hBitmap) Then _WinAPI_DeleteObject ($hBitmap)
$hBitmap =  Mouse_Capture($GUI)
Case $msg = $Button_2
if ($hBitmap) Then SaveImage($hBitmap,$GUI)
EndSelect
WEnd





Func SaveImage($hBitmap,$GUI)
    GUISetState(@SW_HIDE , $GUI)
    $GUI2 = GUICreate("Save Image",250,100,(@DesktopWidth - 125) / 2, (@DesktopHeight - 50) / 2)
    GUISetFont(12, 400, 0, "MS Sans Serif")
    GUICtrlCreateGroup("Save Image", 10, 10, 230, 80)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_3 = GUICtrlCreateButton("BMP", 15, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_4 = GUICtrlCreateButton("PNG", 70, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_5 = GUICtrlCreateButton("GIF", 125, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_6 = GUICtrlCreateButton("JPG", 185, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW , $GUI2)
    GUISwitch($GUI2)
    $Save_error = True
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button_3
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder1, "IMAGE (*.BMP)", 2,"Untitled" & $A)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolder1 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder1 &= $Split[$J] 
            $MyDocsFolder1 &= "\"
            Next
            $A += 1
            If StringUpper(StringRight($var,4)) <> ".BMP" Then $var &= ".BMP"
            EndIf
            ExitLoop
            Case $msg = $Button_4
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder2, "IMAGE (*.PNG)", 2,"Untitled" & $_B)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolder2 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder2 &= $Split[$J] 
            $MyDocsFolder2 &= "\"
            Next
            $_B += 1
            If StringUpper(StringRight($var,4)) <> ".PNG" Then $var &= ".PNG"
            EndIf
            ExitLoop
            Case $msg = $Button_5
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder3, "IMAGE (*.GIF)", 2,"Untitled" & $C)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolde3 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder3 &= $Split[$J] 
            $MyDocsFolder3 &= "\"
            Next
            $C += 1
            If StringUpper(StringRight($var,4)) <> ".GIF" Then $var &= ".GIF"
            EndIf
            ExitLoop
            Case $msg = $Button_6
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder4, "IMAGE (*.JPG)", 2,"Untitled" & $D)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolde4 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder4 &= $Split[$J] 
            $MyDocsFolder4 &= "\"
            Next
            $D += 1
            If StringUpper(StringRight($var,4)) <> ".JPG" Then $var &= ".JPG"
            EndIf
            ExitLoop
        EndSelect
    WEnd

If Not ($Save_error) Then
_GDIPlus_Startup ()
$Bitmap = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap)
_GDIPlus_ImageSaveToFile ($Bitmap,$var)
_WinAPI_DeleteObject ($Bitmap)
_GDIPlus_ShutDown ()
EndIf
GUIDelete($GUI2)
GUISetState(@SW_SHOW , $GUI)
GUISwitch($GUI)
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Nice function! Thanks for sharing it!

How about a function to put the image to the clipboard?

BR,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Nice function! Thanks for sharing it!

How about a function to put the image to the clipboard?

BR,

UEZ

Why do you mean I dont understand

Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Why do you mean I dont understand

After you marked an area of the desktop put the image to the clipboard to paste it into any graphic program, e.g. IrfanView.

Otherwise you have to save the image and open it in any graphic program.

BR,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

After you marked an area of the desktop put the image to the clipboard to paste it into any graphic program, e.g. IrfanView.

Otherwise you have to save the image and open it in any graphic program.

BR,

UEZ

Do I understand that you want an example of this process code ... OK ... This is

not a General Help Support forum! And Also this position provides an example to

save the image in the hard drive ... And can then used it in any Program

Regards

صرح السماء كان هنا

 

Link to comment
Share on other sites

Do I understand that you want an example of this process code ... OK ... This is

not a General Help Support forum! And Also this position provides an example to

save the image in the hard drive ... And can then used it in any Program

Regards

I meant this (just replace the function below in Mouse_Capture.au3 and add #Include <Clipboard.au3> at the top):

Func BitmapCreate()
    ;Global $IMGE_L = 0 , $IMGE_T = 0,$IMGE_W = 0 , $IMGE_H = 0
    $DC = _WinAPI_GetDC(0)
    $CompatibleDC = _WinAPI_CreateCompatibleDC($DC)
    $hBitmap = _WinAPI_CreateCompatibleBitmap($DC, $IMGE_W, $IMGE_H)
    _WinAPI_SelectObject($CompatibleDC, $hBitmap)
    _WinAPI_BitBlt($CompatibleDC, 0, 0, $IMGE_W, $IMGE_H, $DC, $IMGE_L, $IMGE_T, $SRCCOPY)
    _ClipBoard_Open("")
    _ClipBoard_Empty()
    _ClipBoard_SetDataEx($hBitmap, $CF_BITMAP)
    _ClipBoard_Close()
    Return $hBitmap
EndFunc ;==>BitmapCreate

Now you can paste the selected area to any program!

A crosshair would look nicer when selecting the area -> CrossHairs (full-screen) by Ascend4nt Icon

BR,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Clipboard memory used by the user to save data ... And not right to

use this memory by the programmer only if informed The user before

use ... Quite a brilliant idea And This is the process code

Thank everyone

#Include <GDIPlus.au3>
#Include "Mouse_Capture.au3"
#include <ClipBoard.au3>
Global _
$MyDocsFolder1 = @MyDocumentsDir, _
$MyDocsFolder2 = @MyDocumentsDir, _
$MyDocsFolder3 = @MyDocumentsDir, _ 
$MyDocsFolder4 = @MyDocumentsDir, _ 
$A = 0 , $_B = 0 , $C = 0 , $D = 0 , $hBitmap 
$GUI = GUICreate("My GUI")
$Button_1 = GUICtrlCreateButton("Mouse_Capture", 150, 100, 100)
$Button_2 = GUICtrlCreateButton("Save_Image",150, 200, 100)
$Button_3X = GUICtrlCreateButton("Set_Image To Clipboard",100, 300, 200)
GUISetState()
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Button_1
if ($hBitmap) Then _WinAPI_DeleteObject ($hBitmap)
$hBitmap =  Mouse_Capture($GUI)
Case $msg = $Button_2
if ($hBitmap) Then SaveImage($hBitmap,$GUI)
Case $msg = $Button_3X
if ($hBitmap) And _ClipBoard_Open (0) Then 
_ClipBoard_Empty()
_ClipBoard_SetDataEx ($hBitmap, $CF_BITMAP)
_ClipBoard_Close ()
EndIf
EndSelect
WEnd





Func SaveImage($hBitmap,$GUI)
    GUISetState(@SW_HIDE , $GUI)
    $GUI2 = GUICreate("Save Image",250,100,(@DesktopWidth - 125) / 2, (@DesktopHeight - 50) / 2)
    GUISetFont(12, 400, 0, "MS Sans Serif")
    GUICtrlCreateGroup("Save Image", 10, 10, 230, 80)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_3 = GUICtrlCreateButton("BMP", 15, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_4 = GUICtrlCreateButton("PNG", 70, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_5 = GUICtrlCreateButton("GIF", 125, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button_6 = GUICtrlCreateButton("JPG", 185, 33, 50 , 50)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW , $GUI2)
    GUISwitch($GUI2)
    $Save_error = True
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button_3
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder1, "IMAGE (*.BMP)", 2,"Untitled" & $A)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolder1 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder1 &= $Split[$J] 
            $MyDocsFolder1 &= "\"
            Next
            $A += 1
            If StringUpper(StringRight($var,4)) <> ".BMP" Then $var &= ".BMP"
            EndIf
            ExitLoop
            Case $msg = $Button_4
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder2, "IMAGE (*.PNG)", 2,"Untitled" & $_B)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolder2 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder2 &= $Split[$J] 
            $MyDocsFolder2 &= "\"
            Next
            $_B += 1
            If StringUpper(StringRight($var,4)) <> ".PNG" Then $var &= ".PNG"
            EndIf
            ExitLoop
            Case $msg = $Button_5
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder3, "IMAGE (*.GIF)", 2,"Untitled" & $C)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolde3 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder3 &= $Split[$J] 
            $MyDocsFolder3 &= "\"
            Next
            $C += 1
            If StringUpper(StringRight($var,4)) <> ".GIF" Then $var &= ".GIF"
            EndIf
            ExitLoop
            Case $msg = $Button_6
            $var = FileSaveDialog( "Choose a name.", $MyDocsFolder4, "IMAGE (*.JPG)", 2,"Untitled" & $D)
            $Save_error = @error
            If Not ($Save_error) Then
            $MyDocsFolde4 = ""
            $Split = StringSplit($var,"\")
            For $J = 1 To $Split[0] - 1
            $MyDocsFolder4 &= $Split[$J] 
            $MyDocsFolder4 &= "\"
            Next
            $D += 1
            If StringUpper(StringRight($var,4)) <> ".JPG" Then $var &= ".JPG"
            EndIf
            ExitLoop
        EndSelect
    WEnd

If Not ($Save_error) Then
_GDIPlus_Startup ()
$Bitmap = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap)
_GDIPlus_ImageSaveToFile ($Bitmap,$var)
_WinAPI_DeleteObject ($Bitmap)
_GDIPlus_ShutDown ()
EndIf
GUIDelete($GUI2)
GUISetState(@SW_SHOW , $GUI)
GUISwitch($GUI)
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...