Including a Gui directly into a window?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1277
Joined: 06 Aug 2017, 08:21
Location: Germany

Including a Gui directly into a window?

05 Feb 2018, 11:13

Hi,

Is it possible to add Gui elements to a specific window like in this example?
Image

I'm not sure, but I think these points would be mandatory:
- #if WinActive("ahk_class Notepad") (will only work for Notepad in this case)
- Get the current window's x and y pos
- Gui, -Caption (no border)
- Gui, Add, Text, BackgroundTrans (text won't cover the background)
- the gui elements' positions would have to be relative to X0Y0 of the specific window.

I tried it with this code, but it doesn't work.
Even though the MessageBox shows the correct values, the gui has a problem with the gx and gy.

Code: Select all

^F2::
#if WinActive("ahk_class Notepad")
WinGetPos, x, y, Width, Height, Unbenannt - Editor
Sleep, 5
gx := x + 20
gy := y + 20
Gui, -Caption
Gui, +AlwaysOnTop
Gui, Show, %gx% %gy% w100 h10
Gui, Add, Text, BackgroundTrans x0 y0, Test
;MsgBox, %x%, %y%, %gx%, %gy%
return
Thanks for any help!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Including a Gui directly into a window?

05 Feb 2018, 11:21

Würde ich dann doch eher als context-menu aufbereiten. Ständiger abgleich ob das add-on auch richtig positioniert ist/bleibt ist IMHO "ebbes over the top" :silent:
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Including a Gui directly into a window?

05 Feb 2018, 21:13

I had partial success with this:

Code: Select all

#NoEnv
; #Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force

; Check if any notepad window exists, otherwise close this script.
IfWinNotExist, ahk_exe Notepad.exe
	exitapp
NotepadHWND:=WinExist() ; get handle to notepad.

; Add simple dummy context menu entries for testing. I dont know if different context can be added/activated on different GUI elemnts. Would be nice.
Menu, menu1, Add, func 1, dummy
Menu, menu1, Add, func 2, dummy
Menu, menu1, Add, func 3, dummy

; Just for simple testing, plain text GUI elements. Can be replaced with real controls.
gui,add,text,,function 1
gui,add,text, x+0 yp,function 2

gui, +OwnDialogs -Caption +AlwaysOnTop	; Set GUI basics. +ToolWindow could be used as well and positioned so that there remains an X button to close the functions if not needed.
gui, +0x40000000 -0x80000000			; This should prevent notepad from loosing focus when interacting with this GUI.
gui, +parent%NotepadHWND%				; Set GUI to a parent.
gui,show,x0 y0							; Since its a child, it should be placed relative to the parents starting xy 00 coords and menu bars dont usually change their coordinates or size.

return

; This opens the context.
GuiContextMenu:
	Menu, menu1, Show, %A_GuiX%, %A_GuiY%
Return

; Dummy subroutine, does nothing, required by context menu entries.
dummy:
return

^R::Reload		; Reload script on L.Ctrl + R keys. For development, testing and debugging.
^Esc::exitapp	; Exit script on L.Ctrl + Escape keys. For development, testing and debugging.
The issue here is that its placing the gui in the text editor. Also, moving or resizing makes the GUI disappear. Minimizing and restoring the window brings in back again.
Also, if if gui,show, y-10 is used, it is going beneath the menu bar in the notepad. Weird. Any more ideas? It should be doable. Just have to figure out how. xD
Hope this pumps at least something. Good luck! I'll post if i find some solution in the mean time. :)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Including a Gui directly into a window?

05 Feb 2018, 21:45

Try something like the following:

Code: Select all


; Windows 8.1 64 bit - Autohotkey v1.1.27.04 32-bit Unicode

#NoEnv
SetBatchLines, -1 ; in order to make the window docking smoother
SetWinDelay, -1 ; in order to make the window docking smoother
global menuItems := [] ; an arry in which will be store all AHKID of menu items (in fact GUI)
global xmin := 248, ymin := 28 ; the coordinates (relative to the window) from which the menu will be added

f := Func("WinActive").bind("A") ; dummy
Loop, 3
	Menu, menu1, Add, func %a_index%, % f

	; creation of the 'menun items'
DetectHiddenWindows, On
Loop, 2 {
	Gui, New, +LastFound +AlwaysOnTop -Caption -Border +HWNDID
	menuItems[ ID ] := "ahk_id " . ID
	Gui, Color, white
	Gui, Add, Text, gf, function %a_index%
	Gui, Show, Hide
	WinSet, TransColor, white ; make the backgroundof the GUI (set to be white) transparent
	Gui, Show, AutoSize h20 Minimize NA
}
DetectHiddenWindows, Off

run, notepad,,, PID
WinWaitActive, ahk_pid %PID%
global AHKID := "ahk_id " . HWND:=WinExist(lastFoundWindow:="")
for ID in menuItems
	Gui, %ID%:+Owner%HWND% ; make the 'menu items' owned by notepad
machineCodeAddress := RegisterCallback("PIDOnLocationChange") ; machine-code address that will be called by the hook below
hWinEventHook := SetWinEventHook(0x800B, 0x800B, 0, machineCodeAddress, PID, 0, 0) ; EVENT_OBJECT_LOCATIONCHANGE  := "0x800B" (https://msdn.microsoft.com/en-us/library/windows/desktop/dd318066(v=vs.85).aspx)
PIDOnLocationChange(0, 0, 1)
OnExit, handleExit
WinWaitClose
handleExit:
UnhookWinEvent(hWinEventHook)
ExitApp



f() {
Menu, menu1, Show
}

PIDOnLocationChange(__hWinEventHook, __event, __hwnd) { ; https://msdn.microsoft.com/en-us/library/windows/desktop/dd373885(v=vs.85).aspx

	if not (__hwnd) ; if EVENT_OBJECT_LOCATIONCHANGE event has been generated by a change of location, shape, or size of the window object itself (no the caret etc.)
return

	WinGetPos, __x, __y, __w, __h, % AHKID
	__xmin := __x + xmin, __ymin := __y + ymin

		__δh := 0
		for __ID, __AHKID in menuItems {
			WinMove, % __AHKID,, % __xmin + ((a_index-1) * __δh), % __ymin ; arrange  menu items side by side...
			WinGetPos, __mx,, __mw,, % __AHKID
			__δh := __mw + 5 ; ...with a margin of 5px
		}

}

SetWinEventHook(__eventMin, __eventMax, __hmodWinEventProc, __lpfnWinEventProc, __idProcess, __idThread, __dwFlags) {
   DllCall("CoInitialize", "Uint", 0)
   return DllCall("SetWinEventHook"
			, "Uint", __eventMin
			, "Uint", __eventMax
			, "Ptr", __hmodWinEventProc
			, "Ptr", __lpfnWinEventProc
			, "Uint", __idProcess
			, "Uint", __idThread
			, "Uint", __dwFlags)
} ; cf. https://autohotkey.com/boards/viewtopic.php?t=830
UnhookWinEvent(__hWinEventHook) {
    __v := DllCall("UnhookWinEvent", "Ptr", __hWinEventHook)
    DllCall("CoUninitialize")
return __v
}
Last edited by A_AhkUser on 05 Feb 2018, 22:30, edited 1 time in total.
my scripts
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Including a Gui directly into a window?

05 Feb 2018, 22:00

So much about me thinking it would be more or less simple code. Holly pancakes...
Nice piece of code.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Including a Gui directly into a window?

05 Feb 2018, 22:33

theimmersion wrote:So much about me thinking it would be more or less simple code.
I added some comments and references alongside the source. ;)
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 136 guests