How to get the values of all elements of Edit of dialogue form using Delphi?

Functions WinAPI in Delphi to get values of Edit

Dear visitors! This article will help understand how to read all values of Edit dialogue elements from the window of another application using Delphi and WinAPI functions. We will use WinAPI functions to get the Edit class dialogue elements. For instance, we need to get Edit values from the form with the title “MyProgram”, so we will use the WinAPI function FindWindow.


HWND FindWindow(
    LPCTSTR lpClassName,	// pointer of the class name
    LPCTSTR lpWindowName 	// pointer of the window name
   );

Therefore, in the first string we write :

Hndl:=FindWindow(nil, 'MyProgram');

Apparently using this function we can get a window pointer (Нandle – window descriptor). Push the window descriptor into the Handle variable. We need this Handle for the following action:

Hedt:=FindWindowEx(Hndl,0,'Edit',''); // we get the first Edit

For search, let’s use WinAPI function FindWindowEx:


[Is not supported in Windows NT]

FindWindowEx function searches for the data about the window descriptor, 
class name and window name of which comply with certain strings. 
Search function of the child windows goes from the first 
to the last preset window. 

HWND FindWindowEx
(
    HWND hwndParent,		// descriptor of the parent window 
    HWND hwndChildAfter,	// descriptor of the child window
    LPCTSTR lpszClass,		// pointer of the class name 
    LPCTSTR lpszWindow		// pointer of the window name 
    );


Now we get a descriptor of the child window of the fist Edit into the variable Hedt. The only thing left to do is to open the search of elements of the dialogue window. To this end, we use WinAPI function GetWindow:


GetWindow function returns the window descriptor, which is defined  
with the relation (Z index or parent), to the indicated window. 

HWND GetWindow
(
    HWND hWnd,	// descriptor of the parent window
    UINT uCmd 	// relation glag 
   );

So we write the following text:

Hedt:=GetWindow(Hedt,GW_HWNDFIRST);

Now a pointer is established to the first Edit in the variable Hedt. Parameter GW_HWNDFIRST points to selection of the first element. Selection has started, and to continue it let’s write the following:

repeat
    //we get next Edit
    Hedt:=GetWindow(Hedt,GW_HWNDNEXT);
    if Hedt<>0 then 
	SendMessage(Hedt,WM_GETTEXT,WPARAM(512),lparam(HoldString));
    HoldString__ := HoldString__ + ' ' + String(HoldString);
until Hedt=0;


Now a pointer is established to the next Edit in the variable Hedt. Parameter GW_HWNDNEXT points to selection of the following element. Selection will go on until all elements are searched. We also used WinAPI SendMessage function using which we read the value of the dialogue element.


LRESULT SendMessage(

    HWND hWnd,		// descriptor of the destination window 
    UINT Msg,		// message 
    WPARAM wParam,	// first parameter of the message 
    LPARAM lParam 	// second parameter of the message
   );

WM_GETTEXT parameter tells the window with Hedt descriptor that we need to read its value into the HoldString varibale larger than 512. Let’s check our work. Create new project. Put the button onto the form. Here is the whole listing:

procedure Form1.Button1Click(Sender: TObject);
var
  HoldString: PChar;
  HoldString__: String;
  Hndl,Hedt: HWND;
begin
    Hndl:=FindWindow(nil, 'MyProgram');
    Hedt:=FindWindowEx(Hndl,0,'Edit',''); // we get the first Edit
    Hedt:=GetWindow(Hedt,GW_HWNDFIRST); //pointer to the first Edit
    repeat
        //set the pointer to the next Edit
        Hedt:=GetWindow(Hedt,GW_HWNDNEXT);
        if Hedt<>0 then 
	    SendMessage(Hedt,WM_GETTEXT,WPARAM(512),lparam(HoldString));
        HoldString__ := HoldString__ + ' ' + String(HoldString);
    until Hedt=0;
end;