GUI modification questions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

GUI modification questions

24 May 2020, 20:20

Hello,

I've got some basic GUI questions for which I can't seem to find answers in the GUI docs.

The GUI script was created by username: boiler and when run, shows a list of filenames for currently-open Excel (2010) Windows.* Clicking one of the filenames activates the corresponding window.

Here is the script with my changes shown in the indented lines:

Code: Select all

SetTitleMatchMode, 2
ExcelList := {}
WinGet, WinList, List, Microsoft Excel - ahk_exe EXCEL.EXE
loop, % WinList
{
	WinGetTitle, Title, % "ahk_id" WinList%A_Index%
	ExcelList[StrReplace(Title, "Microsoft Excel - ")] := WinList%A_Index%
}
Gui, +ToolWindow +AlwaysOnTop
			Gui, Font, s14, Tahoma
for Title, ID in ExcelList
;Gui, Add, Button, xm w200 v%ID% gExcelBtn, % Title
			Gui, Add, Button, xm w600 v%ID% gExcelBtn, % Title
Gui, Add, Button, xm+50 w100, Refresh
;Gui, Show,, Excel Windows
			Gui, Show, w800, h1000,, Excel Windows
return

ButtonRefresh:
reload

ExcelBtn:
	WinActivate, % "ahk_id" A_GuiControl
return

GuiClose:
ExitApp

^Esc::ExitApp
And here is an example of the resulting GUI:

2020-05-24f_upload to ahk forum.png
2020-05-24f_upload to ahk forum.png (39.92 KiB) Viewed 1333 times

Questions
• How can I get the filenames to display with left justification rather than centered?
• Why does the name of the GUI (upper-left corner) display "w800" instead of "Excel Windows" as it did originally?
• How can I get the GUI and individual fields to open with widths just large enough to contain the content (without wrapping) rather than with a fixed width?
• How can I get the GUI to close automatically once I click one of the filenames?

Bonus Question
• How can I modify the list to show the filenames in the order they were opened rather than alphabetically?

Thanks

*https://www.autohotkey.com/boards/viewtopic.php?f=76&t=73680&start=20
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: GUI modification questions

25 May 2020, 00:11

• How can I get the filenames to display with left justification rather than centered? https://www.autohotkey.com/docs/misc/Styles.htm#Button
• Why does the name of the GUI (upper-left corner) display "w800" instead of "Excel Windows" as it did originally? Gui, Show,, wIBecameTheTitelBCSomeonePutAnAdditionalCommaToTheLeftOfMe
• How can I get the GUI and individual fields to open with widths just large enough to contain the content (without wrapping) rather than with a fixed width? Removing the fixed width? BTBH, I'd go for another GuiControl (eg Gui, Add, ListView, ... ?) for file listing. https://www.autohotkey.com/docs/commands/Gui.htm#OtherOptions
• How can I get the GUI to close automatically once I click one of the filenames? Setting a label and using Gui, Submit and/or Gui, Destroy

Bonus Question
• How can I modify the list to show the filenames in the order they were opened rather than alphabetically? I'm sure that's available somewhere within the system (something like "Window creation date/time"). Stay tuned.

HTH :)
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: GUI modification questions

25 May 2020, 02:17

@WeThotUWasAToad
What's the reason you're asking the ~"same", already answered questions again, in a new thread?? :wtf:
https://www.autohotkey.com/boards/viewtopic.php?p=318787#p318787
User avatar
boiler
Posts: 17043
Joined: 21 Dec 2014, 02:44

Re: GUI modification questions

25 May 2020, 02:18

To left justify, you can just use the Left option from this simpler list: Controls: Common Styles and Other Options. That makes that line:

Code: Select all

Gui, Add, Button, xm w600 Left v%ID% gExcelBtn, % Title

You actually had two extra commas in your Gui, Show command. One is in w800, h1000. You don't separate your list of options with commas. Then you had an extra one after that. The line should be:

Code: Select all

Gui, Show, w800 h1000, Excel Windows
User avatar
boiler
Posts: 17043
Joined: 21 Dec 2014, 02:44

Re: GUI modification questions

25 May 2020, 02:21

BoBo wrote:
25 May 2020, 02:17
@WeThotUWasAToad
What's the reason you're asking the ~"same", already answered questions again, in a new thread?? :wtf:
https://www.autohotkey.com/boards/viewtopic.php?p=318787#p318787
Other than the left justification question, these are actually new questions after he has been using (and has modified) the code that resulted from that other thread.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 17:17

BoBo wrote:
25 May 2020, 02:17
@WeThotUWasAToad
What's the reason you're asking the ~"same", already answered questions again, in a new thread?? :wtf:
https://www.autohotkey.com/boards/viewtopic.php?p=318787#p318787
Apologies if I have asked questions in this thread which were already answered in another thread. Doing so was unintended.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 18:05

BNOLI wrote:
25 May 2020, 00:11
• How can I get the filenames to display with left justification rather than centered? https://www.autohotkey.com/docs/misc/Styles.htm#Button
• Why does the name of the GUI (upper-left corner) display "w800" instead of "Excel Windows" as it did originally? Gui, Show,, wIBecameTheTitelBCSomeonePutAnAdditionalCommaToTheLeftOfMe
• How can I get the GUI and individual fields to open with widths just large enough to contain the content (without wrapping) rather than with a fixed width? Removing the fixed width? BTBH, I'd go for another GuiControl (eg Gui, Add, ListView, ... ?) for file listing. https://www.autohotkey.com/docs/commands/Gui.htm#OtherOptions
• How can I get the GUI to close automatically once I click one of the filenames? Setting a label and using Gui, Submit and/or Gui, Destroy

Bonus Question
• How can I modify the list to show the filenames in the order they were opened rather than alphabetically? I'm sure that's available somewhere within the system (something like "Window creation date/time"). Stay tuned.

HTH :)
Thanks for the comments BNOLI.

• Left justification
DONE

• Upper-left corner
DONE

• GUI and fields widths
I removed the fixed width as you suggested and got just what I asked for — but not what I really wanted (haha).

Here's what I got:

2020-05-25c_ahk forum.png
2020-05-25c_ahk forum.png (35.67 KiB) Viewed 1209 times

But I suppose what I really wanted is to have all fields the same length but just long enough to accommodate the longest filename without having to wrap text. Is that doable?

Can you explain what you mean by "another GuiControl for file listing"? I've gone to the docs multiple times in the past to try to get my head around GUI controls but I'm always confused. I assume that controls on a GUI are sort of like the controls on an automobile dashboard. However, what do you mean by "GuiControl for file listing"?

• GUI auto-close after clicking
Can you explain what you mean by "Setting a label"?

Thanks for the help BNOLI.

By the way, can you explain the initialisms BTBH & HTH? Thanks

Here's the script as it is now:

Code: Select all

^+x::						; displays GUI list of open Excel Windows; Key [G18]
SetTitleMatchMode, 2
ExcelList := {}
WinGet, WinList, List, Microsoft Excel - ahk_exe EXCEL.EXE
loop, % WinList
{
	WinGetTitle, Title, % "ahk_id" WinList%A_Index%
	ExcelList[StrReplace(Title, "Microsoft Excel - ")] := WinList%A_Index%
}
if !GuiHwnd
{
	Gui, +ToolWindow +AlwaysOnTop +HwndGuiHwnd
				Gui, Font, s14, Tahoma
	for Title, ID in ExcelList
	;Gui, Add, Button, xm w200 v%ID% gExcelBtn, % Title
				;Gui, Add, Button, xm w600 Left v%ID% gExcelBtn, % Title
				Gui, Add, Button, xm Left v%ID% gExcelBtn, % Title
	Gui, Add, Button, xm+50 w100, Refresh
}
;Gui, Show,, Excel Windows
			;Gui, Show, w800, h1000,, Excel Windows
			Gui, Show,, Excel Windows
return

ButtonRefresh:
reload

ExcelBtn:
	WinActivate, % "ahk_id" A_GuiControl
return
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 18:07

boiler wrote:
25 May 2020, 02:18
To left justify, you can just use the Left option from this simpler list: Controls: Common Styles and Other Options. That makes that line:

Code: Select all

Gui, Add, Button, xm w600 Left v%ID% gExcelBtn, % Title

You actually had two extra commas in your Gui, Show command. One is in w800, h1000. You don't separate your list of options with commas. Then you had an extra one after that. The line should be:

Code: Select all

Gui, Show, w800 h1000, Excel Windows
Got it.

Thanks boiler
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
boiler
Posts: 17043
Joined: 21 Dec 2014, 02:44

Re: GUI modification questions

25 May 2020, 18:21

Not to answer for BNOLI, but since I saw your post...
WeThotUWasAToad wrote: But I suppose what I really wanted is to have all fields the same length but just long enough to accommodate the longest filename without having to wrap text. Is that doable?
I figured you didn't want them to be different widths, which is why I made them fixed widths in your other thread. If you want to still use buttons and make them all the same width but not longer than necessary, you would have to estimate the length of each item, which is not really an precise calculation unless you switch to a fixed-width (monospaced) font, then use that result for specifying the width of all of the buttons.
WeThotUWasAToad wrote: Can you explain what you mean by "another GuiControl for file listing"? I've gone to the docs multiple times in the past to try to get my head around GUI controls but I'm always confused. I assume that controls on a GUI are sort of like the controls on an automobile dashboard. However, what do you mean by "GuiControl for file listing"?
I believe he meant "another GUI control" (two words) such as a ListView. GuiControl is a command, which he wasn't referring to. He is suggesting you might want to use a different kind of control in your GUI to display your list of items rather than a series of buttons.
WeThotUWasAToad wrote: By the way, can you explain the initialisms BTBH & HTH? Thanks
I believe HTH is "happy to help." I don't know about BTBH.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 18:31

Another question:

How do you change the font attributes (eg color or bold) of the filenames listed?

I tried modifying the existing line:

Gui, Font, s14, Tahoma

to

Gui, Font, cGreen, s14, Tahoma
&
Gui, Font, s14, Tahoma, cGreen

I also tried inserting the example from the docs* to see what that does:

Gui, Add, Text, cGreen, My Text

None did what I was after.

Thanks

*https://www.autohotkey.com/docs/commands/Gui.htm#Font
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 18:40

Thanks for the explanations boiler.

These latest questions are, I suppose, less important. However, the one big item remaining is to determine how to have the GUI automatically close when I click one of the filenames.

Can you post the code to do that?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
boiler
Posts: 17043
Joined: 21 Dec 2014, 02:44

Re: GUI modification questions

25 May 2020, 18:48

You can't change the color of text in buttons, at least not through "normal" means.

Also, be careful in how you treat parameters in commands. Not keeping them in their proper place is what caused an issue before with your GUI title not appearing. Parameters aren't just a list of items that you can have as many as you want and separate them by commas. They must be in the exact place defined by the command. (If there are optional parameters, you can leave them out, but you have to use a blank parameter if it's going to be followed by other parameters so that they end up in the right place.) So for Gui, Font, it must follow this format:

Code: Select all

Gui, Font, Options, FontName
When you tried the line below, you inserted an extra parameter:

Code: Select all

Gui, Font, cGreen, s14, Tahoma
The options cGreen and s14 both are options that must appear in the option parameter (no comma separating them), like this:

Code: Select all

Gui, Font, cGreen s14, Tahoma
You also can't add an extra parameter like you did here:

Code: Select all

Gui, Font, s14, Tahoma, cGreen
Commands are expecting the parameters to appear as defined in the list as they appear in the documentation. You can't change it. (However, Click is an example of a special command that looks like it has flexibility in the order and number of parameters, but it is really just one parameter that gets parsed.)
User avatar
boiler
Posts: 17043
Joined: 21 Dec 2014, 02:44

Re: GUI modification questions

25 May 2020, 18:52

WeThotUWasAToad wrote:
25 May 2020, 18:40
Thanks for the explanations boiler.

These latest questions are, I suppose, less important. However, the one big item remaining is to determine how to have the GUI automatically close when I click one of the filenames.

Can you post the code to do that?

Thanks
BNOLI answered that in his post. Gui, Submit is the best way if you are going to display the same GUI again later. If you are finished with it or will be rebuilding it from scratch, you can use Gui, Destroy. You would put one of those at the end of the subroutine launched by the button click (ExcelBtn in this case).
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 19:11

boiler wrote:
25 May 2020, 18:52
WeThotUWasAToad wrote:
25 May 2020, 18:40
Thanks for the explanations boiler.

These latest questions are, I suppose, less important. However, the one big item remaining is to determine how to have the GUI automatically close when I click one of the filenames.

Can you post the code to do that?

Thanks
BNOLI answered that in his post. Gui, Submit is the best way if you are going to display the same GUI again later. If you are finished with it or will be rebuilding it from scratch, you can use Gui, Destroy. You would put one of those at the end of the subroutine launched by the button click (ExcelBtn in this case).
OK, I did see that before but I didn't know how to apply it.

So now I've got:

Code: Select all

…
ExcelBtn:
	WinActivate, % "ahk_id" A_GuiControl
	Gui, Destroy
Return
…and it works just as I wanted.

Many thanks.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 19:14

boiler wrote:
25 May 2020, 18:48
You can't change the color of text in buttons, at least not through "normal" means.

Also, be careful in how you treat parameters in commands. Not keeping them in their proper place is what caused an issue before with your GUI title not appearing. Parameters aren't just a list of items that you can have as many as you want and separate them by commas. They must be in the exact place defined by the command. (If there are optional parameters, you can leave them out, but you have to use a blank parameter if it's going to be followed by other parameters so that they end up in the right place.) So for Gui, Font, it must follow this format:

Code: Select all

Gui, Font, Options, FontName
When you tried the line below, you inserted an extra parameter:

Code: Select all

Gui, Font, cGreen, s14, Tahoma
The options cGreen and s14 both are options that must appear in the option parameter (no comma separating them), like this:

Code: Select all

Gui, Font, cGreen s14, Tahoma
You also can't add an extra parameter like you did here:

Code: Select all

Gui, Font, s14, Tahoma, cGreen
Commands are expecting the parameters to appear as defined in the list as they appear in the documentation. You can't change it. (However, Click is an example of a special command that looks like it has flexibility in the order and number of parameters, but it is really just one parameter that gets parsed.)
Thanks a bunch boiler! That is really great info and answers lingering questions I've had, not only about GUIs, but other types of scripts as well.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 19:32

Argh!

I keep thinking I've got this but now I'm having another issue.

Pressing a hotkey opens the GUI—just as I want. Then clicking one of the filenames activates the corresponding Excel file and closes the GUI— just as I want.

However, after running the thread once, it will not run again until I refresh the overall script (ie restart the .ahk file). The same is true with pressing the Refresh button. In other words, if I open the GUI, pressing the Refresh button kills the GUI altogether and, as before, I cannot open the GUI again until I have restarted the script.

The goal is to have the script running constantly and be able to open the GUI whenever wanted without needing to restart the script.

Here is the current form of the thread:

Code: Select all

^+x::						; displays GUI list of open Excel Windows; Key [G18]
SetTitleMatchMode, 2
ExcelList := {}
WinGet, WinList, List, Microsoft Excel - ahk_exe EXCEL.EXE
loop, % WinList
{
	WinGetTitle, Title, % "ahk_id" WinList%A_Index%
	ExcelList[StrReplace(Title, "Microsoft Excel - ")] := WinList%A_Index%
}
if !GuiHwnd
{
	Gui, +ToolWindow +AlwaysOnTop +HwndGuiHwnd
				Gui, Font, cGreen s14, Tahoma
				;Gui, Add, Text, cGreen, My Text
	for Title, ID in ExcelList
	;Gui, Add, Button, xm w200 v%ID% gExcelBtn, % Title
				Gui, Add, Button, xm w500 Left v%ID% gExcelBtn, % Title
				;Gui, Add, Button, xm Left v%ID% gExcelBtn, % Title
	Gui, Add, Button, xm+50 w100, Refresh
}
;Gui, Show,, Excel Windows
			;Gui, Show, w800, h1000,, Excel Windows
			Gui, Show,, Excel Windows
return

ButtonRefresh:
reload

ExcelBtn:
	WinActivate, % "ahk_id" A_GuiControl
	Gui, Destroy
return

;GuiClose:
;ExitApp
Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
boiler
Posts: 17043
Joined: 21 Dec 2014, 02:44

Re: GUI modification questions

25 May 2020, 19:58

That's why I said you should use Gui, Submit instead of Gui, Destroy if you're planning on displaying the GUI again.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

25 May 2020, 21:01

boiler wrote:
25 May 2020, 19:58
That's why I said you should use Gui, Submit instead of Gui, Destroy if you're planning on displaying the GUI again.
That's all it needed.

Thanks again boiler.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: GUI modification questions

25 May 2020, 23:12

JFTR (just for the records) BTBH is BNOLI-style for ... but TBH (to be honest) :mrgreen:. Yep, I'm not sure if an abbreviation really exists, but leave it with my counterpart to guess its minor variation - AFAIK (as far as I know) is AFAICS not that tough to guess (no idea if it exists :lol:)?
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: GUI modification questions

27 May 2020, 13:49

Thanks for the enlightenment. Head scratching was wearing a hole in my scalp. The only thing you should have done more is to begin your latest post with: FYE.*
Last edited by WeThotUWasAToad on 27 May 2020, 13:51, edited 1 time in total.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 454 guests