Monday, November 10, 2008

Example 2 of QTP API

For this example make sure that a notepad window is open with the title Untitled – Notepad. This below code will change the title from Untitled – Notepad to Testing.

'Declare FindWindow method. The FindWindow function provides a way for advanced developers to get a handle to a window by specifying its window class and window name. You can then use that handle to send messages directly to the window.
Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString

'Declare SetWindowText method. The SetWindowText function changes the text of the specified window's title bar (if it has one).
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString

'Get HWND of the Notepad window. Two parameters are being passed - name of class i.e. Notepad and Windows caption/title i.e. Untitled - Notepad. This program will also work if you put vbNullString.vbNullString is a special VB constant that denotes a null string.
hwnd = Extern.FindWindow("Notepad", "Untitled - Notepad")
if hwnd = 0 then
MsgBox "Notepad window not found"
end if

'Change the title of the notepad window. Two parameters are being passed, first is the Windows handle and second is the text string that is needed to set on the window.
result=Extern.SetWindowText(hwnd, "Testing")
'if the function succeeds then the return value is non-zero i.e. in our case result will contain a non zero value which can be used further.