Solved

FindChildByClass & FindChildByTitle (Delphi Win32 API)

Posted on 2000-02-24
4
348 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
Comment Utility
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
Comment Utility
Adjusted points to 95
0
 

Author Comment

by:sleepy186
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now