richardli79

导航

设置控件输入的输入方式

使用托管代码可以订制输入文本框的输入模式,这个对用户很方便,使用得当可以减少一次用户的操作。至少在手机上输入东西是越少越方便。
原理:向文本控件发送一个设置输入模式的消息。
使用到的API:SendMessage和GetFocus
public 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();
    }

posted on 2005-05-30 16:56  Richard  阅读(547)  评论(0编辑  收藏  举报