开始
如上次所说,我抽时间完成了ahk的gdip版取色工具,如下:
具体代码在下节。
代码
#Requires AutoHotkey v2.0
#Include g:\AHK\git-ahk-lib\lib\gdip\GdipStarter.ahk
#Include g:\AHK\git-ahk-lib\Extend.ahk
#Include g:\AHK\git-ahk-lib\util\Cursor.ahk
CoordMode 'Mouse'
CoordMode 'Pixel'
; config
hex := true, staticG := true
offsetX := 12, offsetY := 12, width := 128, height := 128
font := "consolas", fc := '#ffdbffd5'.substring(2)
pBrush := Gdip_BrushCreateSolid(0x8f000000)
pPenLine := Gdip_CreatePen(0x44a2e9cd, 1)
pPenbkBlack := Gdip_CreatePen(0xff000000, 1)
pPenbkWhite := Gdip_CreatePen(0xffffffff, 1)
_()
_() {
Cursor.SetIcon(Cursor.Icon.cross)
Gdip_FontFamilyCreate(Font)
pBitmap := Gdip_BitmapFromScreen()
, hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
, staticHdc := CreateCompatibleDC()
, staticObm := SelectObject(staticHdc, hBitmap)
gui_ := Gui('-Caption +AlwaysOnTop +ToolWindow +E0x00080000')
gui_.Show('NA')
global hex, flag
Hotkey('LButton Up', Done, 'On')
Hotkey('MButton', (*) => (flag := false, hex := !hex), 'On')
Hotkey('RButton Up', Exit, 'On')
Hotkey('Left', (*) => MouseMove(-1, 0, , 'R'), 'On')
Hotkey('Right', (*) => MouseMove(1, 0, , 'R'), 'On')
Hotkey('Up', (*) => MouseMove(0, -1, , 'R'), 'On')
Hotkey('Down', (*) => MouseMove(0, 1, , 'R'), 'On')
Hotkey('Esc', Exit, 'On')
Done(*) => (A_Clipboard := hex ? g_c : Format('rgb({})', g_c), Exit())
Exit(*) => (Clean(), ExitApp())
SetTimer(Start, 10)
Start() {
static o_mX := 0, o_mY := 0
MouseGetPos(&n_mX, &n_mY)
if n_mX = o_mX && n_mY = o_mY && flag
return
flag := true, o_mX := n_mX, o_mY := n_mY
hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight)
hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc), Gdip_SetSmoothingMode(G, 4)
if staticG ; static background
BitBlt(hdc, 0, 0, A_ScreenWidth, A_ScreenHeight, staticHdc, 0, 0)
_DrawEnlargementfiFrame(n_mX, n_mY)
UpdateLayeredWindow(gui_.Hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)
SelectObject(hdc, obm) DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteGraphics(G)
_DrawEnlargementfiFrame(mx, my) {
_offsetX := mx + offsetX + width > A_ScreenWidth ? -width : offsetX
_offsetY := my + offsetY + height + 20 > A_ScreenHeight ? -height - 20 : offsetY
_hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight)
_obm := SelectObject(_hdc := CreateCompatibleDC(), _hbm), _G := Gdip_GraphicsFromHDC(_hdc)
BitBlt(_hdc, 0, 0, A_ScreenWidth, A_ScreenHeight, staticHdc, 0, 0)
Gdip_DrawLine(_G, pPenLine, mx, my - 1, mx, my - 10)
Gdip_DrawLine(_G, pPenLine, mx, my + 1, mx, my + 10)
Gdip_DrawLine(_G, pPenLine, mx - 1, my, mx - 10, my)
Gdip_DrawLine(_G, pPenLine, mx + 1, my, mx + 10, my)
_x := mx + _offsetX, _y := my + _offsetY, cx := _x + width // 2, cy := _y + height // 2
StretchBlt(hdc, _x, _y, width + 2, height + 2, _hdc, mx - 9, my - 9, 19, 19 * (height // width))
Gdip_DrawRoundedRectangle(G, pPenbkBlack, _x, _y, width + 2, height + 2, 0)
Gdip_DrawRoundedRectangle(G, pPenbkWhite, _x + 1, _y + 1, width, height, 0)
Gdip_DrawRoundedRectangle(G, pPenbkBlack, cx - 4, cy - 4, 9, 9, 0)
Gdip_DrawRoundedRectangle(G, pPenbkWhite, cx - 3, cy - 3, 7, 7, 0)
_DrawTip()
SelectObject(_hdc, _obm), DeleteObject(_hbm), DeleteDC(_hdc), Gdip_DeleteGraphics(_G)
_DrawTip() {
local x := _x + 1, y := _y + height + 3
global g_c
Gdip_FillRoundedRectangle(G, pBrush, _x, y - 1, width + 2, 22, 0) ; background
_c := '0xff' (hexC := PixelGetColor(Cursor.x, Cursor.y, 'slow').substring(3))
Gdip_FillRectangle(G, _b := Gdip_BrushCreateSolid(_c), x, y, 20, 20) ; color box
Gdip_DrawRoundedRectangle(G, pPenbkWhite, x, y, 20, 20, 0)
options := Format('x{} y{} c{} Center s15', x + 15, y + 2, fc)
Gdip_TextToGraphics(G, (g_c := hex ? '#' hexC : _hexToRGB(hexC)), options, font, width - 20, 30)
Gdip_DeleteBrush(_b)
_hexToRGB(_c) {
local r, g, b
if _c.length = 3
_c := _c[0] + _c[0] + _c[1] + _c[1] + _c[2] + _c[2]
r := ('0x' _c.substring(1, 3)) & 0xFF
g := ('0x' _c.substring(3, 5)) & 0xFF
b := ('0x' _c.substring(5)) & 0xFF
return JoinStr(',', r, g, b)
}
}
}
}
Clean() {
; HotKeysOff('LButton Up', 'RButton Up', 'Esc')
SetTimer(Start, 0)
Gdip_DeletePen(pPenLine)
Gdip_DeletePen(pPenbkBlack), Gdip_DeleteBrush(pBrush)
SelectObject(staticHdc, staticObm), DeleteDC(staticHdc), DeleteObject(hBitmap)
}
}
说明
这里使用了
gdi
库,以及自己编写的一些扩展脚本(在仓库中)。
脚本开头是一些配置:
- hex #xx形式或rgb()形式
- staticG 是否显示静态背景
然后是取色显示框的偏移和大小,注意大小最好是偶数,且最好是正方形。
因为我懒了,非正方形会拉伸图像,要解决这点需要修改74行StretchBlt
,前几个参数要动态计算。
最后是字体,以及画笔和笔刷的颜色,可以自由调整。
标签:AHK2,mx,width,取色,hdc,工具,height,my,Gdip From: https://www.cnblogs.com/refiz/p/18286452