Solved

Generate clicks for winform app

Posted on 2012-03-25
7
506 Views
Last Modified: 2012-03-27
I have a winform app (C# 4.0). i'm testing a race condition and need to click repeatedly very fast on a form panel, faster than I can click on my mouse. Is there a way to do this?

This click utility should be external to the program itself, since I'll need to run it on the current production version as well as the corrected development version. If the solution involves writing a separate program, would prefer C#.
0
Comment
Question by:BlearyEye
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 37762902
Have you heard of AutoIT? I just tested it out for your specific scenario. It would be one less thing you have to write (well, you'd only have to write a quick script for the clicking).

To simulate your example, I wrote the following in AutoIT's editor:

For $i = 0 To 10000
  ControlClick("Form1", "", "[ID:329122]")
Next

Open in new window


In my test, my form had the title (Text property) of "Form1". You would change this to exactly match the text of your form. The ID was attained by using the Window Info tool which comes with AutoIT:

Window Info Tool
Within this tool, there is a small target icon. If you've ever used Spy++, then you should have an idea of what this feature does. If you haven't, then you can actually click and drag the little target to any form on your desktop. As you mouse over a form, the border of that form will be highlighted. This will be your active target. Further, you can select specific controls from a form. Likewise to the form, a control's border will highlight when you mouse over it. When you let go of your mouse button, the currently highlighted control's information will be populated within the Window Info window.

The "Title" field is the same text as what I mentioned above for the form, so you could also use this tool to grab that value as well. If you switch to the "Control" tab, as I have in the screenshot above, you will get access to the ID property for the control you selected. This value is what you would put in the script above.

The above script is going to execute "faster than [you] can click with a mouse," and if you need to slow it down a bit, you can include a Sleep call in your script.

e.g.

For $i = 0 To 10
  ControlClick("Form1", "", "[ID:656868]")
  Sleep(1000) ' in milliseconds
Next

Open in new window


Realize that the scripting language is VB/VBA-esque. The tool has decent documentation, and since it's similar to VB, you can adapt a lot of VB code to be used within the tool. The tool itself is pretty popular, so even if you get stuck, you should be able to find help with any problems you encounter.

At least it's an option  = )
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 37762917
If you want to do it manually, then you'll need to obtain a handle the panel first.  Do you know how to that?

Assuming so, then you'd use SendMessage() to send a mouse down message (and possibly a follow-up mouse up message depending on what you're doing):

        private const int WM_LBUTTONDOWN = 0x201;

        [DllImport("User32.DLL")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private void button1_Click(object sender, EventArgs e)
        {
            SendMessage(panel1.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
        }
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37762934
For Idle_Mind's method, I believe you would also need FindWindow (maybe FindWindowEx) since you mention the click utility's "external" requirement.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40
ID: 37763553
If this apply to your situation, typically if no other mouse events are involved, you can call the click event from inside the application itself.

Simply add a button or menu on the form to start your test, something that is visible only by an administrator or through a back door if you do not want the users to see it.

Prepare a loop, maybe with a short Sleep instruction to be closer to a reall life situation, and call your event handler in every loop:

Button1_Click (null, null);
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 37771808
Am checking into AutoIT ... JamesBurger: can't add a button to the app since I need to test a deployed version.
0
 
LVL 40
ID: 37771852
As I told you, the button can be hidden by a backdoor so that only you have access to it for your tests.

A nice thing about that feature is that you can still test at the users station after the application is deployed if you find problems on some of the computers. The user will never see the button. And a hidden button or menu with the simple code that the test would require does not add a significant overhead to the application.
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 37775187
After a bit of effort, AutoIT did the trick. Nice tool. I had to use MouseClick rather than ControlClick, for some reason that I did not quite divine; the control in question has a MouseDown event that changes the focus, as well as a Click event.

Idle_Mind's solution would also have worked, tho it would have required a C# app which is a bit more complex.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

15 Experts available now in Live!

Get 1:1 Help Now