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
Solved

SendMessage API C# Find and Click NO

Posted on 2009-04-08
2
971 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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 42
Service Controller 1 27
Add different cell to otherwise similiar row 4 45
How come this regular expression allows "e"? 14 45
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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