設定控制項輸入的輸入方式
來源:互聯網
上載者:User
使用Managed 程式碼可以訂製輸入文字框的輸入模式,這個對使用者很方便,使用得當可以減少一次使用者的操作。至少在手機上輸入東西是越少越方便。
原理:向文本控制項發送一個設定輸入模式的訊息。
使用到的API:SendMessage和GetFocuspublic class EditModeHandler
{
public static int SetT9Mode()
{
try
{
int hWnd = GetFocus();
return SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_AMBIG);
}
catch (Exception)
{
MessageBox.Show("不能設定為T9輸入模式!");
return -1;
}
}
public static int SetNumbersMode()
{
try
{
int hWnd = GetFocus();
return SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS);
}
catch (Exception)
{
MessageBox.Show("不能設定為數字輸入模式!");
return -1;
}
}
public static int SetTextMode()
{
try
{
int hWnd = GetFocus();
return SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_TEXT);
}
catch (Exception)
{
MessageBox.Show("不能設定為字元輸入模式!");
return -1;
}
}
// API declarations
private const uint EM_SETINPUTMODE = 0x00DE;
private const uint EIM_SPELL = 0;
private const uint EIM_AMBIG = 1; // T9
private const uint EIM_NUMBERS = 2;
private const uint EIM_TEXT = 3;
[DllImport("coredll.dll")]
private static extern int SendMessage(int hWnd, uint wMsg, uint wParam, uint lParam);
[DllImport("coredll.dll")]
private static extern int GetFocus();
}