Azurewrath

Hi,

I am automating some stuff via COM, but for some reason one action brings up a dialog with OK, and Cancel buttons. Is there a way to press that OK button in my code

Thanks,

aw



Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

nobugz

You could SendKeys.Send() the Enter key...





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

Azurewrath

Thanks for replying. How can I send it to the application, if it's not the forefront application that has the focus Or is there a way to focus another application

Thanks,

aw





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

Azurewrath

I tried this:

SendKeys.Send ( "{ENTER}" );

But it doesn't work. I use it in the load event of the form. But when I manually press enter, it works, which I believe that photoshop is the active app, so sending keys should work.

Thanks,

aw





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

deodorant2

Try looking into the FindWindow/FindWindowEx/SendMessage API's and the BM_CLICK window's message.

If you can't figure it out don't hesitate to ask.

-Deodorant





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

deodorant2

Ah, it started to rain here, can't go to the beach now so I whipped up some code to solve your problem:

Code Snippet

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindow(string lpClassName,

string lpWindowName

);

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindowEx(IntPtr hwndParent,

IntPtr hwndChildAfter,

string lpszClass,

string lpszWindow

);

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr SendMessage(IntPtr hWnd,

uint Msg,

int wParam,

int lParam

);

private const uint BM_CLICK = 0x00F5;

private const uint WM_LBUTTONDOWN = 0x0201;

private const uint WM_LBUTTONUP = 0x0202;

private void PushOKButton()

{

IntPtr ptrWindow = FindWindow("#32770", "Confirm");

if (ptrWindow == IntPtr.Zero)

return;

IntPtr ptrOKButton = FindWindowEx(ptrWindow, IntPtr.Zero, "Button", "&Yes");

if (ptrOKButton == IntPtr.Zero)

return;

SendMessage(ptrOKButton, WM_LBUTTONDOWN, 0, 0);

SendMessage(ptrOKButton, WM_LBUTTONUP, 0, 0);

SendMessage(ptrOKButton, BM_CLICK, 0, 0);

Console.WriteLine("If we made it this far, the button should have been hit!");

}

You'll have to change the FindWindow/FindWindowEx lines to make them work for your application.

Hope this helps.

-Deodorant





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

Azurewrath

Thanks for writing it. I will try it on Monday, I have arrived home now Smile

I will report the result.

Thanks so much again,

aw





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

Azurewrath

Hi again,

I tried your code, but didn't make it to work. I searched for findwindow and findwindowex functions, but thought findwindowex should be used before, because I don't know the handle of the window before searching for it. I think you use a different method to find the right window's dialog, but I don't know how it works.

Can you please show me, how I would change it to find the parent window named "Adobe Photoshop"

Thanks again,

aw





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

deodorant2

I used a program called WinDowse to obtain the window's classname and title. (If you have Spy++, that will work, too) Or, you can use the classname API. (Example)

FindWindow will return an IntPtr that you later use in FindWindowEx to dig deeper into the window.

-Deodorant





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

Azurewrath

Hi again, I downloaded windowse, but still couldn't make it to work. For instance what's the "#32770" value in your code As for class name I get PSFloatS, is this the one Can you please explain in more detail as I have never done this before.

Thanks again,

aw





Re: Windows Forms General How to press a button in another application's dialog (OK Button)?

deodorant2

#32770 is the class name of the messagebox that popped up in another app I wrote.

I can't tell you if that the class name or not, but if you select the "Class" tab and then put the mouse over the messagebox window, it will tell you the class name to put in the FindWindow(...) API.

FindWindow(ClassName, Title);