[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SendMessage API C# Find and Click NO

Posted on 2009-04-08
2
Medium Priority
?
978 Views
Last Modified: 2013-12-03
I'm attemping to create an automated process for closing a particular Dialog Box which pops up when an application crashes.

Basically a message box appears: "Start in Safe Mod?" [Yes] [No] [Cancel]

I have created an API that can close that dialog box but that won't work. I need it to actualy send a key N or click on the NO button.

(I'm still a bit novice with Windows API)

The code below closes it.

I tried to use WN_KEYDOWN / WM_KEYUP /WM_CHAR but nothing happen. :( I guess it wasn't as simple as that. Anyone know what I need to do? Like find the button handle? any way of searching for all the button handle ids?


NativeWIN32.SendMessage(hwnd, NativeWIN32.WM_SYSCOMMAND, NativeWIN32.SC_CLOSE, 1);  // Works
 
 
NativeWIN32.SendMessage(hwnd, NativeWIN32.WM_CHAR, Convert.ToInt32('n'), 1); // Doesn't work

Open in new window

0
Comment
Question by:ultramoo
[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
2 Comments
 
LVL 5

Accepted Solution

by:
burningmace earned 2000 total points
ID: 24106211
There's a somewhat botch-like way round of doing it, which involves simulating the mouse click. It works well despite not being the most elegant solution. Use the following steps:

1) FindWindow the dialog
2) Use GetWindowRect to find the position of the dialog window.
3) Calculate the X/Y coordinates of the button based on the amount it is offset from the window position (experiment with this until you find a value that works). It shouldn't be too hard to estimate this value.
4) Use GetCursorPos to store the current cursor position. Store this value.
5) Use SetCursorPos to jump the cursor to the position of the button you calculated above.
6) Use mouse_event to simulate a click.
7) Use SetCursorPos to jump the cursor back to its original position.

The idea is that you can quickly flick the cursor over the button, click yes, and flick it back at such a speed that the user doesn't notice at all. It would be as though the box dismissed itself.

Side note: You can use System.Windows.Forms.Cursor.Position instead of SetCursorPos if you wish, I have not tried this though.

Side note #2: A useful resource for .NET signatures of Win32 APIs and related sample code is pinvoke.net
0
 

Author Closing Comment

by:ultramoo
ID: 31568355
Thanks, took me a long time to figure it all out but finally got it working. Not the best way but it does the job

cheers.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

656 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