1

My c# test target winform only have one button , when click it , will display a messageBox . it look like this :

enter image description here

with code :

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("msg");
    }

Now I want to use c++ and win-api to findwindows and sendmessage to above form , here is my code , first time I assume button1 in the first level child window of mainform :

#include <windows.h>
void main()
{


HWND windowHandle = FindWindowA(NULL, "Form1");   //Can’t find a proccess

if( windowHandle ) 
{
    HWND hwndChild = FindWindowEx(windowHandle, NULL, NULL, NULL);

    while ( hwndChild )
    {
        SendMessage(hwndChild, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0, 0));
        Sleep(100);
        SendMessage(hwndChild, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0, 0));

        hwndChild = FindWindowEx(windowHandle, hwndChild, NULL, NULL);
    }
}

return;
}

but this doed not effect , the c# application 's message do not display .

then , I assume button1 is in the second level of mainform 's child form , here is the code :

#include <windows.h>
void main()
{


HWND windowHandle = FindWindowA(NULL, "Form1");   //Can’t find a proccess

if( windowHandle ) 
{
    HWND hwndChild = FindWindowEx(windowHandle, NULL, NULL, NULL);

    while ( hwndChild )
    {
        HWND hwndChildChild = FindWindowEx(hwndChild, NULL, L"Button", NULL);
        if(hwndChildChild)
        {
            SendMessage(hwndChildChild, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0, 0));
            Sleep(100);
            SendMessage(hwndChildChild, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0, 0));
            break;
        }
        hwndChild = FindWindowEx(windowHandle, hwndChild, NULL, NULL);
    }
}

return;
}

But this time also does not effect , still no messagebox display . My question is : how to determine button1 's hierachy position of mainform , from search I know someone use spy++ , how can I use spy++ to know if button1 is child window of mainform , or child window of a certain child of mainform , and so on .

yangl
  • 337
  • 1
  • 3
  • 18
  • Button may be not a windows native window at all. This is common case for many frameworks. For example, Qt assignes HWND to a widget only if you put NativeWindow attribute for that widget. I suppose C# does similar thing. First of all you difinitely need to check your button with Spy++. Open it, press Spy->FindWindow and move circle on your button and see what happens. – Vladimir Tsyshnatiy Aug 22 '17 at 10:53
  • Jesus, this ain't going to end, now will it? [You can’t simulate keyboard input with PostMessage](https://blogs.msdn.microsoft.com/oldnewthing/20050530-11/?p=35513) (same principles apply to mouse input and `SendMessage`). If you absolutely **must** fake input, use [SendInput](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310.aspx). In this case, however, being a standard control, [UI Automation](https://learn.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview) is the safe way to automate a UI. – IInspectable Aug 25 '17 at 19:21

0 Answers0