Solved

SendMessage API C# Find and Click NO

Posted on 2009-04-08
2
972 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 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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