Solved

SendMessage API C# Find and Click NO

Posted on 2009-04-08
2
968 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 500 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now