Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. ;==============================================
  2. ; Author: Toady
  3. ;
  4. ; Purpose: Takes screenshot of
  5. ; a user selected region and saves
  6. ; the screenshot in ./images directory.
  7. ;
  8. ; Rewrite by Ashaman42 for latest UDF's
  9. ;
  10. ; How to use:
  11. ; Press "s" key to select region corners.
  12. ; NOTE: Must select top-left of region
  13. ; first, then select bottom-right of region.
  14. ;=============================================
  15.  
  16. #include <ScreenCapture.au3>
  17. #include <misc.au3>
  18. #include <GuiConstants.au3>
  19. #include <WinAPI.au3>
  20. #Include <GuiListBox.au3>
  21. #Include <GuiStatusBar.au3>
  22. #include <GDIPlus.au3>
  23. #include <WindowsConstants.au3>
  24.  
  25. _Singleton("cap")
  26.  
  27. Global $format = ".jpg"
  28. Global $filename = ""
  29. Global $title = "Screen Region Capture"
  30.  
  31. DirCreate(@ScriptDir & "/images/")
  32.  
  33. $GUI = GUICreate($title,610,210,-1,-1)
  34. GUICtrlCreateGroup("Select Format",18,80,210,50)
  35. $radio1 = GUICtrlCreateRadio("JPG",30,100,40)
  36. GuiCtrlSetState(-1, $GUI_CHECKED)
  37. $radio2 = GUICtrlCreateRadio("BMP",80,100,45)
  38. $radio3 = GUICtrlCreateRadio("GIF",130,100,45)
  39. $radio4 = GUICtrlCreateRadio("PNG",180,100,45)
  40. GUICtrlCreateGroup ("",-99,-99,1,1)
  41. GUICtrlCreateLabel("Name of image",20,20)
  42. $gui_img_input = GUICtrlCreateInput("",20,40,200,20)
  43. $go_button = GUICtrlCreateButton("Select region",20,140,200,30)
  44. $list = GUICtrlCreateList("",240,20,150,150)
  45. $editbutton = GUICtrlCreateButton("Edit", 400,60,40)
  46. $deletebutton = GUICtrlCreateButton("Delete", 400,100,40)
  47. $exitbutton = GUICtrlCreateButton("Exit", 400,140,40)
  48. Global $a_PartsRightEdge[5] = [240,320,400,480,-1]
  49. Global $a_PartsText[5] = [@TAB & "Your Name Here!","","","",""]
  50. Global $hImage
  51. $statusbar = _GUICtrlStatusBar_Create($GUI,$a_PartsRightEdge,$a_PartsText)
  52. GUISetState(@SW_SHOW,$GUI)
  53.  
  54. $SELECT_H = GUICreate( "AU3SelectionBox", 0 , 0 , 0, 0, $WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)
  55. GUISetBkColor(0x00FFFF,$SELECT_H)
  56. WinSetTrans("AU3SelectionBox","",60)
  57. GUISetState(@SW_SHOW,$SELECT_H)
  58.  
  59. _ListFiles()
  60.  
  61. While 1
  62. $msg = GUIGetMsg()
  63. Select
  64. Case $msg = $GUI_EVENT_CLOSE
  65. Exit
  66. Case $msg = $GUI_EVENT_RESTORE
  67. _ListFiles()
  68. Case $msg = $radio1
  69. $format = ".jpg"
  70. Case $msg = $radio2
  71. $format = ".bmp"
  72. Case $msg = $radio3
  73. $format = ".gif"
  74. Case $msg = $radio4
  75. $format = ".png"
  76. Case $msg = $editbutton
  77. ShellExecute(_GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)),"",@ScriptDir & "\images\","edit")
  78. Case $msg = $deletebutton
  79. Local $msgbox = MsgBox(4,"Delete image","Are you sure?")
  80. If $msgbox = 6 Then
  81. FileDelete(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
  82. _ListFiles()
  83. EndIf
  84. Case $msg = $exitbutton
  85. Exit
  86. Case $msg = $list
  87. _ListClick()
  88. Case $msg = $go_button
  89. $filename = GUICtrlRead($gui_img_input)
  90. If $filename <> "" Then
  91. Local $msgbox = 6
  92. If FileExists(@ScriptDir & "\images\" & $filename & $format) Then
  93. $msgbox = MsgBox(4,"Already Exists","File name already exists, do you want to overwrite it?")
  94. EndIf
  95. If $msgbox = 6 Then
  96. GUISetState(@SW_HIDE,$GUI)
  97. _WinAPI_MoveWindow($SELECT_H,1,1,2,2)
  98. GUISetState(@SW_RESTORE,$SELECT_H)
  99. GUISetState(@SW_SHOW,$SELECT_H)
  100. _TakeScreenShot()
  101. GUICtrlSetData($gui_img_input,"")
  102. GUISetState(@SW_SHOW,$GUI)
  103. _ListFiles()
  104. EndIf
  105. Else
  106. MsgBox(0,"Error","Enter a filename")
  107. EndIf
  108. EndSelect
  109. WEnd
  110.  
  111. Func _ConvertSize($size_bytes)
  112. If $size_bytes < 1024*1024 Then
  113. Return Round($size_bytes/1024,2) & " KB"
  114. Else
  115. Return Round($size_bytes/1024/1024,2) & " MB"
  116. EndIf
  117. EndFunc
  118.  
  119. Func _ConvertTime($time)
  120. Local $time_converted = $time
  121. Local $time_data = StringSplit($time, ":")
  122. If $time_data[1] > 12 Then
  123. $time_converted = $time_data[1] - 12 & ":" & $time_data[2] & " PM"
  124. ElseIf $time_data[1] = 12 Then
  125. $time_converted = $time & " PM"
  126. Else
  127. $time_converted &= " AM"
  128. EndIf
  129. Return $time_converted
  130. EndFunc
  131.  
  132. Func _ListClick()
  133. If _GUICtrlListBox_GetCurSel($list) = -1 Then ;List clicked but nothing selected
  134. Return
  135. EndIf
  136. GUICtrlSetState($deletebutton, $GUI_ENABLE)
  137. GUICtrlSetState($editbutton, $GUI_ENABLE)
  138. Local $date = FileGetTime(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
  139. _GUICtrlStatusBar_SetText($statusbar,@tab & "Size: " & _ConvertSize(FileGetSize(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))),1)
  140. _GUICtrlStatusBar_SetText($statusbar,@tab & "Date: " & $date[1] & "/" & $date[2] & "/" & StringTrimLeft($date[0],2) & " " & _ConvertTime($date[3] & ":" & $date[4]),4)
  141. _GDIPlus_StartUp()
  142. $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
  143. $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gui)
  144. $iWidth = _GDIPlus_ImageGetWidth ($hImage)
  145. $iHeight = _GDIPlus_ImageGetHeight($hImage)
  146. _GUICtrlStatusBar_SetText($statusbar,@tab & "Width: " & $iWidth,2)
  147. _GUICtrlStatusBar_SetText($statusbar,@tab & "Height: " & $iHeight,3)
  148. Local $destW = 150
  149. Local $destH = 150
  150. _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, 0, 0,$iWidth,$iHeight,450,20,$destW ,$destH)
  151. _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 0)
  152. _GDIPlus_GraphicsDrawRect($hGraphic, 450, 20, $destW, $destH)
  153. _GDIPlus_GraphicsDispose($hGraphic)
  154. _GDIPlus_ImageDispose($hImage)
  155. _GDIPlus_ShutDown()
  156. EndFunc
  157.  
  158. Func _TakeScreenShot()
  159. Local $x, $y
  160. HotKeySet("s","_DoNothing")
  161. While Not _IsPressed(Hex(83,2))
  162. Local $currCoord = MouseGetPos()
  163. Sleep(10)
  164. ToolTip("Select top-left coord with 's' key" & @CRLF & "First coord: " & $currCoord[0] & "," & $currCoord[1])
  165. If _IsPressed(Hex(83,2)) Then
  166. While _IsPressed(Hex(83,2))
  167. Sleep(10)
  168. WEnd
  169. ExitLoop 1
  170. EndIf
  171. WEnd
  172. Local $firstCoord = MouseGetPos()
  173. _WinAPI_MoveWindow($SELECT_H,$firstCoord[0],$firstCoord[1],1,1)
  174. While Not _IsPressed(Hex(83,2))
  175. Local $currCoord = MouseGetPos()
  176. Sleep(10)
  177. ToolTip("Select bottom-right coord with 's' key" & @CRLF & "First coord: " & $firstCoord[0] & "," & $firstCoord[1] _
  178. & @CRLF & "Second coord: " & $currCoord[0] & "," & $currCoord[1] & @CRLF & "Image size: " & _
  179. Abs($currCoord[0]-$firstCoord[0]) & "x" & Abs($currCoord[1]-$firstCoord[1]))
  180. $x = _RubberBand_Select_Order($firstCoord[0],$currCoord[0])
  181. $y = _RubberBand_Select_Order($firstCoord[1],$currCoord[1])
  182. _WinAPI_MoveWindow($SELECT_H,$x[0],$y[0],$x[1],$y[1])
  183. If _IsPressed(Hex(83,2)) Then
  184. While _IsPressed(Hex(83,2))
  185. Sleep(10)
  186. WEnd
  187. ExitLoop 1
  188. EndIf
  189. WEnd
  190. ToolTip("")
  191. Local $secondCoord = MouseGetPos()
  192. _WinAPI_MoveWindow($SELECT_H,1,1,2,2)
  193. GUISetState(@SW_HIDE,$SELECT_H)
  194. Sleep(100)
  195. _ScreenCapture_SetJPGQuality(80)
  196. _ScreenCapture_Capture(@ScriptDir & "\images\" & $filename & $format,$x[0], $y[0], $x[1]+$x[0], $y[1]+$y[0])
  197. HotKeySet("s")
  198. EndFunc
  199.  
  200. Func _ListFiles()
  201. _GUICtrlListBox_ResetContent($list)
  202. $search = FileFindFirstFile(@ScriptDir & "\images\*.*")
  203. If $search <> -1 Then
  204. While 1
  205. Local $file = FileFindNextFile($search)
  206. If @error Then ExitLoop
  207. If StringRegExp($file,"(.bmp)|(.jpg)|(.gif)|(.png)") Then
  208. $test_error = _GUICtrlListBox_AddFile($list, @ScriptDir & "\images\" & $file)
  209. EndIf
  210. WEnd
  211. EndIf
  212. FileClose($search)
  213. If _GUICtrlListBox_GetCurSel($list) = -1 Then ; No selection
  214. GUICtrlSetState($deletebutton, $GUI_DISABLE)
  215. GUICtrlSetState($editbutton, $GUI_DISABLE)
  216. _GUICtrlStatusBar_SetText($statusbar,"",1)
  217. _GUICtrlStatusBar_SetText($statusbar,"",2)
  218. _GUICtrlStatusBar_SetText($statusbar,"",3)
  219. _GUICtrlStatusBar_SetText($statusbar,"",4)
  220. _GDIPlus_Startup()
  221. $hGraphicss = _GDIPlus_GraphicsCreateFromHWND($GUI)
  222. _GDIPlus_GraphicsFillRect($hGraphicss, 450, 20, 150, 150)
  223. _GDIPlus_GraphicsDispose($hGraphicss)
  224. _GDIPlus_Shutdown()
  225. _DrawText("Preview",495, 80)
  226. Else
  227. GUICtrlSetState($deletebutton, $GUI_ENABLE)
  228. GUICtrlSetState($editbutton, $GUI_ENABLE)
  229. _GUICtrlListBox_GetCurSel($list)
  230. WinWaitActive("Screen Region Capture")
  231. _ListClick()
  232. EndIf
  233. EndFunc
  234.  
  235. Func _DrawText($text,$x, $y)
  236. _GDIPlus_Startup()
  237. $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
  238. $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
  239. $hFormat = _GDIPlus_StringFormatCreate()
  240. $hFamily = _GDIPlus_FontFamilyCreate("Arial")
  241. $hFont = _GDIPlus_FontCreate($hFamily, 12, 2)
  242. $tLayout = _GDIPlus_RectFCreate($x, $y, 0, 0)
  243. $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
  244. _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush)
  245. _GDIPlus_FontDispose ($hFont )
  246. _GDIPlus_FontFamilyDispose ($hFamily )
  247. _GDIPlus_StringFormatDispose($hFormat )
  248. _GDIPlus_BrushDispose ($hBrush )
  249. _GDIPlus_GraphicsDispose ($hGraphic)
  250. _GDIPlus_Shutdown()
  251. EndFunc
  252.  
  253. Func _RubberBand_Select_Order($a,$b)
  254. Dim $res[2]
  255. If $a < $b Then
  256. $res[0] = $a
  257. $res[1] = $b - $a
  258. Else
  259. $res[0] = $b
  260. $res[1] = $a - $b
  261. EndIf
  262. Return $res
  263. EndFunc
  264.  
  265. Func _DoNothing()
  266. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement