Run Action Upon Mouse Click and Mouse Release - c#

I am working on a Windows Form C# project that requires running code when a user clicks down on a button, then waiting, and running a different set of code when the user then releases the mouse button. I have tried Button1.MouseDown and MouseUp as something that was suggested when I did a search for this information, but neither of these worked.
Does anyone know how to do this?
Thanks.

In visual studios, try clicking on the button and in properties, view the "Events" menu. You may have to scroll down and find Events "MouseDown" and Events "MouseUp".
Double click on each of this, which will load the class to work with.
Inside of those, you may add your code.
You can create a timer if you wish running at 100ms and if the user clicks the button and holds, then the timer goes and the moment he releases, inside that, you could have timer1.stop();

Related

Coded UI Test can't access focus-visable button in winRow

When recording CUIT in Visual Studio 2015 (C#), I click inside a WinRow (inside a property grid-view table) which enables the ellipse [...] button control to click and open a pop-up window. However, the button is only visible when the WinRow is selected. So on playback, the WinRow loses focus between steps and the button is no longer visible to be clicked.
Does anyone know how to keep the focus inside the WinRow to be able to see/click the button?
Well if the WinRow is defined in your UIMAp Designer file, then you should add a clik on it just before the click on the button to put again the focus on the Winrow first.
But you will have to handcode it in external cs file because any modification on the UIMap designer will be automatically removed at it first next update...
Note that a lot of recorded actions may not replay properly, mostly when you are in a dynamic behavior context, so you may have to handcode your automatisation to playback each needed actions.

Enable press and hold for right-clicking with

I'm working with a WPF application.This application runs on a pc with a touch screen and when I push one button for more than 3 seconds MyButton_PreviewMouseUp event occurs and after that occurs a MyButtonSimple_PreviewMouseRightButtonDown event. I need to disable something for disabling the function of windows that do this.
You are not describing the default behaviour of a Button. In a normal WPF application, the PreviewMouseUp event occurs only when a pressed mouse button is let go. You can easily prove this yourself by adding a Button to a new WPF application and attaching a handler to the PreviewMouseUp event. You will see that it is only raised when you lift your finger off a pressed mouse button.
So this means that it is your code that is causing that behaviour and as you haven't shown us the relevant section (or indeed any code), we cannot help you with this further. I suggest searching your application for PreviewMouseUp to see if this event is being manually raised anywhere.

C# Suppressing mouse click for multiple mice

I have written an application that currently handles clicks from multiple mouse devices.
I used this project and modified to handle mouse clicks as apposed to keyboards.
This is working fine, however now I need to know if there is a way to suppress a click event if it has been handled by my app. The app is a quiz game so the idea is that the quiz master will have (and still be able to use) 1 mouse, and the other contestants will have their own mouse (as buzzers). So when they buzz in, I don't want the mouse click events to fire in the operating system (or at least this application).
The concept is the familiar override of void WndProc(ref Message message), and so I have tried not calling base.WndProc(ref Message) when I don't want the click events to fire, but this has not worked.
Can anybody point me in the right direction here?
Should I be going down the windows hook route? I have looked at this, but I can't seem to work out how I could hook to each mouse device individually.
Any help would be greatly appreciated.
Edit:
This is a Windows Form UI project, and not WPF. So the MultiPoint SDK from Microsoft won't work.
The solution to this lies within not WndProc, but PreFilterMessage(). By intercepting messages before they even reach the form, you can remove them from the message pump causing them to never reach the control that was clicked. This also works for child controls within the form.
I answered this and posted the full source in the following question:
C# Get Mouse handle (GetRawInputDeviceInfo)

Disable click on a 3rd party application

I'm writing an application that needs to record a click location on the screen, over the top of another window using the mouse. Is there anyway to disable the mouse click so that it doesn't affect the window that is being clicked over?
e.g. I want to set a point over the top of my browser, but I don't want to click anything within my browswer whilst setting it.
You have a couple options:
Write a plugin/extension for the browser you're talking about. Maybe the browser's interface allows you to record such information
Use windows hooks. Using SetWindowsHookEx and WH_MOUSE_LL, you can get all mouse activity on the computer fairly simply. You will just need to check if the mouse activity is happening in the browser, using WindowFromPoint.

Does NotifyIcon have a MouseDown equivalent?

I have a NotifyIcon in the system tray. How can I detect when the user has left-clicked down on it? I assumed the MouseDown event would be what I want to use but it only handles right click and middle-button click. For left-click, it only fires after the user has let go (as in they've just performed a normal click). Is there a way to get the MouseDown event only?
This is by design, the shell synthesizes the MouseDown message from the up event. You'll see why it works this way when you click and hold the button down and then start dragging. Note how the notification area overflow window pops up and lets you drag the icon into it to remove it from the visible area. It can't work both ways.
Technically you could hook the window owned by Explorer.exe to get a crack at the messages before Explorer does with SetWindowsHookEx(). That however requires a kind of DLL that you cannot write in C#, it needs to be injected into Explorer. Very destabilizing and hard to beat the competition that is trying to do the same thing. Also the kind of code that causes sleepless nights for the Microsoft appcompat team.
It appears that the underlying Win32 API Shell_NotifyIcon sends a WM_LBUTTONDOWN message when the user clicks the icon. According to MSDN anyway.
Examining the Windows Forms source code for NotifyIcon reveals standard mouse down event handling, so if the Win32 message was being sent at the "correct" time it would work as you want/expect.
I have to agree with a previous comment that NotifyIcon will be swallowing WM_LBUTTONDOWN since it needs to do mouse capture to allow the user to drag the icons about.
It's possible that this article about creating a tray icon for WPF will be useful since it shows how to use SetWindowsHookEx etc from C#.

Categories

Resources