Solved

FindChildByClass & FindChildByTitle (Delphi Win32 API)

Posted on 2000-02-24
4
355 Views
Last Modified: 2010-04-04
How can I convert these old Visual Basic functions to something that might work in Delphi?

Here are the two VB functions:
-----------------------------------

Function FindChildByClass(parentw, childhand)
firs% = GetWindow(parentw, 5)
If UCase(Mid(GetClass(firs%), 1, Len(childhand))) Like UCase(childhand) Then GoTo bone
firs% = GetWindow(parentw, GW_CHILD)
If UCase(Mid(GetClass(firs%), 1, Len(childhand))) Like UCase(childhand) Then GoTo bone

While firs%
firss% = GetWindow(parentw, 5)
If UCase(Mid(GetClass(firss%), 1, Len(childhand))) Like UCase(childhand) Then GoTo bone
firs% = GetWindow(firs%, 2)
If UCase(Mid(GetClass(firs%), 1, Len(childhand))) Like UCase(childhand) Then GoTo bone
Wend
FindChildByClass = 0

bone:
Room% = firs%
FindChildByClass = Room%

End Function

Function FindChildByTitle(parentw, childhand)
firs% = GetWindow(parentw, 5)
If UCase(GetCaption(firs%)) Like UCase(childhand) Then GoTo bone
firs% = GetWindow(parentw, GW_CHILD)

While firs%
firss% = GetWindow(parentw, 5)
If UCase(GetCaption(firss%)) Like UCase(childhand) & "*" Then GoTo bone
firs% = GetWindow(firs%, 2)
If UCase(GetCaption(firs%)) Like UCase(childhand) & "*" Then GoTo bone
Wend
FindChildByTitle = 0

bone:
Room% = firs%
FindChildByTitle = Room%
End Function

Here Is An Example Usage:
-------------------------------
//This finds the AOL welcome window
//and returns the User's ScreenName.
Function UserSN()
On Error Resume Next
AOL% = FindWindow("AOL Frame25", "America  Online")
MDI% = FindChildByClass(AOL%, "MDIClient")
welcome% = FindChildByTitle(MDI%, "Welcome, ")
WelcomeLength% = GetWindowTextLength(welcome%)
WelcomeTitle$ = String$(200, 0)
a% = GetWindowText(welcome%, WelcomeTitle$, (WelcomeLength% + 1))
User = Mid$(WelcomeTitle$, 10, (InStr(WelcomeTitle$, "!") - 10))
UserSN = User
End Function
-------------------------------------------------------------------------
Feel free to rewrite the labels and variables in the code.  They aren't important to me, and are probably making things more confusing.  Possibly some labels and variables with names of meaning would make the code a little easier to read.

Thanks,
 Chet
0
Comment
Question by:sleepy186
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
Radler earned 95 total points
ID: 2556204
Hi sleepy186,

You are lucky, I've this function already made, but your points %-(

Well, take, raise the points if aprecious the speed.

{---------------------------------------------------------------------------------------------}
function FindWindowChildByClass( const ParentWnd : HWnd; const ClassName : string) : HWnd;
var
      W1st, Wth : HWnd;
      WinClass : array[0..256] of Char;
begin
      Result:=0;
      W1st:=GetWindow(ParentWnd, GW_CHILD);
      if W1st <> 0 then begin
            Wth:=W1st;
            repeat
                  GetClassName(Wth,WinClass,SizeOf(WinClass));
                  if (StrComp(WinClass,PChar(ClassName)) = 0) then begin
                        Result:=Wth;
                        break;
                  end;
                  Wth:=GetNextWindow(Wth, GW_HWNDNEXT);
            until (Wth = 0)or(Wth = W1st);
      end;
end;

{---------------------------------------------------------------------------------------------}
function FindWindowChildByTitle( const ParentWnd : HWnd; const Title : string) : HWnd;
var
      W1st, Wth : HWnd;
      WinName : PChar;
begin
      Result:=0;
      W1st:=GetWindow(ParentWnd, GW_CHILD);
      if W1st <> 0 then begin
            WinName:=StrAlloc(256);
            Wth:=W1st;
            repeat
                  GetWindowText(Wth,WinName,255);
                  if StrPas(WinName) = Title then begin
                        Result:=Wth;
                        break;
                  end;
                  Wth:=GetNextWindow(Wth, GW_HWNDNEXT);
            until (Wth = 0)or(Wth = W1st);
            StrDispose(WinName);
      end;
end;

T++, Radler.
0
 

Author Comment

by:sleepy186
ID: 2560029
Adjusted points to 95
0
 

Author Comment

by:sleepy186
ID: 2560042
Hi,
     You saved me with those functions! I've raised the points to 95, hoping to still conserve some for me to use later on.
     I haven't gotten a chance to test the function yet, but if you could answer just one more question for me : Will the FindChildWindowByTitle work with only a partial title? For example, I'm looking for the AOL welcome window.  It says "Welcome, Screenname!" - Where Screenname can be anything.  Is there anyway I can search for a window with only knowing the beginning of the title? Thanks.
 Thanks.
0
 
LVL 4

Expert Comment

by:Radler
ID: 2564656
Hi I'm back;

To search a substring replaces

if StrPas(WinName) = Title then begin
Result:=Wth;
break;
end;

by

if Pos( StrPas(WinName), Title ) = 1 then begin //begins with Title ends anything
Result:=Wth;
break;
end;

T++, Radler.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question