Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SingleInstance, Force
- #NoEnv
- SetBatchLines, -1
- if !pToken := Gdip_Startup()
- {
- MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
- ExitApp
- }
- OnExit, Exit
- Width := 600
- Height := 500
- Gui, 1: +E0x80000 +LastFound
- Gui, 1: Show
- hwnd1 := WinExist()
- hbm := CreateDIBSection(Width, Height)
- hdc := CreateCompatibleDC()
- obm := SelectObject(hdc, hbm)
- G := Gdip_GraphicsFromHDC(hdc)
- pBrush := Gdip_BrushCreateSolid(0xFFFF0000)
- Gdip_FillRectangle(G, pBrush, 0, 0, 600, 500)
- Gdip_DeleteBrush(pBrush)
- pBrush := Gdip_BrushCreateSolid(0xFFFFFFFF)
- Gdip_FillEllipse(G, pBrush, 5, 420, 75, 75)
- Gdip_FillEllipse(G, pBrush, 520, 420, 75, 75)
- Gdip_DeleteBrush(pBrush)
- pBrush := Gdip_BrushCreateSolid(0xFFC4C4C4)
- Gdip_FillRectangle(G, pBrush, 75, 75, 450, 350)
- Gdip_DeleteBrush(pBrush)
- Gdip_TextToGraphics(G, "AHK Sketch", "w450 h75 x80 y10 s40 Center", "Rondo")
- UpdateLayeredWindow(hwnd1, hdc, A_ScreenWidth/4+Width/8, A_ScreenHeight/4, Width, Height)
- MyPen := Gdip_CreatePen("0xFF000000", 5)
- xPos := BackupX := 300
- yPos := BackupY := 250
- return
- #If WinActive("ahk_id " hwnd1)
- a::
- d::
- s::
- w::
- #If
- dir := (A_ThisHotkey = "s" || A_ThisHotkey = "w") ? "x" : "y"
- xdir := (A_ThisHotkey = "s" || A_ThisHotkey = "w") ? "y" : "x"
- Backup%dir% := %dir%pos
- %xdir%Pos += (A_ThisHotkey = "d" || A_ThisHotkey = "a") ? ((A_ThisHotkey = "d") ? (xPos > 520 ? 0 : 5) : (xPos < 80 ? 0 : -5)) : ((A_ThisHotkey = "s") ? (yPos > 420 ? 0 : 5) : (yPos < 80 ? 0 : -5))
- Gdip_DrawLine(G, MyPen, BackupX, BackupY, XPos, YPos)
- UpdateLayeredWindow(hwnd1, hdc, A_ScreenWidth/4+Width/8, A_ScreenHeight/4, Width, Height)
- return
- Esc::
- Exit:
- Gdip_Shutdown(pToken)
- ExitApp
- return
- Gdip_Startup()
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
- DllCall("LoadLibrary", "str", "gdiplus")
- VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
- DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
- return pToken
- }
- CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- hdc2 := hdc ? hdc : GetDC()
- VarSetCapacity(bi, 40, 0)
- NumPut(w, bi, 4, "uint")
- , NumPut(h, bi, 8, "uint")
- , NumPut(40, bi, 0, "uint")
- , NumPut(1, bi, 12, "ushort")
- , NumPut(0, bi, 16, "uInt")
- , NumPut(bpp, bi, 14, "ushort")
- hbm := DllCall("CreateDIBSection"
- , Ptr, hdc2
- , Ptr, &bi
- , "uint", 0
- , A_PtrSize ? "UPtr*" : "uint*", ppvBits
- , Ptr, 0
- , "uint", 0, Ptr)
- if !hdc
- ReleaseDC(hdc2)
- return hbm
- }
- CreateCompatibleDC(hdc=0)
- {
- return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
- }
- SelectObject(hdc, hgdiobj)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
- }
- Gdip_GraphicsFromHDC(hdc)
- {
- DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
- return pGraphics
- }
- Gdip_BrushCreateSolid(ARGB=0xff000000)
- {
- DllCall("gdiplus\GdipCreateSolidFill", "UInt", ARGB, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
- return pBrush
- }
- Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- return DllCall("gdiplus\GdipFillRectangle"
- , Ptr, pGraphics
- , Ptr, pBrush
- , "float", x
- , "float", y
- , "float", w
- , "float", h)
- }
- Gdip_FillEllipse(pGraphics, pBrush, x, y, w, h)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- return DllCall("gdiplus\GdipFillEllipse", Ptr, pGraphics, Ptr, pBrush, "float", x, "float", y, "float", w, "float", h)
- }
- Gdip_DeleteBrush(pBrush)
- {
- return DllCall("gdiplus\GdipDeleteBrush", A_PtrSize ? "UPtr" : "UInt", pBrush)
- }
- Gdip_DrawLine(pGraphics, pPen, x1, y1, x2, y2)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- return DllCall("gdiplus\GdipDrawLine"
- , Ptr, pGraphics
- , Ptr, pPen
- , "float", x1
- , "float", y1
- , "float", x2
- , "float", y2)
- }
- Gdip_CloneBrush(pBrush)
- {
- DllCall("gdiplus\GdipCloneBrush", A_PtrSize ? "UPtr" : "UInt", pBrush, A_PtrSize ? "UPtr*" : "UInt*", pBrushClone)
- return pBrushClone
- }
- Gdip_FontFamilyCreate(Font)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- if (!A_IsUnicode)
- {
- nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, "uint", 0, "int", 0)
- VarSetCapacity(wFont, nSize*2)
- DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, Ptr, &wFont, "int", nSize)
- }
- DllCall("gdiplus\GdipCreateFontFamilyFromName"
- , Ptr, A_IsUnicode ? &Font : &wFont
- , "uint", 0
- , A_PtrSize ? "UPtr*" : "UInt*", hFamily)
- return hFamily
- }
- Gdip_FontCreate(hFamily, Size, Style=0)
- {
- DllCall("gdiplus\GdipCreateFont", A_PtrSize ? "UPtr" : "UInt", hFamily, "float", Size, "int", Style, "int", 0, A_PtrSize ? "UPtr*" : "UInt*", hFont)
- return hFont
- }
- Gdip_StringFormatCreate(Format=0, Lang=0)
- {
- DllCall("gdiplus\GdipCreateStringFormat", "int", Format, "int", Lang, A_PtrSize ? "UPtr*" : "UInt*", hFormat)
- return hFormat
- }
- CreateRectF(ByRef RectF, x, y, w, h)
- {
- VarSetCapacity(RectF, 16)
- NumPut(x, RectF, 0, "float"), NumPut(y, RectF, 4, "float"), NumPut(w, RectF, 8, "float"), NumPut(h, RectF, 12, "float")
- }
- Gdip_SetStringFormatAlign(hFormat, Align)
- {
- return DllCall("gdiplus\GdipSetStringFormatAlign", A_PtrSize ? "UPtr" : "UInt", hFormat, "int", Align)
- }
- Gdip_TextToGraphics(pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0)
- {
- IWidth := Width, IHeight:= Height
- RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
- RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
- RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
- RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
- RegExMatch(Options, "i)C(?!(entre|enter))([a-f\d]+)", Colour)
- RegExMatch(Options, "i)Top|Up|Bottom|Down|vCentre|vCenter", vPos)
- RegExMatch(Options, "i)NoWrap", NoWrap)
- RegExMatch(Options, "i)R(\d)", Rendering)
- RegExMatch(Options, "i)S(\d+)(p*)", Size)
- if !Gdip_DeleteBrush(Gdip_CloneBrush(Colour2))
- PassBrush := 1, pBrush := Colour2
- if !(IWidth && IHeight) && (xpos2 || ypos2 || Width2 || Height2 || Size2)
- return -1
- Style := 0, Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
- Loop, Parse, Styles, |
- {
- if RegExMatch(Options, "\b" A_loopField)
- Style |= (A_LoopField != "StrikeOut") ? (A_Index-1) : 8
- }
- Align := 0, Alignments := "Near|Left|Centre|Center|Far|Right"
- Loop, Parse, Alignments, |
- {
- if RegExMatch(Options, "\b" A_loopField)
- Align |= A_Index//2.1 ; 0|0|1|1|2|2
- }
- xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
- ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
- Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
- Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
- if !PassBrush
- Colour := "0x" (Colour2 ? Colour2 : "ff000000")
- Rendering := ((Rendering1 >= 0) && (Rendering1 <= 5)) ? Rendering1 : 4
- Size := (Size1 > 0) ? Size2 ? IHeight*(Size1/100) : Size1 : 12
- hFamily := Gdip_FontFamilyCreate(Font)
- hFont := Gdip_FontCreate(hFamily, Size, Style)
- FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
- hFormat := Gdip_StringFormatCreate(FormatStyle)
- pBrush := PassBrush ? pBrush : Gdip_BrushCreateSolid(Colour)
- if !(hFamily && hFont && hFormat && pBrush && pGraphics)
- return !pGraphics ? -2 : !hFamily ? -3 : !hFont ? -4 : !hFormat ? -5 : !pBrush ? -6 : 0
- CreateRectF(RC, xpos, ypos, Width, Height)
- Gdip_SetStringFormatAlign(hFormat, Align)
- Gdip_SetTextRenderingHint(pGraphics, Rendering)
- ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
- if vPos
- {
- StringSplit, ReturnRC, ReturnRC, |
- if (vPos = "vCentre") || (vPos = "vCenter")
- ypos += (Height-ReturnRC4)//2
- else if (vPos = "Top") || (vPos = "Up")
- ypos := 0
- else if (vPos = "Bottom") || (vPos = "Down")
- ypos := Height-ReturnRC4
- CreateRectF(RC, xpos, ypos, Width, ReturnRC4)
- ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
- }
- if !Measure
- E := Gdip_DrawString(pGraphics, Text, hFont, hFormat, pBrush, RC)
- if !PassBrush
- Gdip_DeleteBrush(pBrush)
- Gdip_DeleteStringFormat(hFormat)
- Gdip_DeleteFont(hFont)
- Gdip_DeleteFontFamily(hFamily)
- return E ? E : ReturnRC
- }
- Gdip_DeleteFont(hFont)
- {
- return DllCall("gdiplus\GdipDeleteFont", A_PtrSize ? "UPtr" : "UInt", hFont)
- }
- Gdip_DeleteFontFamily(hFamily)
- {
- return DllCall("gdiplus\GdipDeleteFontFamily", A_PtrSize ? "UPtr" : "UInt", hFamily)
- }
- Gdip_DeleteStringFormat(hFormat)
- {
- return DllCall("gdiplus\GdipDeleteStringFormat", A_PtrSize ? "UPtr" : "UInt", hFormat)
- }
- Gdip_DrawString(pGraphics, sString, hFont, hFormat, pBrush, ByRef RectF)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- if (!A_IsUnicode)
- {
- nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, 0, "int", 0)
- VarSetCapacity(wString, nSize*2)
- DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
- }
- return DllCall("gdiplus\GdipDrawString"
- , Ptr, pGraphics
- , Ptr, A_IsUnicode ? &sString : &wString
- , "int", -1
- , Ptr, hFont
- , Ptr, &RectF
- , Ptr, hFormat
- , Ptr, pBrush)
- }
- Gdip_MeasureString(pGraphics, sString, hFont, hFormat, ByRef RectF)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- VarSetCapacity(RC, 16)
- if !A_IsUnicode
- {
- nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, "uint", 0, "int", 0)
- VarSetCapacity(wString, nSize*2)
- DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
- }
- DllCall("gdiplus\GdipMeasureString"
- , Ptr, pGraphics
- , Ptr, A_IsUnicode ? &sString : &wString
- , "int", -1
- , Ptr, hFont
- , Ptr, &RectF
- , Ptr, hFormat
- , Ptr, &RC
- , "uint*", Chars
- , "uint*", Lines)
- return &RC ? NumGet(RC, 0, "float") "|" NumGet(RC, 4, "float") "|" NumGet(RC, 8, "float") "|" NumGet(RC, 12, "float") "|" Chars "|" Lines : 0
- }
- Gdip_SetTextRenderingHint(pGraphics, RenderingHint)
- {
- return DllCall("gdiplus\GdipSetTextRenderingHint", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", RenderingHint)
- }
- UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- if ((x != "") && (y != ""))
- VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
- if (w = "") ||(h = "")
- WinGetPos,,, w, h, ahk_id %hwnd%
- return DllCall("UpdateLayeredWindow"
- , Ptr, hwnd
- , Ptr, 0
- , Ptr, ((x = "") && (y = "")) ? 0 : &pt
- , "int64*", w|h<<32
- , Ptr, hdc
- , "int64*", 0
- , "uint", 0
- , "UInt*", Alpha<<16|1<<24
- , "uint", 2)
- }
- Gdip_CreatePen(ARGB, w)
- {
- DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
- return pPen
- }
- Gdip_Shutdown(pToken)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
- if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
- DllCall("FreeLibrary", Ptr, hModule)
- return 0
- }
- GetDC(hwnd=0)
- {
- return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
- }
- ReleaseDC(hdc, hwnd=0)
- {
- Ptr := A_PtrSize ? "UPtr" : "UInt"
- return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
- }
Add Comment
Please, Sign In to add comment