jeeswg's Notepad tutorial

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

jeeswg's Notepad tutorial

14 May 2017, 20:47

[Note: I will be expanding the content here.]

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 staus 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



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

WINDOWS - WINDOW STYLE [add in]
[style/extended style]
[word wrap]
[centre text]

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

WINDOWS - COORDINATES [add in]
[window coordinates, absolute/relative]
[control coordinates, absolute/relative]

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

TITLE BAR [add in]
[maximise/restore]
[alt-space menu]
[show/hide title bar, title bar buttons]
[title bar - system properties, font]

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

MENU BAR [add in]
[get menu item text/check state/enabled state/command ID]
[command IDs]
[resources in Notepad.exe]
[show/hide menu bar]
[PostMessage/SendMessage]

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

CONTROLS - STATUS BAR [add in]
[2 parts]

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

CONTROLS - EDIT CONTROL [add in]
[word wrap - menu item check state, control style]
[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 [add in]
[get path]

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

WINDOW - OPEN/SAVE AS DIALOG [add in]
[Common File Dialog/Common Item Dialog]

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

SHELL - RECENT FILES [add in]

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

REGISTRY - AUTOHOTKEY EDIT SCRIPT [add in]

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

REGISTRY - OPEN WITH NOTEPAD [add in]

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

NOTEPAD - CLIPBOARD [add in]
[clipboard + menu items]

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

NOTEPAD - DATE/TIME [add in]

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

NOTEPAD - EDIT CONTROL - GET/SET TEXT [add in]

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

NOTEPAD - DLL INJECTION [add in]
[set Edit control font]
[set Edit control background colour]

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

NOTEPAD - IMPROVEMENTS - ENHANCING AN EDIT CONTROL [add in]

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

NOTEPAD - IMPROVEMENTS - GET/SET/USE PATH [add in]
[encodings]

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 [add in]

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

NOTEPAD - IMPROVEMENTS - GO-BETWEEN [add in]
[prevent double open]
[open with a different program if the file is too big]

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

NOTEPAD - IMPROVEMENTS - AUTOSAVE [add in]

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

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

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 4 guests