jeeswg's Notepad tutorial

Helpful script writing tricks and HowTo's
User avatar
jeeswg
Posts: 3000
Joined: 19 Dec 2016, 01:58
Location: UK

jeeswg's Notepad tutorial

14 May 2017, 20:47

INTRODUCTION

This tutorial is intended to use Notepad to introduce some IT concepts and to explain some AutoHotkey techniques.

It is also meant as an addition to an AutoHotkey beginner's tutorial.

In theory, the focus of this tutorial is not Notepad itself, but using Notepad to explain more general ideas. So if you think that there is something missing from this tutorial, then please provide a solid or roundabout connection to Notepad as a justification for why it should be included in this tutorial.

==================================================

ABBREVIATIONS

AHK (AutoHotkey)
OS (operating system)
PC (personal computer)

TERMINOLOGY

32-bit/64-bit
class
ClassNN
hWnd
PID
window title
Wow64

==================================================

WINDOWS - CLASS

Notepad's main window has class 'Notepad'.

E.g. programs and their classes.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



E.g. get a window's class.

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get the active window's class
WinGetClass, vWinClass, A
MsgBox, % vWinClass
return


==================================================

WINDOWS - PATH

On 32-bit PCs, Notepad has path:
C:\Windows\System32\notepad.exe

On 64-bit PCs, Notepad has path:
C:\Windows\System32\notepad.exe [64-bit version]
C:\Windows\SysWOW64\notepad.exe [32-bit version][WoW64 (Windows 32-bit on Windows 64-bit)]

E.g. get a window's process name/path.

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;get the active window's process name/path
WinGet, vPName, ProcessName, A
WinGet, vPPath, ProcessPath, A
MsgBox, % vPName "`r`n" vPPath
return


Regarding 64-bit PCs, it may look like that is the wrong way round, but it is correct.

If you are running a 32-bit version of AutoHotkey, on a 64-bit PC and try to run 'C:\Windows\System32\notepad.exe', it will instead open 'C:\Windows\SysWOW64\notepad.exe', unless you turn off Wow64 file system redirection.

E.g. get the 'bitness' of the OS and of AutoHotkey, and if applicable demonstrate the effects of Wow64 file system redirection.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

WINDOWS - PID AND HWND

When you open Notepad e.g. by clicking on its Start menu icon or double-clicking a txt file, a new instance of Notepad is opened.

Each new instance of Notepad (or any process) has a unique process ID (PID). A process ID is usually a 4-digit number that is divisible by 4.

Each new instance of Notepad has a main window, that window has a window handle (hWnd). All windows have a unique window handle. If a window no longer exists, its window handle can be reused.

If for example you open the Find dialog or the Replace dialog. Those are also windows with their own window handle (hWnd).

In any particular instance of Notepad, all the windows will have the same process ID (PID).

==================================================

CONTROLS

Notepad's window normally has the following GUI elements:
- A title bar (with icon, window title, and minimise/maximise/close buttons).
- A menu bar (e.g. text: 'File, Edit, Format, View, Help').
- An Edit control (with vertical and horizontal scrollbars).
- A status bar (e.g. text: 'Ln 1, Col 1').

The title bar and menu bar are not regarded as controls.

The Edit control and status bar are regarded as controls.

Controls are actually windows. Controls have window handles and classes just like Notepad's main window (which has class 'Notepad').

Notepad's main window normally has 2 controls:
- An Edit control, which has class 'Edit'.
- A status bar, which has class 'msctls_statusbar32'.

Notepad's Find dialog normally has 10 controls:
- A Static control, with text 'Find what'.
- An Edit control, which starts off blank.
- 7 visible Button controls, with text: 'Match case, Direction, Up, Down, Find Next, Cancel, Help'.
- 1 hidden Button control, with text 'Match whole word only'

Why would there be a hidden control?

This is because Notepad uses a template Find dialog, that many other programs also use.

Notepad does not offer 'whole word' searching, so therefore this control is unnecessary, and is thus hidden.

==================================================

WINDOWS - CLASSNN

If you use AutoHotkey's window spy on a Notepad's main window it will list the following controls:
- Edit1, msctls_statusbar321

If you use AutoHotkey's window spy on a Notepad Find dialog it will give the following controls:
- Static1, Edit1, Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8

AutoHotkey refers to controls by their 'ClassNN'. Each control is referred to by its class and a number.

E.g. list a window's controls and their text.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

REGISTRY

Notepad stores information regarding its settings in the registry in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Notepad

You can open RegEdit (Registry Editor) and navigate to the key to view (and edit) the information.

The meaning of the registry entries is as follows:
- fWrap/StatusBar - word wrap/status bar, on/off.
- iMargin*/szHeader/szTrailer - Page Setup dialog settings.
- iPointSize - font size.
- iWindowPosDX/iWindowPosDY - main window width/height.
- iWindowPosX/iWindowPosY - main window top-left corner coordinates.
- lf* - font details (part of a LOGFONT structure).
- fMLE_is_broken - UNKNOWN.
- fSavePageSettings - UNKNOWN (may not appear in all versions of Notepad).
- fSaveWindowPositions - UNKNOWN.

E.g. get some of Notepad's font details.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



E.g. set some of Notepad's font details.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

REGISTRY - TXT FILE OPEN / AHK FILE EDIT SCRIPT / AUTOHOTKEY TRAY MENU EDIT SCRIPT

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

REGISTRY - ADD 'OPEN WITH NOTEPAD' TO ALL FILES

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

WINDOWS - WINDOW STYLE

[style/extended style]

A window/control has styles which affect how they look/behave. Some can only be changed when the window is created, some can be changed at any time.

[word wrap][centre text]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



See lower down for hide/show title bar.

==================================================

WINDOWS - COORDINATES

[window coordinates, absolute]
[control coordinates, absolute/relative to window/relative to window's client area]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;window - get coordinates
WinGetPos, vWinX, vWinY, vWinW, vWinH, A
MsgBox, % Format("x{} y{} w{} h{}", vWinX, vWinY, vWinW, vWinH)
return

w:: ;window - set coordinates
WinGetPos, vWinX, vWinY, vWinW, vWinH, A
WinMove, A,, % vWinX + 10, % vWinY, % vWinW, % vWinH
return


Code: [Select all] [Download] GeSHi © Codebox Plus

w:: ;control - set coordinates
;in AHK v1: ControlMove is relative to the window's top-left corner
ControlGetPos, vCtlX, vCtlY, vCtlW, vCtlH, Edit1, A
ControlMove, Edit1, % vCtlX, % vCtlY, % vCtlW - 5, % vCtlH, A
return


==================================================

TITLE BAR / TITLE BAR BUTTONS / ALT-SPACE MENU

[hide/show title bar]

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;toggle hide/show title bar
;WS_CAPTION := 0xC00000
;which is a combination of:
;WS_BORDER := 0x800000
;WS_DLGFRAME := 0x400000
WinSet, Style, ^0xC00000, A
return


[minimise, maximise/restore, close]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Odd behavior for WinRestore and WinMaximize - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34205

[enable/disable title bar buttons][hide show min/max buttons]

Note: if you disable both the min and max buttons, they disappear.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



event hooks and blocking min/max/restore/close - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34732&p=160424#p160424
Enable/disable external menuitem - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34907&p=161161#p161161

[show alt-space menu/show context menus]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[title bar: system properties, font]

How to open the context menu of the window's title bar - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=35975&p=165570#p165570
Odd behavior for WinRestore and WinMaximize - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34205&p=158372#p158372

[title bar - system properties, font]

to set the title bar appearance manually in Windows 7: Control Panel, Appearance and Personalization, Change the theme, Window Color

;function to set system fonts:
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893&p=131078#p131078
get a process's GDI handles (e.g. get/set title bar font and apply WM_SETFONT to a control) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=31228&p=145585#p145585

==================================================

MENU BAR

[get menu item text/check state/enabled state/command ID]

To invoke menu items use WM_COMMAND.
To invoke sysmenu (system menu) (alt-space menu) items use WM_SYSCOMMAND.

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;invoke menu items - save as
WinGet, hWnd, ID, ahk_class Notepad
PostMessage, 0x111, 4,,, % "ahk_id " hWnd ;WM_COMMAND := 0x111
return

w:: ;invoke sysmenu items - maximise
WinGet, hWnd, ID, ahk_class Notepad
PostMessage, 0x112, 61488,,, % "ahk_id " hWnd ;WM_SYSCOMMAND := 0x112
return


Enable/disable external menuitem - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34907&p=161185#p161185

[menu command IDs][for PostMessage/SendMessage]

Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

[show/hide menu bar]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[resources in Notepad.exe]

Use Resource Hacker and check for a Menu resource:
On older OSes: C:\Windows\System32\notepad.exe
On newer OSes: C:\Windows\System32\en-US\notepad.exe.mui

[get/set a menu item's checked/unchecked state (ticked/unticked)]

list of AutoHotkey WM_COMMAND IDs (e.g. Reload/Edit/Suspend/ListVars on another script) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=27824

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

CONTROLS - STATUS BAR

[get status bar text (multiple parts)]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

CONTROLS - EDIT CONTROL

see above: [word wrap - menu item check state, control style]
see below: [ctrl+left/ctrl+right]

==================================================

CONTROLS - EDIT CONTROL'S SCROLLBARS

E.g. get information regarding the scrollbars within Notepad's Edit control.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

PROCESS - COMMAND LINE

[get path]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

WINDOW - OPEN/SAVE AS DIALOG

[Common File Dialog/Common Item Dialog]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

SHELL - RECENT FILES

;[JEE_SysGetRecentItems function]
;list Recent Items (My Recent Documents) (Start Menu) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=31386

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

NOTEPAD - CLIPBOARD

[clipboard + menu items]

In Notepad's Edit menu, Cut and Copy are enabled/disabled based on whether any text is selected.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

NOTEPAD - UNDO STATE, UNSAVED STATE, READ-ONLY STATE

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

INSERT DATE/TIME

Customise the F5 (Edit, Time/Date) functionality.

Code: [Select all] [Download] GeSHi © Codebox Plus

F5::
q::
FormatTime, vDate,, HH:mm dd/MM/yyyy
SendInput, % vDate "`n"
return


==================================================

EDIT CONTROL - GET/SET ALL/SELECTED TEXT

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[See here for get selection (anchor/active) script:]
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893&p=138292#p138292

==================================================

CARET, CARET BLINK RATE

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



See also on this page: EM_SCROLLCARET, EM_GETSEL, EM_SETSEL.

==================================================

EDIT CONTROL - SCROLL

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

EDIT CONTROL - LEFT/RIGHT MARGINS

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

TEXT - UNICODE: UTF-8 / UTF-16

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;calculate which encoding will give the smallest file size
;note: depending on which characters are in the string, ANSI is lossy
ControlGetText, vText, Edit1, A
vSize16 := StrLen(vText)*2+2 ;UTF-16
vSize8 := StrPut(vText, "UTF-8")+3-1 ;UTF-8
vSizeA := StrPut(vText, "CP0")-1 ;ANSI
MsgBox, % Format("{} {} {}", vSize16, vSize8, vSizeA)
return


==================================================

NOTEPAD - DLL INJECTION

[set Edit control font]
[set Edit control background colour]

how to dll Inject - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=67&t=27047&p=166697#p166697

==================================================

NOTEPAD - IMPROVEMENTS - ENHANCING AN EDIT CONTROL

[ctrl+left/ctrl+right]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

NOTEPAD - IMPROVEMENTS - GET/SET/USE PATH

notepad get/set path (get/set text file path) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=30050

Some uses:
- rename file prompt (together with JEE_NotepadSetPath to reopen the file once it has a new name)
- jump to previous/next file (together with JEE_NotepadSetPath)
- check if file is already open in Notepad
- run current file in AutoHotkey (v1.0/v.1.1/v2.0 A32/U32/U64)
- compare Edit control text with saved text/backup file (e.g. via WinMerge) (put old contents onto the clipboard)
- open containing folder
- get file details e.g. encoding/size/date
- open with another program
- add to Recent folder
- save but maintain date
- reopen file, 'refresh' (together with JEE_NotepadSetPath)
- do something with current file (e.g.: move file, send to recycle bin, cut/copy to clipboard for Explorer file paste)

==================================================

NOTEPAD - IMPROVEMENTS - WARN DOUBLE OPEN

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

NOTEPAD - IMPROVEMENTS - GO-BETWEEN

[prevent double open]
[force open as ANSI]
[open with a different program if the file is too big]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

NOTEPAD - IMPROVEMENTS - AUTOSAVE

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



==================================================

LINKS

notepad get/set path (get/set text file path) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=30050

windows - How to get Notepad to enter fullscreen? - Super User
https://superuser.com/questions/1114935/how-to-get-notepad-to-enter-fullscreen

Edit Control Messages (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ff485923(v=vs.85).aspx

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 6 guests