Draggable GUI with Controls!

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Draggable GUI with Controls!

01 Oct 2013, 18:04

The so-called "goyyah" trick has been around for quite a while now... (really cool by the way! :P )
The only problem is that it renders other controls "unclickable"?? eg. Scrollbars...
 
I don't know if workarounds have posted before...so here it is anyways! :D
 
The Function
The basic function is this... (you could call it a wierd glitch :P )

Code: Select all

enableGuiDrag(GuiLabel=1) {
	WinGetPos,,,A_w,A_h,A
	Gui, %GuiLabel%:Add, Text, x0 y0 w%A_w% h%A_h% +BackgroundTrans gGUI_Drag
	return
	
	GUI_Drag:
	SendMessage 0xA1,2  ;-- Goyyah/SKAN trick
	;http://autohotkey.com/board/topic/80594-how-to-enable-drag-for-a-gui-without-a-titlebar
	return
}
Usage
Example script...

Code: Select all

pic=%A_temp%\pic.png
URLDownloadToFile,http://cdn.autohotkey.com/static/ahk_logo_ipb.png,%pic%
ltext:=lines(100)

Gui, Add, CheckBox, x12 y10 w80 h30 , CheckBox
Gui, Add, Radio, x12 y40 w80 h30 , Radio
Gui, Add, Edit, x2 y70 w90 h30 , Edit
Gui, Add, GroupBox, x92 y10 w100 h90 , GroupBox
Gui, Add, DropDownList, x102 y30 w80 h20 , DropDownList
Gui, Add, ComboBox, x102 y60 w80 h20 , ComboBox
Gui, Add, Progress, x192 y10 w100 h30 , 25
Gui, Add, ListBox, x192 y40 w100 h60 , ListBox
Gui, Add, ListView, x2 y100 w190 h80 , ListView
Gui, Add, DateTime, x192 y130 w100 h30 , 
Gui, Add, MonthCal, x2 y180 w230 h170 , 
Gui, Add, Slider, x192 y100 w100 h30 , 25
Gui, Add, Hotkey, x192 y160 w100 h20 , 
Gui, Add, UpDown, x232 y180 w20 h170 , UpDown
Gui, Add, Picture, x312 y10 w150 h40 , %pic%
Gui, Add, Picture, x312 y60 w150 h44 , %pic%
Gui, Add, Picture, x312 y120 w150 h44 , %pic%
;Tab2 doesnt work for some reason... Solution by Nazzal below...
Gui, Add, Tab, x252 y180 w220 h170 , Tab1|Tab2
gui, Add, Link,, <a href="http://www.Autohotkey.com">hello`, this is a link. Autohotkey.com</a>
Gui, Add, Edit, w200 h80, Scrollable Edit Control`n%ltext%
Gui, Show, w479 h358, Draggable GUI with Controls

enableGuiDrag()
return

GuiClose:
ExitApp

lines(numberoflines) {
	z=
	loop, %numberoflines%
		z=%z%Line%A_Index%`n
	StringTrimRight,z,z,1
	return z
}

enableGuiDrag(GuiLabel=1) {
	WinGetPos,,,A_w,A_h,A
	Gui, %GuiLabel%:Add, Text, x0 y0 w%A_w% h%A_h% +BackgroundTrans gGUI_Drag
	return
	
	GUI_Drag:
	PostMessage 0xA1,2  ;-- Goyyah/SKAN trick
	;http://autohotkey.com/board/topic/80594-how-to-enable-drag-for-a-gui-without-a-titlebar
	return
}
 
:) cheers! Comments are welcome! (suggestions, better versions, issues...)
 
Known issue: tabs don't show...  Solution by Nazzal

Nazzal's Implementation:

Code: Select all

#SingleInstance force

Gui, Main: +HWNDhMain
Gui, Main: Add, Tab, w400 h200, 1|2
OnMessage(0x0201, "WM_LBUTTONDOWN")
Gui, Main: Show,,WM_LBUTTONDOWN Drag
Return

WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {
    global hMain
    If (hwnd = hMain)
        DllCall("PostMessage", "uint", hWnd, "uint", 0x00A1, "uint", 2, "uint", 0)
    }

MainGuiClose:
    ExitApp
Last edited by joedf on 01 Oct 2013, 19:50, edited 1 time in total.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Draggable GUI with Controls!

01 Oct 2013, 19:07

Use SendMessage instead of PostMessage.
With PostMessage, I have seen ( very rarely ) that next line executes before mouse capture ends.
My Scripts and Functions: V1  V2
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Draggable GUI with Controls!

01 Oct 2013, 19:50

SKAN wrote:Use SendMessage instead of PostMessage.
With PostMessage, I have seen ( very rarely ) that next line executes before mouse capture ends.
ahh! I see, because PostMessage is Asynchronous. http://stackoverflow.com/a/3376637/883015
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: Draggable GUI with Controls!

12 May 2015, 11:00

So I may have been randomly looking for this and the first attempt didn't work, however Nazzal's Implementation worked perfectly! I know I didn't "really" need it but I wanted it lol.. Thank you :)
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Draggable GUI with Controls!

14 Feb 2022, 22:07

Can you help me make my GUI draggable:

Code: Select all

#SingleInstance Force

FormatTime, var0,, h:mm:ss tt

Gui, +AlwaysOnTop +ToolWindow -SysMenu -Caption
Gui, Color, Teal
Gui, Font, cFF0000 s20 w1000, Curlz MT
Gui, Add, Text, y0 vD, % var0
Gui, Show, NoActivate, uptime
WinSet, TransColor, CCCCCC 255,uptime
SetTimer, RefreshD, 1000
return

RefreshD:
    FormatTime, var0,, h:mm:ss tt
    GuiControl,, D, % var0
return

NatSuf(n) ; by VxE
{
    return Mod(n + 9, 10) >= 3 || Mod(n + 89, 100) < 3 ? "th"
    : Mod(n, 10) = 1 ? "st" : Mod(n, 10) = 2 ? "nd" : "rd"
}

Esc::ExitApp
Might you also know how to stop the flashing AM and PM?
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Draggable GUI with Controls!

15 Feb 2022, 09:53

I just inserted in there for you. Not tested, but should work.

Code: Select all

#SingleInstance Force

FormatTime, var0,, h:mm:ss tt

Gui, +AlwaysOnTop +ToolWindow -SysMenu -Caption
Gui, Color, Teal
Gui, Font, cFF0000 s20 w1000, Curlz MT
Gui, Add, Text, y0 vD, % var0
Gui, Show, NoActivate, uptime
WinSet, TransColor, CCCCCC 255,uptime

enableGuiDrag()

SetTimer, RefreshD, 1000
return

RefreshD:
    FormatTime, var0,, h:mm:ss tt
    GuiControl,, D, % var0
return

NatSuf(n) ; by VxE
{
    return Mod(n + 9, 10) >= 3 || Mod(n + 89, 100) < 3 ? "th"
    : Mod(n, 10) = 1 ? "st" : Mod(n, 10) = 2 ? "nd" : "rd"
}

enableGuiDrag(GuiLabel=1) {
	WinGetPos,,,A_w,A_h,A
	Gui, %GuiLabel%:Add, Text, x0 y0 w%A_w% h%A_h% +BackgroundTrans gGUI_Drag
	return
	
	GUI_Drag:
	SendMessage 0xA1,2  ;-- Goyyah/SKAN trick
	;http://autohotkey.com/board/topic/80594-how-to-enable-drag-for-a-gui-without-a-titlebar
	return
}

Esc::ExitApp
As for the flashing, there's a few things you can do...
Have it in its own seperate label that you only update when needed.
And you can try adding the following to the begging of you script:

Code: Select all

#NoEnv
SetBatchLines -1
SetWinDelay -1
;GUI Double buffering
Gui, +E0x02000000 +E0x00080000 ; WS_EX_COMPOSITED & WS_EX_LAYERED
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Draggable GUI with Controls!

15 Feb 2022, 09:58

Thank you @joedf, works great!
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Draggable GUI with Controls!

15 Feb 2022, 16:27

LAPIII wrote:
15 Feb 2022, 09:58
Thank you @joedf, works great!
Glad I could help! :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Draggable GUI with Controls!

16 Feb 2022, 23:25

It turns out that didn't work. What I did instead of using your snippet was add GuiWidth := 900 and delete the tt on the lines with FormatTime. Would you know how to make this draggable:

Code: Select all

; Set up environment
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force

; Define your variables
GuiWidth := 630 ; adjust to fit your desired font - will be trickier with proportional fonts
; The positions below fit my particular needs.  You can use fixed x and y rather than calculating
ClockPosX := (A_ScreenWidth - GuiWidth) / 2
ClockPosY := (A_ScreenHeight - 180)
OldTime := ""
CustomColor := "000000"  ; Can be any RGB color , but I found this prevents character shadowing the best
TextColor := "237DF9"
FontName := "Curlz MT"
;FontName := "7-Segment"
FontSize := 60

; Set up your GUI
Gui +LastFound +AlwaysOnTop -Caption +ToolWindow  ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
Gui, Color, %CustomColor%
Gui, Font, s%FontSize%, %FontName%
Gui, Add, Text, vMyTime c%TextColor%, XX/XX/XX   XX:XX  ; XX:XX:XX serves to auto-size the window.
; Make all pixels of this color transparent and make the text itself translucent (150):
WinSet, TransColor, %CustomColor% 255
SetTimer, UpdateClock, 1000
Gosub, UpdateClock  ; Make the first update immediate rather than waiting for the timer.

; Display the clock
; NoActivate avoids deactivating the currently active window.
Gui, Show, x%ClockPosX% y%ClockPosY% w%GuiWidth% NoActivate  
return

UpdateClock:
FormatTime, CurTime,, hh:mm:ss
If (CurTime != OldTime) {
    OldTime := CurTime
    GuiControl,, MyTime, %CurTime%
}
return
Esc::ExitApp

I tried making this draggable. If you change %CustomColor% to a color then you can see the background.
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Draggable GUI with Controls!

17 Feb 2022, 10:14

Sure, you just to copy the function somewhere, and just call in the auto-execution section or before the first return

Code: Select all

; Set up environment
 #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force

; Define your variables
GuiWidth := 630 ; adjust to fit your desired font - will be trickier with proportional fonts
; The positions below fit my particular needs.  You can use fixed x and y rather than calculating
ClockPosX := (A_ScreenWidth - GuiWidth) / 2
ClockPosY := (A_ScreenHeight - 180)
OldTime := ""
CustomColor := "000000"  ; Can be any RGB color , but I found this prevents character shadowing the best
TextColor := "237DF9"
FontName := "Curlz MT"
;FontName := "7-Segment"
FontSize := 60

; Set up your GUI
Gui +LastFound +AlwaysOnTop -Caption +ToolWindow  ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
Gui, Color, %CustomColor%
Gui, Font, s%FontSize%, %FontName%
Gui, Add, Text, vMyTime c%TextColor%, XX/XX/XX   XX:XX  ; XX:XX:XX serves to auto-size the window.
; Make all pixels of this color transparent and make the text itself translucent (150):
WinSet, TransColor, %CustomColor% 255
SetTimer, UpdateClock, 1000
Gosub, UpdateClock  ; Make the first update immediate rather than waiting for the timer.

; Display the clock
; NoActivate avoids deactivating the currently active window.
Gui, Show, x%ClockPosX% y%ClockPosY% w%GuiWidth% NoActivate  

; make draggable
enableGuiDrag()

return

UpdateClock:
FormatTime, CurTime,, hh:mm:ss
If (CurTime != OldTime) {
    OldTime := CurTime
    GuiControl,, MyTime, %CurTime%
}
return
Esc::ExitApp

enableGuiDrag(GuiLabel=1) {
	WinGetPos,,,A_w,A_h,A
	Gui, %GuiLabel%:Add, Text, x0 y0 w%A_w% h%A_h% +BackgroundTrans gGUI_Drag
	return
	
	GUI_Drag:
	SendMessage 0xA1,2  ;-- Goyyah/SKAN trick
	;http://autohotkey.com/board/topic/80594-how-to-enable-drag-for-a-gui-without-a-titlebar
	return
}
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 227 guests