Why I get when call code from function? - autohotkey

This the code I am trying to call from a function.
it works fine without problem if I use by itself
#3::
FileRead, OutputVar, C:\temp\test.txt
msgbox %OutputVar%
#SingleInstance, force
Gui, Color, White
Gui, -caption +toolwindow +AlwaysOnTop
Gui, font, s30 bold, Arial
Gui, add, text, vTX cRed TransColor, %OutputVar%
Gui, Show, % "x" A_ScreenWidth-1500 " y" A_ScreenHeight-900 ,TRANS-WIN
WinSet, TransColor, White, TRANS-WIN
return
when I call put that in function and call it from there I get error below.
:*:11::
Run calc.exe
numcodee()
return
numcodee()
{
FileRead, OutputVar, C:\temp\test.txt
msgbox %OutputVar%
#SingleInstance, force
Gui, Color, White
Gui, -caption +toolwindow +AlwaysOnTop
Gui, font, s30 bold, Arial
Gui, add, text, vTX cRed TransColor, %OutputVar%
Gui, Show, % "x" A_ScreenWidth-1500 " y" A_ScreenHeight-900 ,TRANS-WIN
WinSet, TransColor, White, TRANS-WIN
}
Error when call it from function

The GUI requires its variables remain in scope for longer than you are allowing. Its been a while since I did anything major with AKH but I would guess is your OutputVar which is causing the error. The way to fix this is simply to move the data from the function, and list it somewhere above in the file)
The thing to keep in mind is that once your function ends, all the variables declared inside it will fall out of scope and you wont be able to use them, the GUI is concerned something it will need will fall out from under it.

Related

Use WinMove to move relative to location, instead of absolut destination

Is it possible to move a window by for example 200 pixles downwards, or similar?
I have only found the WinMove command, but it requires an fixed location, instead of just moving the window a bit downwards for example.
So if I wanted to move the calc downwards a bit in the below example, how would I go about doing that?
Run, calc.exe
WinWait, Calculator
WinMove, xxxx?
Thanks!
What you need in this case, is the WinGetPos command.
Example:
; Press F1 to move the Calculator window 10 pixel downwards:
F1::
If !WinExist("Calculator") ; "!" means "NOT" in this case
Run, calc.exe
WinWait, Calculator
WinGetPos, X, Y, Width, Height, Calculator
WinMove, Calculator, , X, Y+10
return

Make GUI move with window

I'm trying to set up a GUI that moves along with a window of a software that I'm using. My general goal is to block a certain area of that software to be clickable. I've implemented a very simple solution that achieves this for a static window, however it's not anywhere near an optimal solution because whenever I resize/move the software, the blocked part becomes misaligned.
#Persistent
Gui, +alwaysontop -caption
Gui, show, w238 h30 x1032 y30
Gui, 1:Color, 545454
Gui, 2:+alwaysontop -caption
Gui, 2:show, w238 h30 x2008 y30
Gui, 2:Color, 545454
Gui, 3:+alwaysontop -caption
Gui, 3:show, w238 h30 x1032 y732
Gui, 3:Color, 545454
Gui, 4:+alwaysontop -caption
Gui, 4:show, w238 h30 x2008 y732
Gui, 4:Color, 545454
Gui, 5:+alwaysontop -caption
Gui, 5:show, w46 h36 x3676 y33
Gui, 5:Color, 545454
return
My 2nd attempt looks like this:
Win_Hwnd := WinExist("ahk_exe excel.exe ahk_class XLMAIN")
; Gui Create
Gui, Color, yellow
Gui, Font, s12 cRed, Bold Verdana
Gui, Add, Text,, This is Text
Gui, +LastFound +ToolWindow +AlwaysOnTop -Caption -Border HWNDGui_Hwnd
DllCall("SetParent", "uint", Gui_Hwnd, "uint", Win_Hwnd)
Gui, Show, w238 h30 x1 y1 ; Position on Gui Parent
return
Esc::ExitApp
This piece of code works perfectly with Excel, Word, Notepad, however not with the specific software (PokerStars) that I'm using. I've replaced the ahk_exe and ahk_class according to the logic of the code and using Window Spy to identify the parameters, however when running the desired software, the gui only appears in a fixed position according to the x/y coordinates.
My question:
Why does this code work for some software, but not for others?
Is there any other approach I could be using to achieve the desired result?
Edit:
I have so far figured out that it has to do something with the ahk_classes that I'm trying to address:
ahk_class GLFW30
ahk_class #32770
seem to be incompatible with my script, I don't understand why though.

How to align text when the middle is bold?

I have a script that needs to have an emboldened word. If I use:
Gui, Add, Text,, % "Dispassionated Concentration: "
Gui, font, bold
Gui, Add, Text,x+0, % FormatMinutes(dc)
Gui, font
Gui, Add, Text,, % "Non Focused:" FormatMinutes(NonFocused)
. "`nIdle Time:" FormatMinutes(IdleTime)
. "`nTotal Time:" FormatMinutes(TotalTime)
then the result is like this:
If the line is:
Gui, Add, Text,Y+0, % "Non Focused:" FormatMinutes(NonFocused)
If it's x-0 Y+0:
How to align the text?
Use
Gui, Add, Text,xs Y+0, % "Non Focused:" FormatMinutes(NonFocused)
See Gui - Syntax & Usage: Positioning and Sizing of Controls. Admittedly I just try and error without actually understand what they do.

re-maximize a minimized Gui

Is it possible to prevent a Gui to minimize on a WIN+D shortcut ?
One way of doing it would be to check from time to time if the Gui element is minimized and re-maximize it. But I don't know how to do that.
A very simple example script I'm working with as a reference
Gui +LastFound -Caption +ToolWindow -SysMenu
Gui, Color, 000000
Gui, Font, FF0000
Gui, Font, s12
Gui, Add, Text, vTxt cWhite, XXXXX YYYYY ; XX & YY serve to auto-size the window.
WinSet, TransColor, 0
SetTimer, UpdateOSD, 200
Gosub, UpdateOSD ; Make the first update immediate rather than waiting for the timer.
Gui, Show, x10 y10, NoActivate; NoActivate avoids deactivating the currently active window.
return
UpdateOSD:
MouseGetPos, MouseX, MouseY
GuiControl,, Txt, X %MouseX%, Y %MouseY%
return
Thanks for the help !
You can use context sensitivity to find out which GUI you have active and then write a standard button press shortcut, that then leads nowhere
#IfWinActive Untitled - Notepad
#d::
MsgBox , , Winkey + D pressed, You were inside a new untitled notepad and pressed Windows key + D but I stopped all actions
; Do nothing now
return
#IfWinActive
I just tested it... I keyed it to simple notepad, works for me.
Added a screenshot of the result

Right justify a list of values

I am an irregular AHK user and typically work at the edge of my knowledge.
I usually need to keep the help files to hand.
I am using Version 1.0.48.05 and have a problem.
In a nutshell - I want to print a list of numeric values using the Gui interface so that the values line up under each other justified to the right.
I can not find an easy or robust way of doing that. What am I missing.
The values can vary from 0 to 9999. They are always integers.
====================
This is a snippet of code before I try to right-justify :
tabpos := 200
Gui, Add, Text, x%tabpos% y300, %value1%
Gui, Add, Text, x%tabpos% y320, %value2%
Gui, Add, Text, x%tabpos% y340, %value3%
Gui, Add, Text, x%tabpos% y360, %value4%
That prints my values at the "tabpos" position but all left-justified.
My first attempt to right-justify was to use a function which padded with a number of spaces determined by the length of the "value" . For exammple :
Gui, Add, Text, x%tabpos% y300, % pad(6, value1)
pad(width, val)
{
ans := SubStr(" ", 1, width-StrLen(val)).val
return ans
}
But that did not work well as the width of a digit was not the same as (or a multiple of) the width of a space.
Where I introduced a multiple into the function, that worked differently for different type sizes and typefaces.
My second attempt was to adjust the "tabpos" by a number of pixels depending on the length of the "value". For example :
newpos := tabpos - 9*StrLen(value1) ; Where a digit is 9 pixels wide
Gui, Add, Text, x%newpos% y300, %value1%
But that only works well where a digit is exactly 9 pixels wide. I would like a more general, robust method.
I have tried using the AHK SetFormat instruction, but have failed to get any effect. Perhaps I am using it incorrectly. In any event, it appears that SetFormat works by padding with spaces and so it would have the same effect as my first attempt (above) with the same problems.
==================
Any suggestions will be very welcome.
I would like the method to work whatever the typeface, type size, whether bold or normal, and whatever the DPI of the monitor.
How about...
Gui, Add, Text, x100 y100 w100 Right, 123
Gui, Add, Text, x100 y120 w100 Right, 123456
Gui, Add, Text, x100 y140 w100 Right, 123456789