Categories
NETCF

Windows Mobile Context Menu Behaviour On Windows CE

Although many Windows CE devices include the aygshell.dll component which offers some functionality available in the Pocket PC shell, it’s not directly taken advantage of by .NETCF. For example, when you add a ContextMenu to a control in a Pocket PC project you automatically get tap-and-hold behaviour on the control. Run the same code on an aygshell equipped CE device and nothing happens. Therefore I wrote the following helper class to allow you to hook up the context menus. Simply call HookAllControls(Me.Controls) from your code (e.g. in your form constructor after the call to InitializeComponent() ) or at any other stage if you are dynamically creating controls on your form. Now when you run your app you’ll get the tap and hold circles and your context menu will be displayed. Obviously this only works on CE devices which have aygshell support. Just to mix it up a bit this sample is in VB.NET but you should find it easy to convert to C#.


Namespace InTheHand.Windows.Forms


Public Class ContextMenuHelper


Private Shared mousedelegate As System.Windows.Forms.MouseEventHandler = New System.Windows.Forms.MouseEventHandler(AddressOf ControlMouseDown)

Public Shared Sub HookAllControls(ByVal thecontrols As Control.ControlCollection)

‘attach to the mousedown event on all controls with a context menu
For Each thiscontrol As Control In thecontrols

If Not thiscontrol.ContextMenu Is Nothing Then

AddHandler thiscontrol.MouseDown, mousedelegate

End If

HookAllControls(thiscontrol.Controls)
Next

End Sub


Private Shared Sub ControlMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

Dim senderctrl As Control = CType(sender, Control)

‘if theres no context menu do nothing
If Not senderctrl.ContextMenu Is Nothing Then

Dim shrgi As New SHRGINFO
shrgi.cbSize = 20
shrgi.hwndClient = senderctrl.Handle
shrgi.dwFlags = 3
shrgi.ptDownx = e.X
shrgi.ptDowny = e.Y
Dim result As Integer = SHRecognizeGesture(shrgi)
If result = 1000 Then

senderctrl.ContextMenu.Show(senderctrl, New System.Drawing.Point(e.X, e.Y))
End If
End If

End Sub

<System.Runtime.InteropServices.DllImport(“aygshell.dll”)> _
Private Shared Function SHRecognizeGesture(ByRef shrg As SHRGINFO) As Integer
End Function

Public Structure SHRGINFO
Public cbSize As Integer
Public hwndClient As IntPtr
Public ptDownx As Integer
Public ptDowny As Integer
Public dwFlags As Integer
End Structure

End Class

End Namespace

By Peter Foot

Microsoft Windows Development MVP