一直在开发wince上的应用程序,.net cf中,c#的文本框 textbox的selectall方法在getfocus事件中,并没有生效,在网上查了很长时间之后,大体明白了一点,在此做个说明:
在电脑上,用鼠标点击文本框后, 执行getfocus事件,执行完这个事件之后,再执行mouseup事件,如果想调用selectall方法,需要在mouseup事件里调用。
在c#的textbox中,单击文本框后,执行getfocus事件,执行完这个事件之后,再执行click事件,如果想调用selecall方法,需要在click事件里调用。
问题应该明了了,在.net cf 里,我们只需要在click事件里,调用selectall方法就可以了。textbox本身不支持click事件,需要我们填加。
原来就对textbox做了一点封装,填了一个属性和回车事件,可以设置限制输入的内容,因为也重写了selecall方法,在getfocus事件里,再调用这个方法就可以了。
增加单击事件的方法,是复制的codeproject上的大师的代码,具体网址忘了。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using LZLControls.引用类; using ZLDrivers; namespace LZLControls { public partial class ZLTextBox : TextBox, ISupportInitialize { private bool m_bSelectALL = false; public ZLTextBox() { InitializeComponent(); #region 鼠标事件 if (Environment.OSVersion.Platform == PlatformID.WinCE) { WndProcHooker.HookWndProc(this, new WndProcHooker.WndProcCallback(this.WM_LButtonDown_Handler), Win32.WM_LBUTTONDOWN); WndProcHooker.HookWndProc(this, new WndProcHooker.WndProcCallback(this.WM_LButtonUp_Handler), Win32.WM_LBUTTONUP); } #endregion } #region 设计组件 ISupportInitialize Members void ISupportInitialize.BeginInit() { } void ISupportInitialize.EndInit() { } #endregion /// <summary> /// 判断按下的键值,限制只能输入字母,数字 /// </summary> private bool bLimitInput = false; protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); bLimitInput = true; if (this.numInputType == KeyBoardType.全部) { bLimitInput = false; return; } else if (this.numInputType == KeyBoardType.数字) { if (e.Modifiers == Keys.None) { if (e.KeyValue >= 48 && e.KeyValue <= 57) { // 常规数字 bLimitInput = false; return; } else if (e.KeyValue >= 96 && e.KeyValue <= 105) { // 小键盘 bLimitInput = false; return; } } #region 减号、小数点也做了过滤 减号 //else if (bInputNumberMinus) //{ // if (e.KeyValue == 189 || e.KeyValue == 109) // { // bNoAllowInput = true; // return; // } //} else if ( e.KeyValue==190 || e.KeyValue==110 ) { // 小数点 bNoAllowInput = true; return; } #endregion } else if (this.numInputType == KeyBoardType.字母) { if (e.KeyValue >= 65 && e.KeyValue <= 90) { bLimitInput = false; return; } } // ESC键、回车键,退格键,左右方向键、删除键 if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Back || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Delete) { bLimitInput = false; return; } } protected override void OnKeyPress(KeyPressEventArgs e) { if (e.KeyChar == '\r') { if (keyEnter != null) { e.Handled = true; keyEnter(this, new EventArgs()); return ; } } Check for the flag being set in the KeyDown event. if (bLimitInput) { // Stop the character from being entered into the control since it is non-numerical. e.Handled = true; } base.OnKeyPress(e); } #region 回车键事件 /// <summary> /// 回车键事件 /// </summary> /// <param name="strText"></param> public delegate void OnKeyEnter(object sender, EventArgs e); /// <summary> /// 回车键 /// </summary> public event OnKeyEnter keyEnter = null; // 回车键事件 public void OnEnter() { if (keyEnter != null) { keyEnter(this, new EventArgs()); } } #endregion #region "按键时限制输入内容的功能" public enum KeyBoardType { 全部 = 0, 字母, 数字 } KeyBoardType numInputType = KeyBoardType.全部; /// <summary> /// 允许输入的类型 /// </summary> public KeyBoardType InputKey { get { return numInputType; } set { numInputType = value; } } #endregion #region 直接赋值时检测回车键字符 /// <summary> /// 检测赋值时有回车键字符 /// </summary> public override string Text { get { return base.Text; } set { base.Text = value; if (value.Length > 0) { if (value.Substring(value.Length - 1, 1) == "\r") { base.Text = value.Substring(0, value.Length - 1); if (keyEnter != null) { keyEnter(this, new EventArgs()); } } } } } #endregion #region 鼠标事件 public delegate void onClick(object sender, EventArgs e); public new event onClick Click; /// <summary> /// The method that gets called when a WM_NOTIFY message is received by the /// TextBox's parent. /// </summary> /// <param name="hwnd">The handle of the window that received the message</param> /// <param name="msg">The message received</param> /// <param name="wParam">The wParam arguments for the message</param> /// <param name="lParam">The lParam arguments for the message</param> /// <param name="handled">Set to true to indicate that this message was handled</param> /// <returns>An appropriate returen code for the message handled (see MSDN)</returns> int WM_Notify_Handler( IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled) { Win32.NMHDR nmHdr = new Win32.NMHDR(); System.Runtime.InteropServices.Marshal.PtrToStructure((IntPtr)lParam, nmHdr); switch (nmHdr.code) { case Win32.WM_LBUTTONDOWN: case Win32.WM_LBUTTONUP: // get the cursor coordinates on the client Point msgPos = Win32.LParamToPoint((int)Win32.GetMessagePos()); msgPos = this.PointToClient(msgPos); RaiseMouseClickEvent(MouseButtons.Left, msgPos); break; default: break; } return 0; } public void OnMouseClick(MouseEventArgs e) { this.OnClick(new EventArgs()); } protected override void OnClick(EventArgs e) { // 使全选功能生效 if (m_bSelectALL) { base.SelectAll(); m_bSelectALL = false; } base.OnClick(e); } #region 此函数暂时不需要,对控件隐藏 /// <summary> /// Raises the MouseClick event with the specified handle. /// </summary> /// <param name="hNode">The handle of the node for which the event is raised</param> /// <param name="button">The [mouse] buttons that were pressed to raise the event</param> /// <param name="coords">The [client] cursor coordinates at the time of the event</param> private void RaiseMouseClickEvent(MouseButtons button, Point coords) { MouseEventArgs e = new MouseEventArgs(button, 1, coords.X, coords.Y, 0); OnMouseClick(e); } #endregion // The callback called when the window receives a WM_LBUTTONDOWN // message. We capture the mouse and draw the button in the "pushed" // state. // hwnd - The handle to the window that received the // message. // wParam - Indicates whether various virtual keys are // down. // lParam - The coordinates of the cursor. // handled - Set to true if we don't want to pass this // message on to the original window procedure. // Returns zero if we process this message. int WM_LButtonDown_Handler( IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled) { // Start capturing the mouse input. this.Capture = true; // someone clicked on us so grab the focus if (!this.Focused) { this.Focus(); // We have handled this windows message and we don't want the // sub-classed window to do anything else. handled = true; } return 0; } // The callback called when the window receives a WM_LBUTTONUP // message. We release capture on the mouse, draw the button in the // "un-pushed" state and fire the OnMouseUp event if the cursor was // let go of inside our client area. // hwnd - The handle to the window that received the // message // wParam - Indicates whether various virtual keys are // down. // lParam - The coordinates of the cursor // handled - Set to true if we don't want to pass this // message // on to the original window procedure // Returns zero if we process this message. int WM_LButtonUp_Handler( IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled) { this.Capture = false; // TODO : implement your login on mouse key up event Click(this, null); handled = true; return 0; } #endregion private void ZLTextBox_GotFocus(object sender, EventArgs e) { m_bSelectALL = false; } public new void SelectAll() { m_bSelectALL = true; base.SelectAll(); } } }api声明 /// <summary>
/// A callback to a Win32 window procedure (wndproc)
/// </summary>
/// <param name="hwnd">The handle of the window receiving a message</param>
/// <param name="msg">The message</param>
/// <param name="wParam">The message's parameters (part 1)</param>
/// <param name="lParam">The message's parameters (part 2)</param>
/// <returns>A integer as described for the given message in MSDN</returns>
public delegate int WndProc(IntPtr hwnd, uint msg, uint wParam, int lParam);
public const uint WM_NOTIFY = 0x4E;
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
//public enum WM
//{
// LBUTTONDOWN = 0x0201,
// LBUTTONUP = 0x0202
//}
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
public class NMHDR
{
public IntPtr hwndFrom;
public uint idFrom;
public uint code;
}
/// <summary>
/// Helper function to convert a Windows lParam into a Point
/// </summary>
/// <param name="lParam">The parameter to convert</param>
/// <returns>A Point where X is the low 16 bits and Y is the
/// high 16 bits of the value passed in</returns>
public static Point LParamToPoint(int lParam)
{
uint ulParam = (uint)lParam;
return new Point(
(int)(ulParam & 0x0000ffff),
(int)((ulParam & 0xffff0000) >> 16));
}
#if DESKTOP
[DllImport("user32.dll")]
#else
[DllImport("coredll.dll")]
#endif
public extern static int DefWindowProc(IntPtr hwnd, uint msg, uint wParam, int lParam);
#if DESKTOP
[DllImport("user32.dll")]
#else
[DllImport("coredll.dll")]
#endif
public extern static IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong);
public const int GWL_WNDPROC = -4;
#if DESKTOP
[DllImport("user32.dll")]
#else
[DllImport("coredll.dll")]
#endif
public extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, uint msg, uint wParam, int lParam);
#if DESKTOP
[DllImport("user32.dll")]
#else
[DllImport("coredll.dll")]
#endif
public extern static uint GetMessagePos();
[DllImport("user32.dll", EntryPoint="SendMessage")]
public static extern uint SendMessageXP(IntPtr hwnd, uint msg, uint wparam, uint lparam);
[DllImport("coredll.dll", EntryPoint="SendMessage")]
public static extern uint SendMessageCE(IntPtr hwnd, uint msg, uint wparam, uint lparam);
public static uint SendMessage(IntPtr hwnd, uint msg, uint wparam, uint lparam)
{
if ( Environment.OSVersion.Platform==PlatformID.WinCE )
{
return SendMessageCE(hwnd, msg, wparam, lparam);
}
else
{
return SendMessageXP(hwnd, msg, wparam, lparam);
}
}
}