Download
Code: Select all
EnableExplicit
Enumeration
#Win_Tool
#Win_Sel
#Win_View
EndEnumeration
Enumeration
#btnRect
#btnSave
#img
EndEnumeration
#Img2 = 0
Global x1, y1, x2, y2
Global hRectangle_GUI
Global HDC
Define hBmp
Define hPreview
Define WWEvent
Define hCursor
Define Mouse_PosX_old, Mouse_PosY_old, hMask
Define Path$
Define Mouse_PosX, Mouse_PosY, tmp, x_Pos, Width, Height, y_Pos, isStarCapture = 0
Declare GUICreateInvRect(hWnd, X, Y, W, H)
Declare HBitmapFromScreen(X, Y, W, H)
ExamineDesktops()
DataSection
cross:
IncludeBinary "cross.ico"
crossend:
save:
IncludeBinary "save.ico"
saveend:
EndDataSection
CatchImage(0, ?cross)
CatchImage(1, ?save)
;- GUI
OpenWindow(#Win_Tool, 0, 0, 240, 40, "Select", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
ButtonImageGadget(#btnRect, 5, 5, 30, 30, ImageID(0))
ButtonImageGadget(#btnSave, 40, 5, 30, 30, ImageID(1))
hRectangle_GUI = OpenWindow(#Win_Sel, 0, 0, DesktopWidth(0), DesktopHeight(0), "", #WS_POPUP | #PB_Window_BorderLess | #PB_Window_Invisible)
If hRectangle_GUI
SetWindowColor(#Win_Sel, 0)
SetWindowLong_(hRectangle_GUI, #GWL_EXSTYLE, #WS_EX_LAYERED | #WS_EX_TOPMOST)
SetLayeredWindowAttributes_(hRectangle_GUI, #Blue, 110, #LWA_COLORKEY | #LWA_ALPHA)
GUICreateInvRect(hRectangle_GUI, 0, 0, 1, 1)
hCursor = LoadCursor_(0, #OCR_CROSS)
SetClassLongPtr_(hRectangle_GUI, #GCL_HCURSOR, hCursor) ; WindowID(#Win_Sel)
StickyWindow(#Win_Sel, #True)
EndIf
;- Event
Repeat
WWEvent = WaitWindowEvent()
Select EventWindow()
Case #Win_Tool
Select WWEvent
Case #PB_Event_Gadget
Select EventGadget()
Case #btnSave
If hPreview
Path$ = SaveFileRequester("", GetCurrentDirectory(), "Изображение (*.bmp)|*.bmp", 0)
If Asc(Path$)
If Right(Path$, 4) <> ".bmp"
Path$ + ".bmp"
EndIf
If CreateImage(#Img2, x2 - x1, y2 - y1, 24)
StartDrawing(ImageOutput(#Img2))
DrawImage(hBmp , 0 , 0)
StopDrawing()
If IsImage(#Img2)
SaveImage(#Img2 , Path$)
EndIf
FreeImage(#Img2)
EndIf
EndIf
EndIf
Case #btnRect
If hPreview
CloseWindow(#Win_View)
hPreview = 0
EndIf
HideWindow(#Win_Tool, #True)
HideWindow(#Win_Sel, #False)
isStarCapture = 1
hMask = CreateRectRgn_(0, 0, DesktopWidth(0), DesktopHeight(0))
SetWindowRgn_(hRectangle_GUI, hMask, 1)
DeleteObject_(hMask)
EndSelect
Case #PB_Event_CloseWindow
CloseWindow(#Win_Tool)
ReleaseDC_(0, HDC)
DestroyCursor_(hCursor)
CloseWindow(#Win_Sel)
If hPreview
CloseWindow(#Win_View)
DeleteObject_(hBmp)
EndIf
End
EndSelect
Case #Win_View
If WWEvent = #PB_Event_CloseWindow
CloseWindow(#Win_View)
hPreview = 0
DeleteObject_(hBmp)
EndIf
Case #Win_Sel
If GetAsyncKeyState_(#VK_LBUTTON)
If isStarCapture
; Get first mouse position
x1 = WindowMouseX(#Win_Sel)
y1 = WindowMouseY(#Win_Sel)
; Draw rectangle while mouse button pressed
While GetAsyncKeyState_(#VK_LBUTTON)
Delay(10)
Mouse_PosX = WindowMouseX(#Win_Sel)
Mouse_PosY = WindowMouseY(#Win_Sel)
If Mouse_PosX = Mouse_PosX_old And Mouse_PosY = Mouse_PosY_old
Continue
EndIf
; Set in correct order if required
If Mouse_PosX < x1
x_Pos = Mouse_PosX
Width = x1 - Mouse_PosX
Else
x_Pos = x1
Width = Mouse_PosX - x1
EndIf
If Mouse_PosY < y1
y_Pos = Mouse_PosY
Height = y1 - Mouse_PosY
Else
y_Pos = y1
Height = Mouse_PosY - y1
EndIf
GUICreateInvRect(hRectangle_GUI, x_Pos, y_Pos, Width, Height)
Mouse_PosX_old = Mouse_PosX
Mouse_PosY_old = Mouse_PosY
Wend
; Get second mouse position
x2 = Mouse_PosX
y2 = Mouse_PosY
; Set in correct order if required
If x2 < x1
tmp = x1
x1 = x2
x2 = tmp
EndIf
If y2 < y1
tmp = y1
y1 = y2
y2 = tmp
EndIf
hBmp = HBitmapFromScreen( x1, y1, x2 - x1, y2 - y1)
HideWindow(#Win_Tool, #False)
hPreview = OpenWindow(#Win_View, 330, 0, x2 - x1, y2 - y1, "Selected Rectangle", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
ImageGadget(#img, 0 , 0, x2 - x1, y2 - y1, hBmp)
HideWindow(#Win_Sel, #True)
isStarCapture = 0
EndIf
EndIf
EndSelect
ForEver
Procedure HBitmapFromScreen(X, Y, W, H)
Protected hBmp, PDC
If Not HDC
HDC = GetDC_(0)
EndIf
hBmp = CreateCompatibleBitmap_(HDC, W, H)
PDC = CreateCompatibleDC_(HDC)
SelectObject_(PDC, hBmp)
BitBlt_(PDC, 0, 0, W, H, HDC, X, Y, #SRCCOPY)
DeleteDC_(PDC)
ProcedureReturn hBmp
EndProcedure
Procedure GUICreateInvRect(hWnd, X, Y, W, H)
Protected hMask_Top, hMask_Left, hMask_Right, hMask_Bottom
hMask_Top = CreateRectRgn_(0, 0, DesktopWidth(0), Y)
hMask_Left = CreateRectRgn_(0, 0, X, DesktopHeight(0))
hMask_Right = CreateRectRgn_(X + W, 0, DesktopWidth(0), DesktopHeight(0))
hMask_Bottom = CreateRectRgn_(0, Y + H, DesktopWidth(0), DesktopHeight(0))
CombineRgn_(hMask_Top, hMask_Top, hMask_Left, 2)
CombineRgn_(hMask_Top, hMask_Top, hMask_Right, 2)
CombineRgn_(hMask_Top, hMask_Top, hMask_Bottom, 2)
DeleteObject_(hMask_Left)
DeleteObject_(hMask_Right)
DeleteObject_(hMask_Bottom)
SetWindowRgn_(hWnd, hMask_Top, 1)
DeleteObject_(hMask_Top)
EndProcedure