Messaging window.
Microsoft windows is controlled by messaging, and this messaging is a major part of the VBA Windows API subject.
Example: ShowWindow ( Example, maximise and Minimise) http://allapi.mentalis.org/apilist/ShowWindow.shtml
A look at the function description makes this fairly simple to understand. Two arguments, the first, necessarily requiring the hwnd of the window to be controlled in some way/ shown I some way, as specified by the second argument. In effect the second argument is just a number, from a set of numbers which determine what will be done
We are using for the first time in this example the API constants. Using these is more of a conventional practice: Although there are some parallels with Early Binding of the available Library functions , we do not have available any named constants in VBA API work, in the same way that we do sometimes after Early Binding.
But rather than using a number, we define a constant, and use that instead. The constant can have any name we like, ( and in fact, we can also be a simple variable rather than a Constant, but conventionally we use the recognised naming and assigning way. So we will use these sort of conventions also
Const SW_MINIMIZE As Long = 6
Const SW_MAXIMIZE As Long = 3
https://i.postimg.cc/4y94MXh1/API-Constants.jpg https://i.postimg.cc/RqxCx27x/API-Constants.jpg
The following demonstration coding works on a same text file as the previous coding and is self explanatory if run in debug mode
https://i.postimg.cc/DZdPG4g9/Show-W...x-Min-Norm.jpg
Code:
Option Explicit
Private Declare Function FindWndNumber Lib "user32" Alias "FindWindowA" (Optional ByVal lpClassName As String, Optional ByVal lpWindowName As String) As Long
Private Const SW_MINIMIZE As Long = 6
Private Const SW_MAXIMIZE As Long = 3
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_NORMAL As Long = 1
Sub TextAPI()
Dim HdlTextFile As Long, MsgRef As Long
Let HdlTextFile = FindWndNumber(lpClassName:="Notepad", lpWindowName:="Neues Textdokument.txt - Editor"): Debug.Print HdlTextFile & " " & Hex(HdlTextFile) ' 984776 F06C8
Dim FuncReturn As Variant
Let FuncReturn = ShowWindow(HdlTextFile, SW_MINIMIZE): Debug.Print FuncReturn ' 24
Let FuncReturn = ShowWindow(HdlTextFile, SW_MAXIMIZE): Debug.Print FuncReturn ' 24
Let FuncReturn = ShowWindow(HdlTextFile, SW_NORMAL): Debug.Print FuncReturn ' 24
End Sub
Example: ShowWindow ( Example Hide Unhide)
This example follows on directly from the last, and perhaps requires little explanation. This perhaps borders on the area of "dangerous".
Using the same text file as example I hide and unhide the window. I deliberately do this in the pair so as to hopefully leave all back in the previous normal
Code:
Option Explicit
Private Declare Function FindWndNumber Lib "user32" Alias "FindWindowA" (Optional ByVal lpClassName As String, Optional ByVal lpWindowName As String) As Long
Private Const SW_HIDE As Long = 0
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_SHOW As Long = 5
Sub TextAPI()
Dim HdlTextFile As Long, MsgRef As Long
Let HdlTextFile = FindWndNumber(lpClassName:="Notepad", lpWindowName:="Neues Textdokument.txt - Editor"): Debug.Print HdlTextFile & " " & Hex(HdlTextFile) ' 984776 F06C8
Dim FuncReturn As Variant
Let FuncReturn = ShowWindow(HdlTextFile, SW_HIDE): Debug.Print FuncReturn ' 24
Let FuncReturn = ShowWindow(HdlTextFile, SW_SHOW): Debug.Print FuncReturn ' 0 - ' If the window was previously hidden, the return value is zero.
End Sub
Example, close window ( SendMessageA )
Code:
Option Explicit
Private Declare Function FindWndNumber Lib "user32" Alias "FindWindowA" (Optional ByVal lpClassName As String, Optional ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Const WM_CLOSE As Long = &H10
Sub TextAPI()
Dim HdlTextFile As Long
Let HdlTextFile = FindWndNumber(lpClassName:="Notepad", lpWindowName:="Neues Textdokument.txt - Editor"): Debug.Print HdlTextFile & " " & Hex(HdlTextFile) ' 984776 F06C8
Dim FuncReturn As Variant
Let FuncReturn = SendMessage(HdlTextFile, WM_CLOSE, 0, 0): Debug.Print FuncReturn '
End Sub
Example, Capture window text ( SendMessageA )
These API things can be a bit obscure in their functioning and somewhat difficult to define, group or order in terms of what they are about. This example demonstrates that.
In this example we remain with our text file examples, and look again at a text file and the corresponding winspy view centred around the "second level" Edit window, whose handle we found in an initial second handle getting coding above . We noted that it looked like it might be the window that we typically concern ourselves with when working on such a text file.
In fact, as a confirmation, if we restart the winspy from new , we will initially have a small winspy window , and if we leave it initially small, but drag the small target/sight thing into the main part of the text file, then it expands and gives what it thinks is the window we dragged it into, in this case, indeed the Edit window with the actual text form the text file somehow showing in winspy.
https://i.postimg.cc/sx3Fqktw/Draged...ext-editor.jpg

Now it all gets a bit hit and miss and god knows if anyone really knows of any logic to the seemingly hap hazard way of doing things here…
The last two arguments for the SendMessageA are not so well defined: - Specifies additional message-specific information. , and the documentation to the returned values is just as much use: - The return value specifies the result of the message processing and depends on the message sent.
About as useful as a chocolate Teapot and as welcome as a fart in a space suit.
Before completing this I took a look in a bit more detail in the next post and here
Bookmarks