Create a function DrawLine - GUI Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Create a function DrawLine - GUI

23 Jan 2024, 16:39

Hi!

How to create lines or outlines in V2?


In AHK v1 I often used a function to draw a line or an outline in GUI:s
In the call to the function(), it is specified which GUI the line belongs to (if several GUIs exist)
and coordinates etc.

This is some code from AHK v1 (use progressbar)

Code: Select all

GUI
...
DrawLine( NameGui := 1, x := 70, y := 55, w := 600, h := 4, Color := 0xFFA500 )	; Horoisontell linje - orange
DrawLine( NameGui := 1, x := 320, y := 55, w := 4, h := 620, Color := 0xFFA500 ) ; Vertikal linje - orange
...
; DrawOutline( Gui, x, y, w, h, Color1, Color2, Thickness )
DrawOutline( 1, 360, 160, 290, 310, "Green", "Green", 2 )
...
...	
; Draw a line
DrawLine( NameGUI, x, y, w, h, Color )
{	GUI , % NameGUI ": Add" , Progress , % "X" X " Y" Y " W" W " H" H " Background" Color 
}

; Draw a borderline as rectangle or square
DrawOutline( NameGUI, x, y, w, h, Color1 := "Black", Color2 := "Black", Thickness := 1 )
{	GUI , % NameGUI ": Add" , Progress , % "x" x " y" y " w" w " h" Thickness " Background" Color1 
	GUI , % NameGUI ": Add" , Progress , % "x" x " y" y " w" Thickness " h" h " Background" Color1 
	GUI , % NameGUI ": Add" , Progress , % "x" x " y" y + h - Thickness " w" w " h" Thickness " Background" Color2 
	GUI , % NameGUI ": Add" , Progress , % "x" x + w - Thickness " y" y " w" Thickness " h" h " Background" Color2 	
}
There are more better/sophisticated ways (but the code needs to be translated to AHK v2)
Draw a colored outline on a GUI - AHK v1
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Create a function DrawLine - GUI

24 Jan 2024, 06:32

Code: Select all

GuiBox := GuiRectangle()
GuiBox.MovePos(10, 10, 300, 300)
GuiBox.Show()

GuiRectangle(x:= 0, y:= 0 ,w:= 100 ,h:=100 , Color:="Blue",Thickness := 2){
    Static GuiBox := "" 
    if IsObject(GuiBox){
        try GuiBox.Destroy()
    }
    GuiBox := Gui(" +ToolWindow -Caption +AlwaysOnTop +E0x20 -DPIScale", "Highlight")
    GuiBox.x := x
    GuiBox.y := y
    GuiBox.w := w
    GuiBox.h := h
    GuiBox.Thickness := Thickness

    if (Thickness <0){
        Thickness:= -Thickness
        x := x-Thickness
        y := y-Thickness
        w := w+Thickness*2
        h := h+Thickness*2
    }
    GuiBox.MarginX := 0
    GuiBox.MarginY := 0
    goColor := GuiBox.AddText("w" w " h" h " Background" Color)
    goTransp := GuiBox.AddText("x" Thickness " y" Thickness " w" w-Thickness*2 " h" h-Thickness*2 " BackgroundEEAA99")
    WinSetTransColor("EEAA99", GuiBox)
    
    GuiBox.SetColor := SetColor
    GuiBox.SetThickness := SetThickness
    GuiBox.MovePos := MovePos
    GuiBox.MoveToControl := MoveToControl
    GuiBox.MoveToWindow := MoveToWindow
    GuiBox.Show("Hide x" x " y" y)
    
    return GuiBox

    ; Set the color
    SetColor(GuiBox, Color := "Blue"){
        goColor.Opt(" +Background" Color)
        goColor.Redraw()
    }

    ; Set the Thickness (simple function)
    SetThickness(GuiBox, Thickness := 1){
        MovePos(GuiBox, , , , , Thickness)
    }

    ; Change the position of the gui without destroying it
    MovePos(GuiBox, x:="", y:="", w:="", h:="",Thickness:=""){
        x := x=""? GuiBox.x : x
        y := y=""? GuiBox.y : y
        w := w=""? GuiBox.w : w
        h := h=""? GuiBox.h : h
        Thickness:= Thickness=""? GuiBox.Thickness : Thickness

        GuiBox.x := x
        GuiBox.y := y
        GuiBox.w := w
        GuiBox.h := h
        GuiBox.Thickness := Thickness

        if (Thickness < 0) {
            Thickness := -Thickness
            x := x - Thickness
            y := y - Thickness
            w := w + Thickness * 2
            h := h + Thickness * 2
        }

        GuiBox.Move(x, y, w, h)
        goColor.Move(,,w,h)
        goTransp.Move(Thickness, Thickness, w-Thickness*2, h-Thickness*2)
        goColor.Redraw()
        goTransp.Redraw()
        
    }

    ; Set the rectangle arround a control
    MoveToControl(GuiBox,Control,Wintitle){
        Try{
            ControlGetPos(&X, &Y, &W, &H, Control, WinTitle)
            WinGetClientPos(&winX, &winY,,, WinTitle)
            MovePos(GuiBox, winX+x, winY+y, w, h)
        } Catch{
            GuiBox.Hide()
        }
    }

    ; Set the rectangle arround a control
    MoveToWindow(GuiBox,Wintitle){
        try{
            WinGetClientPos(&winX, &winY, &winW, &winH, WinTitle)
            if (winY=-8){
                winX:=winX+8
                winY:=winY+8
                winW:=winW-8*2
                winH:=winH-8*2

            }
            MovePos(GuiBox, winX, winY, winW, winH) ; Strangly, WinGetPos returned slightly offset values
        } Catch {
            GuiBox.Hide()
        }
    }
}
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Create a function DrawLine - GUI

24 Jan 2024, 07:31

Thank you! (Nice solution)
Didn't see the result right away

But is it hard to draw this rectangle on a GUI window? (if I understand correctly the rectangle will be a separate GUI)
Is it easiest to use the same function to draw a line? (width or height = 0)
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Create a function DrawLine - GUI

24 Jan 2024, 13:45

To simply draw a line on a Gui, just use an empty text with a background color.

Code: Select all

MyGui.AddText("w100 h2 BackgroundBlack")
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Create a function DrawLine - GUI

25 Jan 2024, 19:54

Thank you!

This script can .:
create a red rectangle
My desire is to create a function() that calculates the values that the rectangle should have. (more easy to handle)
The function call would be something like this .:

Code: Select all

DrawLine("MyGui", 40, 100, 100, 6, "Red")	; DrawLine( NameGUI, x, y, w, h, Color )
How to create the function in AHK v2?
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Create a function DrawLine - GUI

26 Jan 2024, 14:08

That is easy:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force

MyGui := Gui(, 'Test Draw a line v1')

MyGui.SetFont('cGreen bold underline s13', 'Verdana')
MyGui.AddText('x15 y10 w300 h20 center', 'Draw a line / Rectangle')

AddRectangle(MyGui, 50 , 100, 100, 100,"Blue",10)

MyGui.Show("x200 y200 h400 w400")


AddRectangle(MyGui, x1 , y1, w, h, Color:="Red", t:=2){
    MyGui.AddText("x" x1 " y" y1 " w" w " h" t " Background" Color)
    MyGui.AddText("x" x1 " y" y1 " w" t " h" h " Background" Color)
    MyGui.AddText("x" x1 " y" y1+h-t " w" w " h" t " Background" Color)
    MyGui.AddText("x" x1+w-t " y" y1 " w" t " h" w " Background" Color)
}
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Create a function DrawLine - GUI

27 Jan 2024, 02:57

Thank you!
(it works as square, but not as rectangle)
must analyze which of the parameters is incorrect. (I have no time right now)

eg. This rectangle has not the correct shape .:

Code: Select all

AddRectangle(MyGui, 50 , 100, 50, 25, "Red", 2)
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Create a function DrawLine - GUI  Topic is solved

27 Jan 2024, 03:16

Last line of the function should be this:

Code: Select all

    MyGui.AddText("x" x1+w-t " y" y1 " w" t " h" h " Background" Color)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken and 27 guests