While you can also check to see for a running process (which is better for those programs like MSN Messenger that run in the quick launch bar), simply finding a window is easier and often used as a validation method.
Now, let's begin.
You will need the following for this project: 2 Command Buttons - One named cmdclass and the other named cmdcaption
The caption for the cmdclass button is Class while the caption for the cmdcaption button is Caption.
First, you have to declare an API that you plan to use, which in this case is the FindWindow API. It goes like this (put this in a module):
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
The public part of the above code means that this function can be carried across your forms. In other words, if you put this function in a module, then go to Form1, you will be able to input it. Switching public to private reverses this effect, so a private function can only be used in the form it is declared in.
So now we have declared the API, but we must now declare a variable to store this function in. Let's name it find. Put this at the very top of your form:
Dim find As Long
The next step is finding a window to locate. What window shall we choose? Let's go with the most basic of all, notepad. There are two methods to find the notepad window. By class name or by the window name. The class name of notepad is: Notepad The window name of notepad is: Untitled - Notepad Assuming you have an untitled file.
Now, what's the difference?
Class names are often a more secure way to locate a window. Why? Well, whenever you open a file in notepad, the caption of the window changes to the name of your textfile - notepad. An example, "MyTextFile - Notepad" This makes it rather difficult to keep track of notepad with a changing caption, so class names should be used in this case, because they always stay constant.
Let's begin with class names:
Double click on the class button and put this in:
FindWindow(Notepad, vbNullString)
Don't like class names? Then let's try the same approach with captions.
Double click on your caption button and put this in:
FindWindow(vbNullString, "Untitled - Notepad")
Ok, that's a complete bit o` code, or is it? How do you know it actually "found" the window, it doesn't give you any feedback. Let's add some message boxes to let us now it actually found what we told it to find. Under your cmdclass button, edit your code to make it look like this:
find = FindWindow("Notepad",vbNullString) If find <> 0 Then MsgBox "I found the window by class name!", vbOkOnly, "Window found." Else MsgBox "I can't find the window!", vbCritical, "Window not found." End If
Under your cmdcaption button, edit your code to make it look like this:
find = FindWindow(vbNullString,"Untitled - Notepad") If find <> 0 Then MsgBox "I found the window by caption!", vbOkOnly, "Window found." Else MsgBox "I can't find the window!", vbCritical, "Window not found." End If
Now hit F5 in the Visual Basic IDE and try it out! ;)
The full code looks like this:
Module:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Form:
Dim find As Long Private Sub cmdclass_Click() find = FindWindow("Notepad",vbNullString) If find <> 0 Then MsgBox "I found the window by class name!", vbOkOnly, "Window found." Else MsgBox "I can't find the window!", vbCritical, "Window not found." End If End Sub Private Sub cmdcaption_Click() find = FindWindow(vbNullString,"Untitled - Notepad") If find <> 0 Then MsgBox "I found the window by caption!", vbOkOnly, "Window found." Else MsgBox "I can't find the window!", vbCritical, "Window not found." End If End Sub
Comments
Post new comment