Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active May 28, 2019 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmplinshi/8119956 to your computer and use it in GitHub Desktop.
Save tmplinshi/8119956 to your computer and use it in GitHub Desktop.
;=================================================================================================
; Class GuiBox
; AHK Version: 1.1.13.01
; Version: 1.00 / 2013-12-25 / tmplinshi
; dependency: Gdip.ahk 1.45 by tic (http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/)
; Thanks: nimda, gregster ...
;=================================================================================================
; Methods:
; new GuiBox( GuiOptions [, Color = "808080", Thickness = 2, Style = 1, Text = "", TextFont = "", TextOptions = ""] )
; Modify( GuiOptions [, Color = "808080", Thickness = 2, Style = 1, Text = "", TextFont = "", TextOptions = ""] )
; Show()
; Hide()
; AutoSize(a)
; Example:
; box1 := new GuiBox("x10 y30 w200 h200", "Green" , 5, 0 , "test" , "bold s20" )
; box2 := new GuiBox("xm w200 h200" , "0xF4AC5B", 5, [10,3], "`nline1`nline2", "cRed" , "-0x200")
; box1.Modify("w100", "Blue")
;=================================================================================================
Class GuiBox
{
static pToken := Gdip_Startup()
__New(GuiOptions, BoxOptions*)
{
Gui, Add, Text, %GuiOptions% 0xE hwndhBP
GuiControlGet, c, Pos, %hBP%
This.W := cW, This.H := cH, This.Hwnd := hBP
This.CreateBox(BoxOptions*)
}
;================================================================
; Parameters:
; Color: Box color
; Thickness: Box thickness
; Style:
; http://msdn.microsoft.com/en-us/library/windows/desktop/ms534104(v=vs.85).aspx
; 0 = DashStyleSolid
; 1 = DashStyleDash (default)
; 2 = DashStyleDot
; 3 = DashStyleDashDot
; 4 = DashStyleDashDotDot
; [ DashLength, DashSpace, DashLength, DashSpace, ... ]
; Text:
; TextFont: Options of "Gui, Font"
; TextOptions: Options of "Gui, Add, Text". Specify -0x200 to allow multiple line text.
;================================================================
CreateBox(Color = "808080", Thickness = 2, Style = 1, Text = "", TextFont = "", TextOptions = "")
{
static ColorName := { Black:"000000", Green:"008000", Silver:"C0C0C0", Lime:"00FF00", Gray:"808080", Olive:"808000", White:"FFFFFF", Yellow:"FFFF00", Maroon:"800000", Navy:"000080", Red:"FF0000", Blue:"0000FF", Purple:"800080", Teal:"008080", Fuchsia:"FF00FF", Aqua:"00FFF" }
This.LastOptions := [ Color, Thickness, Style, Text, TextFont, TextOptions ]
Color := ( ColorName[Color] = "" ) ? SubStr(Color, -5) : ColorName[Color]
This.pPen := Gdip_CreatePen( "0xFF" Color, Thickness )
if IsObject(Style)
{
VarSetCapacity(dashArray, Style.MaxIndex() * 4, 0)
Loop, % Style.MaxIndex()
NumPut( Style[A_Index], dashArray, (A_Index - 1) * 4, "Float" )
DllCall("Gdiplus.dll\GdipSetPenDashArray", "uint", This.pPen, "uint", &dashArray, "uint", Style.MaxIndex())
}
else
DllCall("Gdiplus.dll\GdipSetPenDashStyle", "uint", This.pPen, "Int", Style)
This.bBox := Gdip_CreateBitmap(This.W, This.H)
This.G := Gdip_GraphicsFromImage(This.bBox)
Gdip_SetInterpolationMode(This.G, 7)
Gdip_DrawRectangle(This.G, This.pPen, 1, 1, This.W-2, This.H-2)
This.hBox := Gdip_CreateHBitmapFromBitmap(This.bBox)
SetImage(This.Hwnd, This.hBox)
; Add or modify text
if (Text != "")
{
if This.hText
GuiControl,, % This.hText, % Text
else
{
Gui, Add, Text, xp wp hp BackgroundTrans Center 0x200 c%Color% %TextOptions% hwndhText, % Text
GuiControl, MoveDraw, % This.hText
This.hText := hText
}
if TextFont
{
Gui, Font, c%Color% %TextFont%
GuiControl, Font, % This.hText
}
}
}
AutoSize(arg*)
{
Critical
PS := This.__AutoXYWH(This.Hwnd, arg*)
if PS.W
{
Gdip_GraphicsClear(This.G)
This.bBox := Gdip_CreateBitmap(PS.W, PS.H)
This.G := Gdip_GraphicsFromImage(This.bBox)
Gdip_SetInterpolationMode(This.G, 7)
Gdip_DrawRectangle(This.G, This.pPen, 1, 1, PS.W-2, PS.H-2)
This.hBox := Gdip_CreateHBitmapFromBitmap(This.bBox)
SetImage(This.Hwnd, This.hBox)
if This.hText
GuiControl, MoveDraw, % This.hText, % "x" PS.X " y" PS.Y " w" PS.W " h" PS.H
}
Return PS
}
; Show() and Hide()
__Call(FunctionName)
{
if FunctionName in Show,Hide
{
GuiControl, % FunctionName, % This.Hwnd
if This.hText
GuiControl, % FunctionName, % This.hText
}
}
; Parameters: Same as new
Modify(GuiOptions, BoxOptions*)
{
if (GuiOptions != "")
{
GuiControl, Move, % This.Hwnd, % GuiOptions
GuiControlGet, c, Pos, % This.Hwnd
This.W := cW, This.H := cH, This.X := cX, This.Y := cY
}
For i, v in This.LastOptions
BoxOptions[A_Index] := ( BoxOptions[A_Index] = "" ) ? v : BoxOptions[A_Index]
This.CreateBox(BoxOptions*)
if This.hText
GuiControl, MoveDraw, % This.hText, % "x" This.X " y" This.Y " w" This.W " h" This.H
This.__AutoXYWH(This.Hwnd, "", "", True)
}
__AutoXYWH(ctrl, Attributes, Redraw = False, ResetInfo = False)
{
static cInfo := {}, New := []
if ResetInfo
{
cInfo[ctrl]._x := Attributes := ""
For Index, Value in cInfo[ctrl]._a
Attributes .= Value
}
else
This.GuiWidth := A_GuiWidth, This.GuiHeight := A_GuiHeight
if ( cInfo[ctrl]._x = "" )
{
GuiControlGet, i, Pos, %ctrl%
_x := This.GuiWidth - iX
_y := This.GuiHeight - iY
_w := This.GuiWidth - iW
_h := This.GuiHeight - iH
_a := RegExReplace(Attributes, "i)[^xywh]")
cInfo[ctrl] := { _x:(_x), _y:(_y), _w:(_w), _h:(_h), _a:StrSplit(_a) }
}
else
{
if ( cInfo[ctrl]._a.1 = "" )
Return
New.x := This.GuiWidth - cInfo[ctrl]._x
New.y := This.GuiHeight - cInfo[ctrl]._y
New.w := This.GuiWidth - cInfo[ctrl]._w
New.h := This.GuiHeight - cInfo[ctrl]._h
For Index, Value in cInfo[ctrl]._a
Options .= Value New[Value] A_Space
GuiControl, % Redraw ? "MoveDraw" : "Move", % ctrl, % Options
GuiControlGet, c, Pos, %ctrl%
This.W := cW, This.H := cH
Return { X:(cX), Y:(cY), W:(cW), H:(cH) }
}
}
__Delete()
{
Gdip_DeletePen(This.pPen)
Gdip_DeleteGraphics(This.G)
Gdip_DisposeImage(This.bBox)
DeleteObject(This.hBox)
Gdip_Shutdown(This.pToken)
}
}
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
#Include <Gdip>
#Include <Class_GuiBox>
Gui, +Resize
box1 := new GuiBox("w300 h100" , "Blue" , "5" , [10,3] , "AutoHotkey" , "bold s30")
box2 := new GuiBox("x110 w200 h30", "Green", "2" , 1 , "http://ahkscript.org", "cRed s12")
box3 := new GuiBox("xm w300 h60", "0xF39B23", "3", [20,2,1,1,1,1] )
Gui, Add, Button, xp+10 yp+10 w85 h40, Hide
Gui, Add, Button, x+10 wp hp , Show
Gui, Add, Button, x+10 wp hp , Modify
Gui, Show,, Sample
Return
ButtonShow:
box1.Show()
Return
ButtonHide:
box1.Hide()
Return
ButtonModify:
box2.Modify("x10 w300", "Gray", "2", 0, "http://ahkscript.com", "c0x45AFEF")
Return
GuiSize:
box1.AutoSize("w")
box2.AutoSize("x")
Return
GuiClose:
ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment