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.FormsPublic Class ContextMenuHelperPrivate 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 menuFor Each thiscontrol As Control In thecontrolsIf Not thiscontrol.ContextMenu Is Nothing ThenAddHandler thiscontrol.MouseDown, mousedelegateEnd IfHookAllControls(thiscontrol.Controls)NextEnd SubPrivate 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 nothingIf Not senderctrl.ContextMenu Is Nothing ThenDim shrgi As New SHRGINFOshrgi.cbSize = 20shrgi.hwndClient = senderctrl.Handleshrgi.dwFlags = 3shrgi.ptDownx = e.Xshrgi.ptDowny = e.YDim result As Integer = SHRecognizeGesture(shrgi)If result = 1000 Thensenderctrl.ContextMenu.Show(senderctrl, New System.Drawing.Point(e.X, e.Y))End IfEnd IfEnd Sub<System.Runtime.InteropServices.DllImport("aygshell.dll")> _Private Shared Function SHRecognizeGesture(ByRef shrg As SHRGINFO) As IntegerEnd FunctionPublic Structure SHRGINFOPublic cbSize As IntegerPublic hwndClient As IntPtrPublic ptDownx As IntegerPublic ptDowny As IntegerPublic dwFlags As IntegerEnd StructureEnd ClassEnd Namespace
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.