[3.0/LINQ] Simulate user input presicely
I have a class that derives from Form that won't be able to get mouse or key input normally. I have outside classes that monitor the mouse and keyboard. I want to be able to send keypresses / mouse events etc. just as if the form had received them its self.
I've tried things like calling OnMouseDown, OnClick etc. but the problem, as illistrated by these 2 methods is that a child's click / mousedown event won't get raised if appropriate. i.e. i call onmousedown with mouse coordinates that would be over a control on the form, but the event is never raised on the control.
Anyone know how i can achieve the effect i'm after?
Re: [3.0/LINQ] Simulate user input presicely
You should be sending Windows messages to the form, which will cause it to raise its own events. You need to call the SendMessage API. Create a regular form and override its WndProc method. You can then monitor the messages it receives under various circumstances and reproduce them yourself.
Re: [3.0/LINQ] Simulate user input presicely
If that works (child controls get the messages/events as appropriate) then that'll be perfect.
Cheers!
I'll check that the child control's events get fired as appropriate then i'll mark this thread resolved and give you some lovely lovely rep points.
Re: [3.0/LINQ] Simulate user input presicely
Hmm, i've tried using the send message approach, but it doesn't seem to work as i'd hoped.
If i send a WM_MOUSEDOWN message to the form's handle the form's OnMouseDown method does get fired, and if i check the mouseeventargs passed into it i know i'm passing the correct lparam and wparam to indicate the mousebutton and cursor position.
However, even if i click on my usercontrol the onmousedown method is never ran (i checked with a breakpoint).
Any ideas as to why this could happen?
If i sent a message using this method to an external applicaion would it behave as i want it to here? i.e. would a child window have its click event called? If not then i'm a bit stuck, if it does behave like that then i'm obviously doing something wrong.
The way i'm sending the message to my form is by calling its WndProc method with my newly created Message object. (i exposed the WndProc with a helper method that calls it).
Re: [3.0/LINQ] Simulate user input presicely
When you were creating your fancy Form class, did you happen to use SetStyle() to alter the handling of mouse events?
Re: [3.0/LINQ] Simulate user input presicely
Nope.
I've just created a test winforms project to test this, and that doesn't work either so i know the problem isn't with my derived classes.
To test it i created a test form with 2 buttons, one at location 0,0 and another somewhere else. In the 2nd button's click event i send a message to the form that clicks at a location inside the 1st button. However, the 1st button's click event, or mouse down event doesn't get called
Re: [3.0/LINQ] Simulate user input presicely
It's been a while since I meddled in this, but wouldn't (0,0) be underneath the form's titlebar? I don't know what x,y coords you used in your message, nor how big your button is.
Try making the button 100w x 100h at position 0,0 and send the click message to 99,99.
Re: [3.0/LINQ] Simulate user input presicely
Good idea, but no luck still.
I'm starting to think that sending a message to a parent window simply won't send any message or anything to a child window.
Re: [3.0/LINQ] Simulate user input presicely
Cracked it! The parent window never gets a mousedown message when one of its child windows receives one.
I got it to work by simply working out what control was clicked then sending the message to that.