Code Repo    |     RSS
MD's Technical Sharing



Monday, March 9, 2009

Add Cut/Copy/Paste functionality to WM Textboxes

Allow user to display a context menu with cut/copy/paste functionalities to textboxes by tap & hold, y using Windows CE's SIPPREF control to automatically implement default input panel behavior for a dialog. It provides the following features:

  • The Input Panel is automatically shown/hidden as controls gain and loose focus.
  • Edit controls have an automatic context menu with Cut, Copy, Paste type options.
  • The SIP state is remembered if the user switches to another application and later returns to this form

In order to use the SIPPREF control we must first request the operating system to register the SIPPREF window class. We do this by calling the SHInitExtraControls function. This step only needs to be done once, so is typically done during your application’s start up code. It is very easy to call, as the following example demonstrates:


#include
SHInitExtraControls
();


Once we have registered the SIPPREF window class, we simply create a SIPPREF control as a child of our dialog. When the SIPPREF control is created it will enumerate all sibling controls and subclass them in order to provide the default SIP handling behaviour. The SIPPREF control must be the last control added to your dialog, as any controls added after the SIPPREF control will not be present when the SIPPREF control enumerates its siblings, and hence will not be subclassed to provide the proper SIP handling.


If dynamically creating the SIPPREF control, a good place to do this is within the WM_CREATE or WM_INITDIALOG message handler, as the following code sample demonstrates:


case WM_INITDIALOG:
// Create a SIPPREF control to handle the SIP. This
// assumes 'hDlg' is the HWND of the dialog.
CreateWindow(WC_SIPPREF, L"", WS_CHILD,
0, 0, 0, 0, hDlg, NULL, NULL, NULL);


.NET CF Sample Code via P/invoke


Note: if a panel is used, hWndParent passed to CreateWindowEx should be the handle of the panel, otherwise there is no effect!


[DllImport("aygshell.dll")]
private static extern int SHInitExtraControls();

[DllImport("coredll.dll")]
private static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);

private static readonly string WC_SIPPREF = "SIPPREF";
private static readonly uint WS_CHILD = 0x40000000;


protected override void OnLoad()
{
// Initialise the extra controls library
SHInitExtraControls();

// Create our SIPPREF control which will enumerate all existing
// controls created by the InitializeControl() call.
IntPtr hWnd = CreateWindowEx(0, WC_SIPPREF, "", WS_CHILD,
0, 0, 0, 0, this.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
}


Reference: http://www.christec.co.nz/blog/archives/146

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.