Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

FindChildByClass & FindChildByTitle (Delphi Win32 API)

Posted on 2000-02-24
4
358 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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