VS2012 Press & Hold for Right Click.
Hi,
I've created a win forms application that is designed to run on a windows 8 tablet, however I have one issue that I cannot seem to resolve.
The application allows the users to move remote objects by holding down directional buttons, this works fine with actual mouse clicks and mouse down events but when operated using a finger press on the tablet the mouse down event becomes a right button click and the remote device stops moving.
I've read about the stylus class and ways to disable the 'press and hold for right mouse' but this only applies to WPF, does anybody have a method of disabling this on a win forms application ?
Thanks in advance.
Re: VS2012 Press & Hold for Right Click.
No, the mouse down event doesn't become a click. The point is that the button is actually irrelevant except as a location. It is the mouse down event which does all the work and, of course, if you don't use the mouse, there is no mouse down event. The click is always fired by the button however it is operated. I don't (and won't!!!) use W8 or a touchscreen of any kind but surely there is some kind of "finger down" event equivalent to mouse down otherwise drag and drop etc. would be effectively impossible?
Re: VS2012 Press & Hold for Right Click.
Thanks for your reply, what you are saying regarding the mouse down event does make sense, I can however get it to work by tapping the button quickly then holding it down and the mousedown code executes as expected but this is pretty hit and miss.
There is as far as I can tell no 'finger down' type event and I'm sure that the mousedown event at least initially fires as the device begins to move as all the movement code is within the mousedown event but then stops once the 'perceived' right click takes place.
I think the key to the solution is to disable this action if possible.
Re: VS2012 Press & Hold for Right Click.
Well you could just use a disabled button! But that may not look right. In which case I'd be tempted simply to draw your own pseudo-buttons which obviously won't have any click event. (I was gonna suggest using another control without a click event but I don't think there is one!)
Re: VS2012 Press & Hold for Right Click.
Looks like the answer is to stop the window receiving the event using this method.
http://msdn.microsoft.com/en-gb/libr...=vs.85%29.aspx
It steps beyond my comfort zone but hopefully I'll figure it out.
Re: VS2012 Press & Hold for Right Click.
OK need a little advice on this, I've had a few goes at it and done a fair bit of reading but I must be missing something as I can't seem to disable the press and hold function.
Here are the functions and constants that have been declared
Code:
Public Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Public Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal hData As Integer) As Integer
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Public Declare Function GlobalDeleteAtom Lib "kernel32" Alias "GlobalDeleteAtom" (ByVal nAtom As Integer) As Integer
Const SWP_HIDEWINDOW = &H80
Const SWP_SHOWWINDOW = &H40
Dim taskBar As Integer
Dim WindowHandle As Integer
Const WM_SYSCOMMAND As Integer = &H112
Const SC_RESTORE As Integer = &HF120
Const WM_TABLET_DEFBASE As Integer = &H2C0
Const WM_TABLET_QUERYSYSTEMGESTURESTATUS As Integer = WM_TABLET_DEFBASE + 12
Const TABLET_DISABLE_PRESSANDHOLD As Integer = &H1
And here's how I'm implementing it, I also hide the taskbar as well which does work !
Code:
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_TABLET_QUERYSYSTEMGESTURESTATUS Then
'Call the GlobalAdd atom Function
Dim atom As String = GlobalAddAtom("MICROSOFT_TABLETPENSERVICE_PROPERTY")
'Call the setProc function
SetProp(WindowHAndle, atom, TABLET_DISABLE_PRESSANDHOLD)
GlobalDeleteAtom(atom)
End If
If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32 = SC_RESTORE Then
SetWindowPos(taskBar, 0&, 0&, 0&, 0&, 0&, SWP_HIDEWINDOW)
End If
MyBase.WndProc(m)
End Sub
As I said earlier, this is outside my current comfort zone so any advice would be greatly appreciated.
Re: VS2012 Press & Hold for Right Click.
You're allowing the message to pass through to the normal windows routines which are therefore undoing any changes you make.
vb.net Code:
Protected Overrides Sub WndProc(ByRef m As Message)
Dim AllowNormalProcessing As Boolean = True
If m.Msg = WM_TABLET_QUERYSYSTEMGESTURESTATUS Then
'Call the GlobalAdd atom Function
Dim atom As String = GlobalAddAtom("MICROSOFT_TABLETPENSERVICE_PROPERTY")
'Call the setProc function
SetProp(WindowHAndle, atom, TABLET_DISABLE_PRESSANDHOLD)
GlobalDeleteAtom(atom)
AllowNormalProcessing = False
End If
If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32 = SC_RESTORE Then
SetWindowPos(taskBar, 0&, 0&, 0&, 0&, 0&, SWP_HIDEWINDOW)
AllowNormalProcessing = False
End If
If AllowNormalProcessing Then MyBase.WndProc(m)
End Sub
Re: VS2012 Press & Hold for Right Click.
Quote:
Originally Posted by
dunfiddlin
Well you could just use a disabled button! But that may not look right. In which case I'd be tempted simply to draw your own pseudo-buttons which obviously won't have any click event. (I was gonna suggest using another control without a click event but I don't think there is one!)
Couldn't he simply inherit the button control and override click event? Therefore he'd then be able to theoretically preventing the click events from being fire without disabling the button itself.
vbnet Code:
Public Class CustomButton
Inherits System.Windows.Forms.Button
Sub New()
MyBase.New()
Call InitializeComponent()
End Sub
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
'MyBase.OnClick(e) ' Prevents this event from firing.
End Sub
End Class
Re: VS2012 Press & Hold for Right Click.
In theory, certainly. But then will he need to also disable MouseClick? And having done so, what about MouseDown? It would be very easy to end up just chasing your tail (if you'll pardon the punning allusion!) If there is a way to say "I'm doing a drag now so I don't want any nonsense from you other events!" that would be the better option.
Re: VS2012 Press & Hold for Right Click.
Thanks for the help unfortunately if I prevent the MyBase.Wnproc(m) from executing with the boolean flag, the form does not load and I get the folowing exception
'An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll
Additional information: Error creating window handle.'
From what I have read I just need to respond to the WM_TABLET_QUERYSYSTEMGESTURESTATUS message with the constant for disabling the 'press and hold for click' gesture, sounds pretty straight forward but to be honest I'm pretty stuck.
I'm also sure it has nothing to do with buttons as no matter where I press and hold the right click is done but at a lower level than my code if that makes sense.
Re: VS2012 Press & Hold for Right Click.
Try it with the Boolean on the first If only. I almost left it at that anyway. Unfortunately I have no way to lay hands on a touchscreen device to check it for myself.
Otherwise we may have to go back to some of the original suggestions.