[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

FindChildByClass & FindChildByTitle (Delphi Win32 API)

Posted on 2000-02-24
4
Medium Priority
?
371 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 380 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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