Solved

FindChildByClass & FindChildByTitle (Delphi Win32 API)

Posted on 2000-02-24
4
360 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
[X]
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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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