Hi, Community,
this morning I posted a question of how to code a search dialogue
which would highlight the search string in the haystack and which
provides a repetitive search.
HugoV gave me a precious hint of how to find
Laszlo's
script and here's the result.
Just load down the script and let it run.
Enter a search phrase in the edit field underneath the search button,
subsequently click the search button.
Known limitation:
When the text is wider than the edit area, the found string will be
marked but no horizontical scrolling will happen. Any ideas?
To be done:
Install hotkeys ctrl-f (start search) and F3 (continue search)
Code:
; a little gui with some text from the manual
Gui 1:Add, Button, gFindString, Search
Gui 1:Add, Edit, w100 vfind
Gui 1:Add, Edit, w330 r3 vContent HSCROLL
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button
SB_SetParts(300)
Gui 1:Show,, FindSample
WinGet ControlID, ID, FindSample
someText=
(
Creating a script
Each script is a plain text file containing commands
to be executed by the program (AutoHotkey.exe).
A script may also contain hotkeys and hotstrings, or
even consist entirely of them. However, in the absence
of hotkeys and hotstrings, a script will perform its
commands sequentially from top to bottom the moment
it is launched.
To create a new script:
Open Windows Explorer and navigate to a folder of
your choice.
Pull down the File menu and choose New >> AutoHotkey
Script (or Text Document).
Type a name for the file, ensuring that it ends in .ahk.
For example: Test.ahk
Right-click the file and choose Edit Script.
On a new blank line, type the following:
#space::Run www.google.com
The symbol # stands for the Windows key, so #space means
holding down the Windows key then pressing the spacebar
to activate a hotkey. The :: means that the subsequent
command should be executed whenever this hotkey is
pressed, in this case to go to the Google web site.
To try out this script, continue as follows:
Save and close the file.
In Windows Explorer, double-click the script to launch
it. A new tray icon appears. Hold down the Windows key
and press the spacebar. A web page opens in the default
browser.
To exit or edit the script, right click its tray icon.
Note: Multiple scripts can be running simultaneously,
each with its own tray icon. Furthermore, each script
can have multiple hotkeys and hotstrings.
)
Guicontrol,,Edit2, %someText%
return
FindString:
Gui, Submit, Nohide
if (find != lastFind) {
offset = 0
hits = 0
}
GuiControl 1:Focus, Content ; focus on main help window to show selection
SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID% ; Scroll to top
StringGetPos pos, Content, %find% ,,offset ; find the position of the search string
if (pos = -1) {
if (offset = 0) {
SB_SetText("'" . find . "' not found", 1)
SB_SetText("", 2)
}
else {
SB_SetText("No more occurrences of '" . find . "'")
SB_SetText("", 2)
offset = 0
hits = 0
}
return
}
StringLeft __s, Content, %pos% ; cut off end to count lines
StringReplace __s,__s,`n,`n,UseErrorLevel ; Errorlevel <- line number
addToPos=%Errorlevel%
SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible
SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text
; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx
; Scroll the caret into view in an edit control:
SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%
offset := pos + addToPos + Strlen(find)
lastFind = %find%
hits++
SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)
Return
Let me know what you think and of course report me all the bugs.
I'm aware that I only played around with the excellent code of
Laszlo, honour to whom honour is due!
Have a nice and peaceful Christmas and a happy new year,
Klaus