Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


SendMessage to "unknown" button in known window

Posted on 2003-03-06
Medium Priority
Last Modified: 2012-06-22
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!
Question by:tnson
LVL 12

Expert Comment

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.

LVL 12

Expert Comment

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.


Author Comment

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?


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 12

Accepted Solution

Salte earned 200 total points
ID: 8080321

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.


Author Comment

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);
     PostMessage(myhandle, WM_KEYDOWN, 9, 0);
     PostMessage(myhandle, WM_KEYUP, 9, 0);
     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);


Expert Comment

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,

  } // Ends else not found

  return bCarryOn;

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

  } // Ends if found

Author Comment

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.

LVL 11

Expert Comment

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).


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

564 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