Simple file viewer
Simple file viewer
Hi. Using the simple text editor example from the help file as a starting point, I've made a simple file viewer, by setting the edit control to read-only, removing the menu bar, and setting some colours, so all that is shown is the title bar, and the edit control which occupies the entire client area. Passing this script a file name on the command line, causes that file to be displayed for viewing. So far, so good. But, it is still an edit control, so in order to scroll (by keyboard), one has to first scroll the caret all the way to the bottom/right, and then continue scrolling in order to scroll the text up/left (of course page up/down and control-home/end still work).
So the question is: is there any way to set the edit control so that the caret is hidden, and the arrow keys cause the text to scroll up/down/left/right? I suspect the answer is no, so might there be an alternative?
So the question is: is there any way to set the edit control so that the caret is hidden, and the arrow keys cause the text to scroll up/down/left/right? I suspect the answer is no, so might there be an alternative?
Re: Simple file viewer
I've never tried this, just an idea that you could pursue and test. https://www.autohotkey.com/board/topic/13625-how-to-get-and-set-vertical-scroll-position/?p=89143
Re: Simple file viewer
Thanks, but that's way beyond my desire. I'll just live with the way the edit control works, unless someone knows of another control which does allow these kinds of settings.
Re: Simple file viewer
Code: Select all
#Requires AutoHotkey v2
#HotIf WinActive("ahk_id " MyGui.Hwnd)
Up::Send "{WheelUp}"
Down::Send "{WheelDown}"
Right::Send "{WheelRight}"
Left::Send "{WheelLeft}"
#HotIf
Re: Simple file viewer
Thanks. This will work with the edit control (in AHK 2)?
Does each up/down/left/right, translated to wheelup/wheeldown/wheelleft/wheelright, scroll the text by a single line/column, or by multiple lines/columns?
Does each up/down/left/right, translated to wheelup/wheeldown/wheelleft/wheelright, scroll the text by a single line/column, or by multiple lines/columns?
Re: Simple file viewer
The above script would be for v2, but I didn't test much.
Here is a script in v1. I took the example you mentioned from the docs and added some hotkeys that will PostMessage to the edit control to scroll.
The parts that I added are marked with comments.
You can specify the number of lines to scroll in the PostMessage commands. Currently it will scroll 2 lines vertically and 4 characters horizontally.
Here is a script in v1. I took the example you mentioned from the docs and added some hotkeys that will PostMessage to the edit control to scroll.
The parts that I added are marked with comments.
You can specify the number of lines to scroll in the PostMessage commands. Currently it will scroll 2 lines vertically and 4 characters horizontally.
Code: Select all
#Requires AutoHotkey v1.1
; https://www.autohotkey.com/docs/v1/lib/Gui.htm#ExEditor
; Create the sub-menus for the menu bar:
Menu, FileMenu, Add, &New, FileNew
Menu, FileMenu, Add, &Open, FileOpen
Menu, FileMenu, Add, &Save, FileSave
Menu, FileMenu, Add, Save &As, FileSaveAs
Menu, FileMenu, Add ; Separator line.
Menu, FileMenu, Add, E&xit, FileExit
Menu, HelpMenu, Add, &About, HelpAbout
; Create the menu bar by attaching the sub-menus to it:
Menu, MyMenuBar, Add, &File, :FileMenu
Menu, MyMenuBar, Add, &Help, :HelpMenu
; Attach the menu bar to the window:
Gui, Menu, MyMenuBar
; Create the main Edit control and display the window:
Gui, +Resize HwndMyGuiHwnd ; <-- added Hwnd. | Make the window resizable.
Gui, Add, Edit, vMainEdit WantTab W600 R20 HScroll HwndhMainEdit ; <-- added Hwnd. |
Gui, Show,, Untitled
CurrentFileName := "" ; Indicate that there is no current file.
EM_LINESCROLL := 0xB6 ; <-- added https://learn.microsoft.com/en-us/windows/win32/controls/em-linescroll
return
;Added________________________________________________________________________________________________
#If WinActive("ahk_id " MyGuiHwnd)
Up::PostMessage, % EM_LINESCROLL, 0, -2, , ahk_id %hMainEdit%
Down::PostMessage, % EM_LINESCROLL, 0, 2, , ahk_id %hMainEdit%
Left::PostMessage, % EM_LINESCROLL, -4, 0, , ahk_id %hMainEdit%
Right::PostMessage, % EM_LINESCROLL, 4, 0, , ahk_id %hMainEdit%
#If
;_____________________________________________________________________________________________________
FileNew:
GuiControl,, MainEdit ; Clear the Edit control.
return
FileOpen:
Gui +OwnDialogs ; Force the user to dismiss the FileSelectFile dialog before returning to the main window.
FileSelectFile, SelectedFileName, 3,, Open File, Text Documents (*.txt)
if not SelectedFileName ; No file selected.
return
Gosub FileRead
return
FileRead: ; Caller has set the variable SelectedFileName for us.
FileRead, MainEdit, %SelectedFileName% ; Read the file's contents into the variable.
if ErrorLevel
{
MsgBox Could not open "%SelectedFileName%".
return
}
GuiControl,, MainEdit, %MainEdit% ; Put the text into the control.
CurrentFileName := SelectedFileName
Gui, Show,, %CurrentFileName% ; Show file name in title bar.
return
FileSave:
if not CurrentFileName ; No filename selected yet, so do Save-As instead.
Goto FileSaveAs
Gosub SaveCurrentFile
return
FileSaveAs:
Gui +OwnDialogs ; Force the user to dismiss the FileSelectFile dialog before returning to the main window.
FileSelectFile, SelectedFileName, S16,, Save File, Text Documents (*.txt)
if not SelectedFileName ; No file selected.
return
CurrentFileName := SelectedFileName
Gosub SaveCurrentFile
return
SaveCurrentFile: ; Caller has ensured that CurrentFileName is not blank.
if FileExist(CurrentFileName)
{
FileDelete %CurrentFileName%
if ErrorLevel
{
MsgBox The attempt to overwrite "%CurrentFileName%" failed.
return
}
}
GuiControlGet, MainEdit ; Retrieve the contents of the Edit control.
FileAppend, %MainEdit%, %CurrentFileName% ; Save the contents to the file.
; Upon success, Show file name in title bar (in case we were called by FileSaveAs):
Gui, Show,, %CurrentFileName%
return
HelpAbout:
Gui, About:+owner1 ; Make the main window (Gui #1) the owner of the "about box".
Gui +Disabled ; Disable main window.
Gui, About:Add, Text,, Text for about box.
Gui, About:Add, Button, Default, OK
Gui, About:Show
return
AboutButtonOK: ; This section is used by the "about box" above.
AboutGuiClose:
AboutGuiEscape:
Gui, 1:-Disabled ; Re-enable the main window (must be done prior to the next step).
Gui Destroy ; Destroy the about box.
return
GuiDropFiles: ; Support drag & drop.
Loop, Parse, A_GuiEvent, `n
{
SelectedFileName := A_LoopField ; Get the first file only (in case there's more than one).
break
}
Gosub FileRead
return
GuiSize:
if (ErrorLevel = 1) ; The window has been minimized. No action needed.
return
; Otherwise, the window has been resized or maximized. Resize the Edit control to match.
NewWidth := A_GuiWidth - 20
NewHeight := A_GuiHeight - 20
GuiControl, Move, MainEdit, W%NewWidth% H%NewHeight%
return
FileExit: ; User chose "Exit" from the File menu.
GuiClose: ; User closed the window.
ExitApp
Re: Simple file viewer
Thank you! That code works great for scrolling the text. And thanks for that link to the Microsoft control reference; I had tried searching MS for that information without success.
Almost there now, just a couple other points.
When the text is scrolled horizontally, the caret remains in the first column, so it is then off-screen and not visible. Now, when the text is paged up or down (with PgUp/PgDn keys), it automatically snaps back to column one to bring the caret into view, so that the horizontal scroll position is lost. This doesn't happen when the text is scrolled vertically (with up/down arrow keys), in which case it retains its horizontal scroll position. [I would like to hide the caret entirely, so that it would never be visible. But if that were possible, I suppose this problem would still exist of snapping back to the (invisible) caret on page up/down.]
So I suppose one solution might be to always send a message to place the caret within the visible text. I'm not sure if that's possible, and it seems pretty complicated at best. Might there be a better way?
Almost there now, just a couple other points.
When the text is scrolled horizontally, the caret remains in the first column, so it is then off-screen and not visible. Now, when the text is paged up or down (with PgUp/PgDn keys), it automatically snaps back to column one to bring the caret into view, so that the horizontal scroll position is lost. This doesn't happen when the text is scrolled vertically (with up/down arrow keys), in which case it retains its horizontal scroll position. [I would like to hide the caret entirely, so that it would never be visible. But if that were possible, I suppose this problem would still exist of snapping back to the (invisible) caret on page up/down.]
So I suppose one solution might be to always send a message to place the caret within the visible text. I'm not sure if that's possible, and it seems pretty complicated at best. Might there be a better way?
Re: Simple file viewer
This seemed to work. I added hotkeys for PgUp and PgDn. I also added a line to hide the caret.
Code: Select all
;...
EM_LINESCROLL := 0xB6 ; https://learn.microsoft.com/en-us/windows/win32/controls/em-linescroll
WM_VSCROLL := 0x115 ; https://www.autohotkey.com/board/topic/87514-sendmessage-scroll-down-a-certain-number-of-lines/?p=555929
SB_PAGEDOWN := 3
SB_PAGEUP := 2
DllCall("HideCaret", "Ptr", hMainEdit) ; https://www.autohotkey.com/boards/viewtopic.php?p=394169#p394169
return
#If WinActive("ahk_id " MyGuiHwnd)
Up:: PostMessage, % EM_LINESCROLL, 0, -2, , ahk_id %hMainEdit%
Down:: PostMessage, % EM_LINESCROLL, 0, 2, , ahk_id %hMainEdit%
Left:: PostMessage, % EM_LINESCROLL, -4, 0, , ahk_id %hMainEdit%
Right::PostMessage, % EM_LINESCROLL, 4, 0, , ahk_id %hMainEdit%
PgUp:: PostMessage, % WM_VSCROLL, SB_PAGEUP, 0, , ahk_id %hMainEdit%
PgDn:: PostMessage, % WM_VSCROLL, SB_PAGEDOWN, 0, , ahk_id %hMainEdit%
#If
;...
Re: Simple file viewer
Superb! Thank you!
Much nicer with the caret hidden. Page up/down now operate correctly. However, this created a new problem: Home/End now cause the vertical scroll position to be reset! So, I thought, add hotkeys for Home/End using the WM_HSCROLL message, though I wasn't sure what parameters to use with that message. And, I had no idea what the numeric value for WM_HSCROLL is. Searching was very frustrating, but I finally found it (0x114). And the previous message link you gave earlier thankfully provided the constants/values for the WM_HSCROLL parameters, as well as the WM_VSCROLL parameters. So, I starting experimenting by adding the following:
And it worked! So now this is a very nice little file viewer, thanks to a lot of help received here!
It is designed and sized for my simple application, so it purposefully does not begin with a horizontal scroll bar. But, that will be needed if the program is run with a smaller monitor, or if the user resizes the window. So the next task will be to figure out how to detect that condition and add a horizontal scroll bar.
Much nicer with the caret hidden. Page up/down now operate correctly. However, this created a new problem: Home/End now cause the vertical scroll position to be reset! So, I thought, add hotkeys for Home/End using the WM_HSCROLL message, though I wasn't sure what parameters to use with that message. And, I had no idea what the numeric value for WM_HSCROLL is. Searching was very frustrating, but I finally found it (0x114). And the previous message link you gave earlier thankfully provided the constants/values for the WM_HSCROLL parameters, as well as the WM_VSCROLL parameters. So, I starting experimenting by adding the following:
Code: Select all
WM_HSCROLL := 0x114
SB_LEFT := 6
SB_RIGHT := 7
Home:: PostMessage, % WM_HSCROLL, SB_LEFT , 0, , ahk_id %hMainEdit%
End:: PostMessage, % WM_HSCROLL, SB_RIGHT , 0, , ahk_id %hMainEdit%
It is designed and sized for my simple application, so it purposefully does not begin with a horizontal scroll bar. But, that will be needed if the program is run with a smaller monitor, or if the user resizes the window. So the next task will be to figure out how to detect that condition and add a horizontal scroll bar.
Re: Simple file viewer
So I'm trying to set up syntax highlighting for AHK. To begin with, I'm setting it up for Notepad++. I've downloaded the two themes linked from this forum, AHK and Lazy AHK, as well as one from GitHub by Gabriel somebody. In Notepad++, I can switch between these three (which all use different colours, etc.), but none of them match the colours shown in the code blocks of this thread. So, does the forum software use its own colours? And, what theme do you AHK experts recommend? I'd like to find the theme that is most common, or used by the majority of users.
Re: Simple file viewer
Afaik, it's a plugin for the forum software - which can be customized by our admins; and I guess it has some quirks.
Some of its colors even depend on the used forum theme. For example, I am using a dark forum theme, and I also see dark codeboxes with a few adjusted colors (-> contrast).
For your editor, you should just use a theme which you like, and which helps you coding: this usually means, different colors for different syntax elements - afaik, we have no idea what the majority uses (in whatever editors), and I don't think we care.
Of course, you can edit existing themes or create new ones for any editor which supports it, and you could also recreate the highlighting of the forum software, if you really want.
Re: Simple file viewer
Good stuff. Thanks for the response. Maybe some others will chime in with favorite themes? Or what's good or bad about certain themes? Meanwhile, I'll play with the three I've found.
Re: Simple file viewer
That's helpful. Okay, I've configured a colour scheme that suits me, in the editor which I use (CudaText).gregster wrote: ↑27 Mar 2023, 17:36For your editor, you should just use a theme which you like, and which helps you coding: this usually means, different colors for different syntax elements - afaik, we have no idea what the majority uses (in whatever editors), and I don't think we care.
Of course, you can edit existing themes or create new ones for any editor which supports it, and you could also recreate the highlighting of the forum software, if you really want.
Just wanted to mention here that CudaText is a powerful cross-platform editor with syntax highlighting for a huge number of languages, including AutoHotkey. Perhaps this editor could be added to the list of editors which support AHK, in the help file and on the forum?