🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8783

Re: FindWindow and FindWindowEx

#8783
From: JD
Date: Tue, 14 Sep 2004 19:17
78 lines
3650 bytes
"Pedram" <nover@hever.sever> wrote:
>
>Pedram wrote in message <4145f11b@newsgroups.borland.com>...
>>Assume a TForm named B_Form with a TMainMenu
>>There is Choice1 and Chioce2 in TMainMenu Items
>>Also there is a TEdit , TMemo and TButton...
>>This Project is Compiled , executed and Launched !
>>
>>I need to know how can I write a Code to Select B_Form from My Current
>>application by FindWindow() and FindWindowEx()

To reliably be able to locate the window, you need to name the
main form class something unique, like TCompanyNameProgramName
instead of TForm1 (or B_Form).

Or, you need to make sure that the Application::Title is
unique. However, if it's an MDI application, the Application
Title will change if there is an open MDIChild form so I never
use this method unless I have no choice.

Once you find a HANDLE to the application, you need to
enumerate the child windows to get HANDLE(s) to the other
controls in the window (form) to interact with them.

HANDLE hTheHandleOfTheAppYouWant = NULL;
// define other HANDLE(s) that you want.
//-------------------------------------------------------------
BOOL CALLBACK EnumWindowsCallBack( HWND hWnd, LPARAM lParam )
{
    char WindowClassName[256] = {0};
    if( ::GetClassName(hWnd, WindowClassName, 255) )
      {
          if( stricmp(WindowClassName, (char*)lParam) == 0)
            {
                hTheHandleOfTheAppYouWant = hWnd;
                // stop enuming
                return FALSE;
            }
      }
    return TRUE;
}
//-------------------------------------------------------------
BOOL CALLBACK EnumChildCallBack( HWND hWnd, LPARAM lParam )
{
    // we're looking for the second instance of "Static"
    char WindowClassName[256] = {0};
    if( ::GetClassName(hWnd, WindowClassName, 255) )
      {
          if( stricmp(WindowClassName, (char*)lParam) == 0 )
            {
                ++DlgCtrlCounter;
                if( DlgCtrlCounter == 2 )
                  {
                      DlgCtrlHWND = hWnd;
                      // stop Enuming
                      return FALSE;
                  }
            }
      }
    return TRUE;
}
//-------------------------------------------------------------

AnsiString WindowClassName = "TCompanyNameProgramName";
::EnumWindows( (WNDENUMPROC)EnumWindowsCallBack, (LPARAM)WindowClassName.c_str() );
if( hTheHandleOfTheAppYouWant )
  {
      WindowClassName = "Static";
      ::EnumChildWindows( hWnd, (WNDENUMPROC)EnumChildCallBack, (LPARAM)WindowClassName.c_str() );
  }

Now that you have the handles to the controls, what are you 
going to do with them? If it's your application, I would 
suggest that you just use custom messages that can be sent to 
the Application Handle and let it do the work.

~ JD
Technical Details
Message-ID: <414743e0$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4145f11b@newsgroups.borland.com> <4146f9fb@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f438dbb18610c215
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Re: FindWindow and FindWindowEx
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 172.136.12.199     
References: <4145f11b@newsgroups.borland.com> <4146f9fb@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <414743e0$1@newsgroups.borland.com>
Date: 14 Sep 2004 12:17:52 -0700
X-Trace: newsgroups.borland.com 1095189472 207.105.83.66 (14 Sep 2004 12:17:52 -0700)
Lines: 79
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail