?
Solved

SendMessage to "unknown" button in known window

Posted on 2003-03-06
9
Medium Priority
?
364 Views
Last Modified: 2012-06-22
Hi,
I am trying to send a Button Click command to a button that I only know the caption of in a form/window that I have the handle to... does any one know how to do this?

The solution I am trying now is to send key strokes to the window to step to the button and press it, but I have a problem here as well.... I cant figure out how to send the ctrl + tab command to change the field in the tab control in the form.
(what I need to send in this case is "tab, tab, tab,ctrl + tab, ctrl + tab, tab, enter")

The first solution to send the message directly to the button or to the WM_COMMAND in the window would be better, but I don't know what to send and where.... just as I said... the caption of the button and the handle to the form.

Thanks in advance!
/Tomas
0
Comment
Question by:tnson
[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
9 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8079433
Use FindWindow or FindWindowEx function. A button is really a window - a very small window compared to most other windows usually but nevertheless a window.

Search for a window with the specified caption. When you find it, you got the handle to it.

Alf
0
 
LVL 12

Expert Comment

by:Salte
ID: 8079438
Btw, be aware that the button is usually not immediate child of the form/window. It is often placed further down the hierarchy, so you might have to do a recursive search. Not sure if FindWindow does that recursion for you or if you must do a repeated call to FindWindow for each window's children.

Alf
0
 

Author Comment

by:tnson
ID: 8079704
Thanks for the quick answer.... if I'm not wrong you helped me with my last problem to :-)

When I try this with the main window:
myhandle = FindWindow(NULL,"Main window title");
I get a handle to the window, but when I try the same with the button:
myhandle = FindWindow(NULL,"Auto Condition...");
I don't get any handle at all....

But how do you mean that I should search in the windows children?

/T
0
Independent Software Vendors: 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!

 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 8080321
Yeah,

For each of that window's children do a recursive search through their children and children's children etc until you find a window of type "BUTTON" (I think that's the window type for a button, not sure about that though) and with title whatever you see in the caption of that button.

Something like this:

HANDLE find_window(HWND root)
{
   if (root is the window we want)
      return root;

   HWND child = GetFirstChild(root);
   HWND hwnd;

   while (child != 0) {
      if ((hwnd = find_window(child)) != 0)
         return hwnd; // found it.

      child = GetNextSibling(child);
   }
   return 0;
}

GetFirstChild and GetNextSibling is possibly implemented in Win32 by a generic FindWindow where you specify a paramaeter indicating the relation between input window and the window you want to return. It is important that these functions do not specifically look for button or something, that test is done in the 'if root is the window we want' test.

The GetFirstChild and GetNextSibling just return those windows whatever they are.

Alf
0
 

Author Comment

by:tnson
ID: 8081217
For some reason I can't reach to the tab I want.... I can se all the others, but not this one....

Do you know how to PostMessage so that I simulate that I press ctrl and tab?
I think that PostMessage is a faster solution to solve this for now. =)
As I do it now I send this to go where I want, but I cant get the ctrl + tab to work.
     PostMessage(myhandle, WM_KEYDOWN, 9, 0);
     PostMessage(myhandle, WM_KEYUP, 9, 0);
     Sleep(50);
     PostMessage(myhandle, WM_KEYDOWN, 9, 0);
     PostMessage(myhandle, WM_KEYUP, 9, 0);
     Sleep(50);
     PostMessage(myhandle, WM_KEYDOWN, 9, 0);
     PostMessage(myhandle, WM_KEYUP, 9, 0);

        And here I would like to send something like:
     PostMessage(myhandle, WM_KEYDOWN, (Control + tab), 0);

/T
0
 

Expert Comment

by:WaffleSouffle
ID: 8081548
Ok, no time for compiling, but something like:

struct SFindIt
{
  HWND hwndFind;
  char *pszFind;
};

BOOL CALLBACK EnumChildProc (HWND hwndChild, LPARAM lParam)
{
  BOOL bCarryOn = TRUE;
  const int LOTS = 512;
  SFindIt *pfiSearch = SFindIt *(lParam);
  char szBuffer[512];
  ::GetWindowText(hwndChild, szBuffer, LOTS);
  if (strcmp (pfiSearch->pszFind, szBuffer) == 0)//If found
  {
    piSearch->hwndFind = hwndChild;
    bCarryOn = FALSE;

  } // Ends if found
  else // Else not found
  {
    bCarryOn = EnumChildWindows (hwndChild,
                                 &EnumChildProc,
                                 lParam);

  } // Ends else not found

  return bCarryOn;
}

void CSomeWindow::SearchForChild(char *pszText)
{
  SFindIt fiSearch;
  fiSearch.hwndFind = NULL;
  fiSearch.pszSearch = pszText;
  EnumChildWindows (GetSafeHwnd(),
                    EnumChildProc,
                    LPARAM(&fiSearch));
  if (fiSearch.hwndFind != NULL) // If found
  {
    // Do something

  } // Ends if found
0
 

Author Comment

by:tnson
ID: 8086477
I have made the same as in the example you sent, but it still does not work..

When I use the spy++ it seams as the relations is strange between the childs in the main window.
With spy++ I cant find the window (button) if I search from the main window and down in the structure, but if I go from the "button" back upp to the main window it works.

Strange...
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9889592
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Salte

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

752 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