Monday, 29 March 2021

HotkeyToWindow.exe Assigns a hotkey to a window. When pressed will activate that window.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

There are two types of hotkeys in Windows. RegisterHotkey requires a program to expect it. WM_SetHotkey is supported by the default behaviour of a window so the program doesn't need to be aware of it.

Hotkeys are system keys so work all the time. To take the example below, using sendkeys to send Ctrl+N to any application will activate Notepad. This is one way around restrictions on setting the active window.


REM HotkeyToWindow.bat
REM This file compiles TopMost.vb to TopMost.exe
REM HotkeyToWindow.exe sets a hotkey to activate the window
REM To use 
REM     HotkeyToWindow &ltModifier&gt  &ltvirtualkey&gt &ltWindowtitle&gt
REM E.G.
REM To assign Ctrl (2) and N key (78) to notepad
REM     HotkeyToWindow 2 78 Untitled - Notepad
REM To remove the hotkey send 0
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\HotkeyToWindow.exe" "%~dp0\HotkeyToWindow.vb"
pause


'HotkeyToWindow.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module TopMost
	Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare UNICODE Function SendMessageInt Lib "user32" alias "SendMessageW" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, lParam as Integer) As Integer
	Public Const WM_SETHOTKEY = &h32
	Public const HOTKEYF_SHIFT = 1
	Public const HOTKEYF_CONTROL = 2
	Public const HOTKEYF_ALT = 4
	Public const HOTKEYF_EXT = 8

	Sub Main()
		On Error Resume Next
		Dim hWindows as IntPtr
		Dim CmdLine as String
		Dim Ret as Integer
		Dim vKey as UInteger

		CmdLine = Command()
		Dim A as String()
		A = Split(CmdLine, Chr(32), 3, 1)
		hwindows = FindWindowW(vbNullString, A(2))
		If hwindows = 0 then
			Msgbox(A(2) & " cannot be found.")
		Else
			vKey = (cByte(A(0)) * &h100) 
			vKey = vKey + cByte(A(1))
			Ret = SendMessageInt(hWindows, WM_SETHOTKEY, vkey, 0)
			If Ret = -1 then MsgBox("Invalid Hotkey")
			If Ret = 0 then MsgBox("Invalid Window")
		End If
	End Sub
End Module

Modifiers - add them together to use more than one

SHIFT = 1
CONTROL = 2
ALT = 4
EXT = 8
Virtual Keycodes
Key Virtual Code
Decimal
Virtual Code
Hexadecimal
Left mouse button 1 0x1
Right mouse button 2 0x2
Control-break processing 3 0x3
Middle mouse button (three-button mouse) 4 0x4
X1 mouse button 5 0x5
X2 mouse button 6 0x6
Undefined 7 0x7
Backspace 8 0x8
Tab 9 0x9
Clear 12 0xc
Enter 13 0xd
Shift 16 0x10
Ctrl 17 0x11
Alt 18 0x12
Pause 19 0x13
Caps Lock 20 0x14
IME Kana mode 21 0x15
Undefined 22 0x16
IME Junja mode 23 0x17
IME final mode 24 0x18
IME Hanja mode 25 0x19
Esc 27 0x1b
IME Convert 28 0x1c
IME Nonconvert 29 0x1d
IME Accept 30 0x1e
IME Mode Change Request 31 0x1f
Spacebar 32 0x20
Page Up 33 0x21
Page Down 34 0x22
End 35 0x23
Home 36 0x24
Left Arrow 37 0x25
Up Arrow 38 0x26
Right Arrow 39 0x27
Down Arrow 40 0x28
Select 41 0x29
Print 42 0x2a
Execute 43 0x2b
Print Screen 44 0x2c
Ins 45 0x2d
Del 46 0x2e
Help 47 0x2f
0 48 0x30
1 49 0x31
2 50 0x32
3 51 0x33
4 52 0x34
5 53 0x35
6 54 0x36
7 55 0x37
8 56 0x38
9 57 0x39
A 65 0x41
B 66 0x42
C 67 0x43
D 68 0x44
E 69 0x45
F 70 0x46
G 71 0x47
H 72 0x48
I 73 0x49
J 74 0x4a
K 75 0x4b
L 76 0x4c
M 77 0x4d
N 78 0x4e
O 79 0x4f
P 80 0x50
Q 81 0x51
R 82 0x52
S 83 0x53
T 84 0x54
U 85 0x55
V 86 0x56
W 87 0x57
X 88 0x58
Y 89 0x59
Z 90 0x5a
Left Windows key 91 0x5b
Right Windows key 92 0x5c
Applications key 93 0x5d
Reserved 94 0x5e
Computer Sleep 95 0x5f
Numeric keypad 0 96 0x60
Numeric keypad 1 97 0x61
Numeric keypad 2 98 0x62
Numeric keypad 3 99 0x63
Numeric keypad 4 100 0x64
Numeric keypad 5 101 0x65
Numeric keypad 6 102 0x66
Numeric keypad 7 103 0x67
Numeric keypad 8 104 0x68
Numeric keypad 9 105 0x69
Multiply 106 0x6a
Add 107 0x6b
Separator 108 0x6c
Subtract 109 0x6d
Decimal 110 0x6e
Divide 111 0x6f
F1 112 0x70
F2 113 0x71
F3 114 0x72
F4 115 0x73
F5 116 0x74
F6 117 0x75
F7 118 0x76
F8 119 0x77
F9 120 0x78
F10 121 0x79
F11 122 0x7a
F12 123 0x7b
F13 124 0x7c
F14 125 0x7d
F15 126 0x7e
F16 127 0x7f
F17 128 0x80
F18 129 0x81
F19 130 0x82
F20 131 0x83
F21 132 0x84
F22 133 0x85
F23 134 0x86
F24 135 0x87
Num Lock 144 0x90
Scroll Lock 145 0x91
Left Shift 160 0xa0
Right Shift 161 0xa1
Left Control 162 0xa2
Right Control 163 0xa3
Left Menu 164 0xa4
Right Menu 165 0xa5
Browser Back 166 0xa6
Browser Forward 167 0xa7
Browser Refresh 168 0xa8
Browser Stop 169 0xa9
Browser Search 170 0xaa
Browser Favorites 171 0xab
Browser Start And Home 172 0xac
Volume Mute 173 0xad
Volume Down 174 0xae
Volume Up 175 0xaf
Next Track 176 0xb0
Previous Track 177 0xb1
Stop Media 178 0xb2
Play/Pause Media 179 0xb3
Start Mail 180 0xb4
Select Media 181 0xb5
Start Application 1 182 0xb6
Start Application 2 183 0xb7
; : 186 0xba
+ 187 0xbb
, 188 0xbc
- 189 0xbd
. 190 0xbe
/ ? 191 0xbf
` ~ 192 0xc0
[ { 219 0xdb
\ | 220 0xdc
] } 221 0xdd
' " 222 0xde
223 0xdf
Reserved 224 0xe0
Oem Specific 225 0xe1
Either The Angle Bracket Key Or The Backslash Key On The Rt 102-Key Keyboard 226 0xe2
Windows 95/98/Me, Windows Nt 4.0, Ime Process 229 0xe5
Used to pass unicode characters as if they were keystrokes 231 0xe7
Attn 246 0xf6
Crsel 247 0xf7
Exsel 248 0xf8
Erase Eof 249 0xf9
Play 250 0xfa
Zoom 251 0xfb
Pa1 253 0xfd
Clear 254 0xfe

Saturday, 13 March 2021

MoveWindow.exe - Moves a window

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

REM MoveWindow.bat
REM This file compiles MoveWindow.vb to MoveWindow.exe
REM MoveWindow changes the position of a window
REM To use 
REM MoveWindow nnnnxnnnn <Windowtitle>
REM E.G.
REM MoveWindow 300x400 Untitled - Notepad
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\MoveWindow.exe" "%~dp0\MoveWindow.vb" /verbose
pause

'MoveWindow.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module TopMost
	Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
	Public Const HWND_TOPMOST = -1
	Public Const HWND_NOTOPMOST = -2
	Public Const SWP_NOMOVE = &H2
	Public Const SWP_NOSIZE = &H1
	Public Const SWP_NOZORDER = &h4

	Sub Main()
		On Error Resume Next
		Dim hWindows as IntPtr
		Dim CmdLine as String
		Dim Ret as Integer
		CmdLine = Command()
		Dim A as String()
		Dim B as String()
		A = Split(CmdLine, Chr(32), 2, 1)
		B = Split(A(0), "x", 2, 1)
		hwindows = FindWindowW(vbNullString, A(1))
		If hwindows = 0 then
			Msgbox(A(1) & " cannot be found.")
		Else
			Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, B(0), B(1), 0, 0, SWP_NOSIZE + SWP_NOZORDER)
			If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
		End If
	End Sub
End Module

Friday, 12 March 2021

SizeWindow.exe. Changes the size of a window

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

REM SizeWindow.bat REM This file compiles SizeWindow.vb to SizeWindow.exe REM SizeWindow changes the size of a window REM To use REM SizeWindow nnnnxnnnn REM E.G. REM SizeWindow 300x400 Untitled - Notepad "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\SizeWindow.exe" "%~dp0\SizeWindow.vb" /verbose pause

'TopMost.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module TopMost
	Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
	Public Const HWND_TOPMOST = -1
	Public Const HWND_NOTOPMOST = -2
	Public Const SWP_NOMOVE = &H2
	Public Const SWP_NOSIZE = &H1
	Public Const SWP_NOZORDER = &h4

	Sub Main()
		On Error Resume Next
		Dim hWindows as IntPtr
		Dim CmdLine as String
		Dim Ret as Integer
		CmdLine = Command()
		Dim A as String()
		Dim B as String()
		A = Split(CmdLine, Chr(32), 2, 1)
		B = Split(A(0), "x", 2, 1)
		hwindows = FindWindowW(vbNullString, A(1))
		If hwindows = 0 then
			Msgbox(A(1) & " cannot be found.")
		Else
			Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, B(0), B(1), SWP_NOMOVE + SWP_NOZORDER)
			If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
		End If
	End Sub
End Module