?
Solved

SendMessage API C# Find and Click NO

Posted on 2009-04-08
2
Medium Priority
?
980 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

831 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