FindWindow question.

Hey,

this question is related to icq.

ICQ's messenger window name is #32770.

So I do: handlexyz := FindWindow('#32770', nil);
The textbox of icq is a RichEdit20A.

In that messenger (#32770) window there is one RichEdit20A for the textbox where you type messenges and one RichEdit20A for the messeges of the other person.

So when I do a handlendxyz := FindWindowEx(WIN, 0, 'RichEdit20A', nil);
and then a SendMessage(hwndxyz, WM_GETTEXT, 255, integer(@Buffer));
then it sometimes returns the text of the input textbox and sometimes the text of the other person.

Do you have any ideas why sometimes it returns the input textbox text and sometimes the text of the other person. I guess it has something to do with the focus. The question is, do you have any idea how to get a handle on both textboxes?

Thnx!
TonyJizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ivanov_GCommented:
you have 2 controls with name "RichEdit20A" ... ?

I recommend you to check it again. As I a tool, I can recomment you is:
http://www.greatis.com/delphicb/windowse/

> "FindWindowEx(WIN, 0, 'RichEdit20A', nil);"
WIN is the the handle of the parent window. So you should do it like this:

handlexyz := FindWindow('#32770', nil);
handlendxyz := FindWindowEx(handlexyz, 0, 'RichEdit20A', nil);
0
DragonSlayerCommented:
Hmm... with ICQ it is a bit hard huh... relying on #32770 alone is quite 'dangerous', because that is the name used by all standard dialogs.

I guess what you can do after the FindWindowEx is... to check the caption of the window? Because ICQ message windows contain the phrase "Message Session" at the back.

Or, you can actually enumerate through all windows and find the one that contains "Message Session" as its windows title...
0
Eddie ShipmanAll-around developerCommented:
Dragon Slayer, the second parameter of FindWindow is the Window Title, no need to use FindWindowEx.
From MSDN:

"Syntax
  HWND FindWindow(      
    LPCTSTR lpClassName,
    LPCTSTR lpWindowName
  );

Parameters
    lpClassName
        [in] Pointer to a null-terminated string that specifies the class name or a class atom created
              by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in
              the low-order word of lpClassName; the high-order word must be zero.

        If lpClassName points to a string, it specifies the window class name. The class name can
        be any name registered with RegisterClass or RegisterClassEx, or any of the predefined
        control-class names.

        If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.

    lpWindowName
        [in] Pointer to a null-terminated string that specifies the window name (the window's title).
              If this parameter is NULL, all window names match.

Return Value

    If the function succeeds, the return value is a handle to the window that has the specified class
    name and window name.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError."
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

TonyJizAuthor Commented:
Ivanov,

I used that tool. The ICQ message window is just a textbox (RichEdit20A) with a line that seperates the textbox for input and the textbox for messages from the other user. So maybe that there only is one RichEdit20A, that's possible since sometimes the GETTEXT returns the input message and sometimes it returns the messages from the other user. I have no idea why.
About the WIN handle, i copy pasted that wrong, I did do it with the handlexyz and not the win handle.

DragonSlayer,

I don't just rely on #32770, also on the filename, so no need for the message window. It's just that sometimes RichEdit20A returns the input messages and sometimes the messages from the other user. I just want to retrieve the text from both message windows.

Thnx!
0
Russell LibbySoftware Engineer, Advisory Commented:

To answer your question, FindWindow(ex) works by finding the first window in the z-order that matches your criteria. FindWindowEx is a little more advanced, as you can specify a child window to start the search from, for example, in cases when you want to continue a search...

What is happening (I believe) is that the z-order is changing depending on focus, thus depending on when you call findwindowex, you may get either the upper or lower edit window.

An easy solution, and one that will give you both handles, is to call FindWindowEx twice. Once with a child handle of zero, and the second time with  the child handle from the first call. Then check the client rects to determine which is the upper, and which is the lower.

eg:

var  hwndICQ:       HWND;
     hwndSwap:      HWND;
     hwndEdit1:     HWND;
     hwndEdit2:     HWND;
     rcEdit1:       TRect;
     rcEdit2:       TRect;
begin

  // Get base window
  hwndICQ:=FindWindow('#32770', nil);

  // Check handle
  if (hwndICQ <> 0) then
  begin
     // Get first edit window in z-order
     hwndEdit1:=FindWindowEx(hwndICQ, 0, 'RichEdit20A', nil);
     // If non null, get next edit window
     if (hwndEdit1 <> 0) then hwndEdit2:=FindWindowEx(hwndICQ, hwndEdit1, 'RichEdit20A', nil);
     // Check handles
     if (hwndEdit1 <> 0) and (hwndEdit2 <> 0) and (hwndEdit1 <> hwndEdit2) then
     begin
        // Get rects for both windows
        Windows.GetClientRect(hwndEdit1, rcEdit1);
        Windows.GetClientRect(hwndEdit2, rcEdit2);
        // Swap if out of order (edit2's top is less than edit1's top)
        if (rcEdit2.Top < rcEdit1.Top) then
        begin
           // Swap handles
           hwndSwap:=hwndEdit1;
           hwndEdit1:=hwndEdit2;
           hwndEdit2:=hwndSwap;
        end;
     end;
  end;

end;

This will give you both handles, and it also allows you to determine which is which.

Hope this helps,
Russell





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TonyJizAuthor Commented:
That's some great stuff Russell! Points given.

Hey could you also goto http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21217560.html maybe you know that too.

Anyway, thnx!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.