Monday, November 10, 2008

Example 3 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 “Untitled – NotepadUntitled – Notepad”.

'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
'The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer.
Extern.Declare micLong, "GetWindowText", "user32.dll", "GetWindowTextA", micHwnd, micString+micByRef, micLong
'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
'Three parameters are being passed - window handle, Pointer to the buffer that will receive the text, Specifies the maximum number of characters to copy to the buffer, including the NULL character.
result1=Extern.GetWindowText(hwnd,str,256)
strplus=str+str
'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, strplus)