截图需要ImagePut.ahk库的支持
/*
https://github.com/iseahound/ImagePut
*/
if !FileExist("ImagePut.ahk"){
MsgBox,262160,Error,ImagePut.ahk文件不存在!,10
ExitApp
}
#Include ImagePut.ahk
Hotkey,$F1,CaptureScreen,On
TrayTip,提示:,F1键自由截图!,,1
Return
CaptureScreen:
#InstallMouseHook
TrayTip,截图提示,鼠标左键自由截图、右键取消!,,1
SetSystemCursor("IDC_Cross"),IsCapture:=0
WinSet, Disable ,, ahk_class Shell_TrayWnd
Loop
{
MouseGetPos,,,aid
WinActivate,ahk_id %aid%
WinSet, Disable ,,ahk_id %aid%
if (CSID<>aid&&aid&&CSID){
WinSet, Enable ,, ahk_id %CSID%
}
CSID:=aid
if GetKeyState("LButton","P"){
IsCapture:=1
Break
}Else if GetKeyState("RButton","P"){
RestoreCursors(),IsCapture:=0
WinSet, Enable ,, ahk_class Shell_TrayWnd
WinSet, Enable ,, ahk_id %CSID%
TrayTip,截图提示,操作已取消!,,2
Break
}
}
KeyWait,LButton,D
if IsCapture {
CoordMode,Mouse,Screen
MouseGetPos,xpos_init,ypos_init
Highlightline:=New HighlightOutline()
Loop
{
if !GetKeyState("LButton","P")||GetKeyState("RButton","P"){
RestoreCursors()
WinSet, Enable ,, ahk_class Shell_TrayWnd
WinSet, Enable ,, ahk_id %CSID%
Highlightline.Destroy()
ImagePutClipboard([xpos_init,ypos_init,Abs(xpos-xpos_init),Abs(ypos-ypos_init)])
TrayTip,截图提示,截图已保存至剪切板!,,1
Break
}Else if GetKeyState("LButton","P"){
MouseGetPos,xpos,ypos,id
Highlightline.Show(xpos_init,ypos_init,xpos,ypos)
Highlightline.setabove(id)
}
}
}
RestoreCursors()
WinSet, Enable ,, ahk_id %CSID%
WinSet, Enable ,, ahk_class Shell_TrayWnd
Return
RestoreCursors()
{
SPI_SETCURSORS := 0x57
DllCall( "SystemParametersInfo", UInt,SPI_SETCURSORS, UInt,0, UInt,0, UInt,0 )
}
/*
Cursor鼠标状态类型:
IDC_ARROW 常规箭头形
IDC_IBEAM "工"字形文本插入
IDC_WAIT 沙漏形
IDC_CROSS 十字形精确定位
IDC_UPARROW 上箭头
IDC_SIZE 隐藏指针
IDC_ICON 使用指定光标文件
IDC_SIZENWSE 左上角右下角大小调整
IDC_SIZENESW 左下角右上角大小调整
IDC_SIZEWE 水平调整
IDC_SIZENS 垂直调整
IDC_SIZEALL 选中并移动
IDC_NO 不可用
IDC_HAND 小手形状
IDC_APPSTARTING 系统忙
IDC_HELP 帮助
*/
SetSystemCursor( Cursor = "", cx = 0, cy = 0 )
{
BlankCursor := 0, SystemCursor := 0, FileCursor := 0 ; init
SystemCursors = 32512IDC_ARROW,32513IDC_IBEAM,32514IDC_WAIT,32515IDC_CROSS
,32516IDC_UPARROW,32640IDC_SIZE,32641IDC_ICON,32642IDC_SIZENWSE
,32643IDC_SIZENESW,32644IDC_SIZEWE,32645IDC_SIZENS,32646IDC_SIZEALL
,32648IDC_NO,32649IDC_HAND,32650IDC_APPSTARTING,32651IDC_HELP
If Cursor = ; empty, so create blank cursor
{
VarSetCapacity( AndMask, 32*4, 0xFF ), VarSetCapacity( XorMask, 32*4, 0 )
BlankCursor = 1 ; flag for later
}
Else If SubStr( Cursor,1,4 ) = "IDC_" ; load system cursor
{
Loop, Parse, SystemCursors, `,
{
CursorName := SubStr( A_Loopfield, 6, 15 ) ; get the cursor name, no trailing space with substr
CursorID := SubStr( A_Loopfield, 1, 5 ) ; get the cursor id
SystemCursor = 1
If ( CursorName = Cursor )
{
CursorHandle := DllCall( "LoadCursor", Uint,0, Int,CursorID )
Break
}
}
If CursorHandle = ; invalid cursor name given
{
Msgbox,, SetCursor, Error: Invalid cursor name
CursorHandle = Error
}
}
Else If FileExist( Cursor )
{
SplitPath, Cursor,,, Ext ; auto-detect type
If Ext = ico
uType := 0x1
Else If Ext in cur,ani
uType := 0x2
Else ; invalid file ext
{
Msgbox,, SetCursor, Error: Invalid file type
CursorHandle = Error
}
FileCursor = 1
}
Else
{
Msgbox,, SetCursor, Error: Invalid file path or cursor name
CursorHandle = Error ; raise for later
}
If CursorHandle != Error
{
Loop, Parse, SystemCursors, `,
{
If BlankCursor = 1
{
Type = BlankCursor
%Type%%A_Index% := DllCall( "CreateCursor"
, Uint,0, Int,0, Int,0, Int,32, Int,32, Uint,&AndMask, Uint,&XorMask )
CursorHandle := DllCall( "CopyImage", Uint,%Type%%A_Index%, Uint,0x2, Int,0, Int,0, Int,0 )
DllCall( "SetSystemCursor", Uint,CursorHandle, Int,SubStr( A_Loopfield, 1, 5 ) )
}
Else If SystemCursor = 1
{
Type = SystemCursor
CursorHandle := DllCall( "LoadCursor", Uint,0, Int,CursorID )
%Type%%A_Index% := DllCall( "CopyImage"
, Uint,CursorHandle, Uint,0x2, Int,cx, Int,cy, Uint,0 )
CursorHandle := DllCall( "CopyImage", Uint,%Type%%A_Index%, Uint,0x2, Int,0, Int,0, Int,0 )
DllCall( "SetSystemCursor", Uint,CursorHandle, Int,SubStr( A_Loopfield, 1, 5 ) )
}
Else If FileCursor = 1
{
Type = FileCursor
%Type%%A_Index% := DllCall( "LoadImageA"
, UInt,0, Str,Cursor, UInt,uType, Int,cx, Int,cy, UInt,0x10 )
DllCall( "SetSystemCursor", Uint,%Type%%A_Index%, Int,SubStr( A_Loopfield, 1, 5 ) )
}
}
}
}
class HighlightOutline {
__New(color="red") {
static WS_EX_TRANSPARENT := 0x20, WS_EX_LAYERED := 0x00080000
preDefaultGui := A_DefaultGui
this.hwnds := {}
Loop, 4 {
Gui, New, -Caption +ToolWindow HWNDhwnd -DPIScale +E%WS_EX_TRANSPARENT% +E%WS_EX_LAYERED%
Gui, Color, %color%
this[A_Index] := hwnd
DllCall("SetLayeredWindowAttributes", "ptr", hwnd, uint, 0, "uchar", 255, "int", 2)
}
this.isVisible := false
this.color := color
this.top := this[1]
this.right := this[2]
this.bottom := this[3]
this.left := this[4]
Gui, %preDefaultGui%: Default
}
MoveTogether(arrHwnd, arrPos) {
hDWP := DllCall("BeginDeferWindowPos", "int", arrHwnd.Length(), "ptr")
for i, v in arrPos {
DllCall("DeferWindowPos", "ptr", hDWP, "ptr", arrHwnd[i], "ptr", 0
, "int", v[1], "int", v[2], "int", v[3], "int", v[4]
, "uint", 0x54) ; SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOZORDER
}
DllCall("EndDeferWindowPos", "ptr", hDWP)
}
Show(x1, y1, x2, y2) {
b := g_isWindowDpiAware ? Round(2*(A_ScreenDPI/96)) : 2
arrHwnd := [this.1, this.2, this.3, this.4]
arrPos := [ [x1-b, y1-b, x2-x1+b*2, b]
, [x2, y1, b, y2-y1]
, [x1-b, y2, x2-x1+b*2, b]
, [x1-b, y1, b, y2-y1] ]
this.MoveTogether(arrHwnd, arrPos)
; this.GuiShow("top", x1-b, y1-b, x2-x1+b*2, b)
; this.GuiShow("right", x2, y1, b, y2-y1)
; this.GuiShow("bottom", x1-b, y2, x2-x1+b*2, b)
; this.GuiShow("left", x1-b, y1, b, y2-y1)
this.isVisible := true
}
GuiShow(name, x, y, w, h) {
Gui, % this[name] ":Show", % "NA x" x " y" y " w" w " h" h
}
Hide() {
Loop, 4
Gui, % this[A_Index] ": Hide"
this.isVisible := false
}
SetAbove(hwnd) {
ABOVE := DllCall("GetWindow", "ptr", hwnd, "uint", 3, "ptr")
Loop, 4
DllCall( "SetWindowPos", "ptr", this[A_Index], "ptr", ABOVE
, "int", 0, "int", 0, "int", 0, "int", 0
, "uint", 0x1|0x2|0x10 )
}
Transparent(param) {
Loop, 4
WinSet, Transparent, % param=1? 0:255, % "ahk_id" this[A_Index]
this.isVisible := !param
}
Color(color) {
Gui, +HWNDdefault
Loop, 4
Gui, % this[A_Index] ": Color" , %color%
this.color := color
Gui, %default%: Default
}
Destroy() {
Loop, 4
Gui, % this[A_Index] ": Destroy"
}
}
每天都来论坛学习下