In a winform, the form will follow the mouse when the mouse is down, but sometimes when the machine particularly is slow, the form is following the mouse even the mouse is out, so I used win32 dll to judge the state of the mouse
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(int nVirtKey);
public static bool GetCurrentLeftMouseIsDowning()
{
if (GetAsyncKeyState(0x01) == 0)
return false;
else
return true;
}
But the memory will increase when the form has been dragging, and what other way to determine the state of the mouse?Or how to control the memory when the form is dragged??
Considering you are using winform, you can use form1_mousedown event for this
refer this
Related
I am working on a WPF application and want to set the cursor position to the corner of the screen.
I tried using the WinForms approach:
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(0, 0);
and the user32.dll approach:
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
The WinForms approach worked sometimes, but I don't understand why it worked and didn't work sometimes. I made sure the code is actually being called. The user32.dll approach didn't work for me at all.
Simple solution is make your application enough big and move mouse inside of it.
http://www.aspdotnet-pools.com/2017/10/move-mouse-cursor-automatically-c.html
To move mouse outside your application you need global mouse hook.
Global mouse event handler
I have two monitors, one of which is a touch screen device. I want to prevent the mouse from moving to the touch screen device when a touch event is triggered.
So , I use Hook_LL
As long as this object exists all mouse events created from a touch event for legacy support will be disabled. Or, at least, that's the promise. But the system mouse-cursor still gets set to the position of the last touch-event, and so far, there seems to be no way to prevent that.
static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var info = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
// 64-bit
var extraInfo = (ulong)info.dwExtraInfo.ToInt64();
if ((extraInfo & MOUSEEVENTF_MASK) == MOUSEEVENTF_FROMTOUCH)
{
// Prevent Touch -> Mouse Event message delivery.
return new IntPtr(1); // if ((extraInfo & 0x80) != 0) is Touch Input,other is Pen Input.
}
}
return UnsafeNativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam);
}
I use GetCursorPos that the scheme was effective, even though the mouse disappeared.
Unfortunately, when the touch down is triggered, I don't move the mouse, just click the left mouse button, but click down is still triggered on the touch screen device.
Although I can disable click down triggering on the touch screen device, I want to respond to click down at the position of the mouse.
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
I hope that after clicking the touch screen button, when clicking with the mouse, the click event will respond to the original position instead of the button on the touch screen.
How to reset mouse cursor position c#
How to avoid mouse move on Touch
I'm looking for an api that can cause a windows select/highlight event to occur on a windows desktop, without actually causing the mouse cursor to move.. I can cause the mouse cursor to move with :
public static extern bool SetCursorPos(int X, int Y);
But that moves the actual cursor to that point... I'm looking for a way to highlight only as one might do by using the tab and arrow keys to move around the windows desktop. Any suggestions are appreciated..
regards, rob
I think you may be looking for SetFocus. You can get a control's handle with Control.Handle or FindWindow , and p/invoke SetFocus (use IntPtr as the argument type).
i need to implement a cursor with some very specific features:
it has to be animated
because after n seconds it automatically clicks - so the animation is feedback for the user when the click will happen
it has to snap to some of our controls
it has to work outside of our application
the approaches so far:
render my WPF-control into a bitmap, make a cursor-struct out of it and use user32.dll/SetSystemCursor to set it
PRO
the cursor has no delay after the mouse since it's a real cursor
CON
snapping is quite hard, especially since we have absolute and relative inputdevices and i would have to reset the mouseposition all the time or use user32.dll/ClipCursor (System.Windows.Forms.Cursor.Clip does the same) but the snapped cursor is always shaking around the snapped position (tries to escape, get's reset again....)
the code i use throws strange exceptions after some random time - so my current code seems quite unstable
render my own cursor into a maximized, topmost, allowtransparent, windowstyle=none, invisible window and manually move the cursor after the mouse (like Canvas.SetLeft(cursor, MousePosition.X))
PRO
snapping can be (easily) done
CON
when the mouse clicks and hit's the cursor the cursor get's clicked and not the window beyond
polling the mouseposition in a dispatcher-background-loop all the time doesn't seem very beautiful to me
to solve the second approach my cursor would have to have at least one transparent pixel
in the hotspot, so that the mouse can click through... that doesn't seem like a real solution to me...
any idea's anyone?
EDIT:
some example-source to show the problems...:
example app & source to show the problem with snapping the mouse to a fixed position: ClipIt.rar
example app & source that fails after random time - setting a self-drawn cursor: TryOwnCur.rar
can be found under: http://sourcemonk.com/Cursor
thanks to http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f
i can click through my self-drawn cursor which follows the mouse-position by
setting the window style:none, and allowtransparent as i already did and
then
public const int WS_EX_TRANSPARENT = 0x00000020;
public const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hwnd,
int index);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hwnd,
int index, int newStyle);
public static void makeTransparent(IntPtr hwnd) {
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
and call makeTransparent from OnSourceInitialized...
I know how to stimulate clicks using User32 SendInput method and what I need is a similar User32 method but to obtain the current mouse button state.
Something similar to:
public static extern bool GetCursorPos(ref System.Drawing.Point lpPoint);
Function GetCursorPos gives me the current cursor position. What I need is the left button state (if it's clicked or not). Is there such a function?
Use GetAsyncKeyState, To Quote MSDN:
The GetAsyncKeyState function works
with mouse buttons. However, it checks
on the state of the physical mouse
buttons, not on the logical mouse
buttons that the physical buttons are
mapped to. For example, the call
GetAsyncKeyState(VK_LBUTTON) always
returns the state of the left physical
mouse button, regardless of whether it
is mapped to the left or right logical
mouse button. You can determine the
system's current mapping of physical
mouse buttons to logical mouse buttons
by calling
GetSystemMetrics(SM_SWAPBUTTON).
There's a method called GetAsyncKeyState. The method signature looks like this:
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(UInt16 virtualKeyCode);
Then you simply call it passing the left mouse key code (VK_LBUTTON = 0x01) and off you go.
More information directly from MSDN.