 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
karen Guest
|
Posted: Mon Oct 15, 2007 9:05 am Post subject: problem to build a function to display a text to the screen |
|
|
Hello boys and girls.
When i write this function:
calculation (number1, number2)
{
Sum := number1 + number2
return sum
}
result := calculation (2, 2)
msgbox %result%
Return
it works, but when i write this function:
display (title, something)
{
gui, add, text,, %something%
gui, show,, %title%
return texte
}
display = hello
return
it doesn't work, because autohotkey displays :
Error at line 1.
Line Text: display (title, something)
Error: This line does not contain a recognized action.
The program will exit.
why ?
i don't understand, because i make a function like in the manual ?
can someone explain the reason ?
thank you! |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 266 Location: Russia
|
Posted: Mon Oct 15, 2007 9:49 am Post subject: |
|
|
Both of your examples don't work because you inserted spaces between the functions' names and their parameters. For example, you have a space between display and (title, something). That's not like in the manual.  |
|
| Back to top |
|
 |
karen Guest
|
Posted: Mon Oct 15, 2007 4:55 pm Post subject: no error, but the script doesn't work |
|
|
you're right YMP, you found the error.
Now, there's no error anymore, but my word "hello" and the title of the window don't display.
This is the function:
display(title, something)
{
gui, add, text,, %something%
gui, show,, %title%
return title, something
}
display = function building, hello
return
function building is the title of my window, and hello is the word i want to display.
If i want to make this kind of function it's because i don't want to write everytime the command:
gui, add, text,, first text
gui, add, text,, second text
...
i would like to use this function in a script by writing INCLUDE command for each script i will use it.
That's why i would like to build this function.
can someone solve my logical problem?
Thank you. |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 266 Location: Russia
|
Posted: Mon Oct 15, 2007 6:41 pm Post subject: |
|
|
Arguments are not assigned to a function, but passed to it inside parentheses.
| Code: |
display(title, something)
{
gui, add, text,, %something%
gui, show, w250 h50, %title%
}
display("function building", "hello")
|
If that all is for displaying messages, the MsgBox command is a lot more convenient. |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Tue Oct 16, 2007 5:25 am Post subject: |
|
|
Just so you know, | Code: | | return title, something | is effectively the same assince 'return' uses the result of the expression up to the comma, and the expression something has no effect. (Functions can only 'return' one value.)
Also, | Code: | | gui, show,, %title% | will auto-size the GUI based on its contents, while | Code: | | gui, show, w250 h50, %title% | will set the GUI to a specific size.
If you intend to use display() multiple times, you should addto the top of the function, since it would otherwise try to add another text control the next time you call display().  |
|
| Back to top |
|
 |
karen Guest
|
Posted: Wed Oct 17, 2007 10:02 am Post subject: problem to create a display function almosts solved |
|
|
Hello YMP, lexikos and other people.
i don't want to use msgbox function here because i have to clic on ok button each time something is displayed.
That's why i want to create a function that displays every text line rather than to write gui, add, text,, line by line.
YMP'sfunction works but for only one line.
In deed, i can't display two text on two lines because i don't see the seconde line.
So i changed the function like this:
display(something)
{
gui, add, text,, %something%
gui, show
}
display("hello")
display("world")
It's the same thing, i see the first line (where it's written hello), but i don't have to display the title of the window.
Actually i want to create a function that displays a text, and i want to use it to display several lines by writting this function each time i need it, without errasing the other texts higer or lower.
Is it possible?
That's what i can say for this topic today!
Thank you for your responses. |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 266 Location: Russia
|
Posted: Wed Oct 17, 2007 12:47 pm Post subject: |
|
|
Then I suggest using an edit control.
| Code: |
display(text)
{
Static GuiCreated, WinID
If not GuiCreated
{
Gui, +ToolWindow +LastFound
WinID:=WinExist()
Gui, Add, Edit, w300 h200
Gui, Show,, Message Window
GuiCreated=1
}
WinActivate, ahk_id %WinID%
SendInput, {Raw}%text%
}
display("Hello world!`n") ; `n is a new line.
display("It's me.`n")
display("How are you?`n")
|
|
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Wed Oct 17, 2007 3:45 pm Post subject: |
|
|
Alternately, you could add the AutoSize option:
| Code: | display(something)
{
gui, add, text,, %something%
gui, show, autosize
} |
|
|
| Back to top |
|
 |
karen Guest
|
Posted: Thu Oct 18, 2007 9:35 am Post subject: display function solved |
|
|
Fantastic!
It's better what i tought!
Thanks a lot!
I saved the source codes in two files, the first script made by ymp, and in another file the script made by lexico.
I didn't think to use autosize command, and i didn't know GuiCreated variable.
You should append your script in the site:
http://www.autohotkey.com/wiki/index.php?title=Function_Listing
ymp, you should append your script by calling Edit Function, and lexico you should append you script by calling display function.
These two function can serve for the autohotkey community.
Thank you very much! |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 266 Location: Russia
|
Posted: Thu Oct 18, 2007 11:32 am Post subject: |
|
|
Well, to tell the whole truth, nobody knew of that variable before I invented it and posted here. Not even lexikos who seems to know everything.
I'm glad I could help. |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Thu Oct 18, 2007 11:38 am Post subject: |
|
|
| YMP wrote: | Well, to tell the whole truth, nobody knew of that variable before I invented it and posted here. Not even lexikos who seems to know everything.
I'm glad I could help. |
, good reply. |
|
| Back to top |
|
 |
the sofie Guest
|
Posted: Sun Oct 21, 2007 9:49 am Post subject: the first example made by ymp has a little bug |
|
|
Hello everyone, it's just a nice comment but when i run the script made by ymp, i can read the text and move the cursor but the cursor is at the end of the text when i run the script the first time, is it possible to move the cursor at the biginning of the text?
i found this topic because i searched a command like writeln in pascal language.
I've just discovered autohotkey language, i try to read the manual and my english is not very good, that's why i search in the forum to have an example of a script because it's easier for me to learn autohotkey language like this, and i know pascal language in dos.
Thank you to have made this nice script!
bye! |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Sun Oct 21, 2007 11:48 am Post subject: Re: the first example made by ymp has a little bug |
|
|
| the sofie wrote: | | is it possible to move the cursor at the biginning of the text? | The simplest way is to send ^{Home}.
The best way (IMO) is to send the Edit control an EM_SETSEL message.
You may have noticed that YMP's script doesn't output correctly if you move the caret. Rather than sending keystrokes, I'd recommend ControlSetText.
| Code: | ; Created By: YMP
; Modified By: Lexikos
display(text)
{
Static GuiCreated, WinID, EditID
If not GuiCreated
{
Gui, +ToolWindow +LastFound
WinID:=WinExist()
Gui, Add, Edit, w300 h200
Gui, Show,, Message Window
GuiCreated=1
}
; Using the Last Found Window makes it unnecessary to keep specifying ahk_id %WinID%.
; It also avoids the need for DetectHiddenWindows if the GUI has been hidden/closed.
Gui, +LastFound
; Edit controls need `r`n for newline.
ifNotInString, text, `r
StringReplace, text, text, `n, `r`n
; Append the text.
ControlGetText, cur_text, Edit1
ControlSetText, Edit1, %cur_text%%text%
; Set caret position using EM_SETSEL. (0, 0 = first char, zero-length selection)
PostMessage, 0xB1, 0, 0, Edit1
; WinShow in case the GUI was closed/hidden between display() calls.
WinShow
WinActivate
}
display("Hello world!`n") ; `n is a new line.
display("It's me.`n")
display("How are you?`n")
return
;GuiClose:
;ExitApp | I made a couple of other changes; see the comments. |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 266 Location: Russia
|
Posted: Sun Oct 21, 2007 2:38 pm Post subject: |
|
|
But is that LastFound trick so reliable as to advise it to beginners? I'd rather say, let them see what each command operates on. Less efficient code maybe, but more clear.
I still very rarely use LastFound because you have to all the time recall whether you can or cannot use it in this particular case. Perhaps later, when the code is at last working, I'll optimize it this way, but not from the beginning. IMHO  |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Sun Oct 21, 2007 2:54 pm Post subject: |
|
|
| YMP wrote: | | But is that LastFound trick so reliable as to advise it to beginners? | Yes.
| Quote: | | It can make scripts easier to create and maintain since the WinTitle and WinText of the target window do not need to be repeated for every windowing command. | Sounds like good advice to be giving beginners...
| Quote: | | The "last found" window can be used by all of the windowing commands except WinWait, WinActivateBottom, and GroupAdd. To use it, simply omit all four window parameters (WinTitle, WinText, ExcludeTitle, and ExcludeText). | To me these exceptions are common sense:
- WinWait waits for a window to exist. The LFW obviously already exists, so there'd be no point WinWaiting for it.
- GroupAdd adds window criteria to a group - it doesn't interact with any windows at all. If no criteria is specified, what would it add?
- Similarly, WinActivateBottom activates the bottommost window of a group of windows. The LFW is a single window, so it would not be logical for WinActivateBottom to support it (WinActivate should be used instead.)
It has its own page in the manual, btw. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|