Tạo mặt nạ cho các control nhập liệu từ người dùng |
Mã nguồn chương trình: Sử dụng ngôn ngữ lập trình C#
Source: http://www.aaronlerch.com/blog/2007/12/01/watermarked-edit-controls/
Các bạn có thể download trực tiếp các file cài đặt *.cs tại đây: CueTextBox.cs, CueComboBox.cs, và CueToolStripTextBox.cs
CueTextbox.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Lerch.Samples
{
public class CueTextBox : TextBox
{
#region PInvoke Helpers
private static uint ECM_FIRST = 0x1500;
private static uint EM_SETCUEBANNER = ECM_FIRST + 1;
[DllImport("user32.dll",
CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr
SendMessage(HandleRef hWnd, uint Msg, IntPtr
wParam, String lParam);
#endregion PInvoke Helpers
#region CueText
private string
_cueText = String.Empty;
///
/// Gets or sets the text
the
///
[Description("The
text value to be displayed as a cue to the user.")]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string CueText
{
get { return
_cueText; }
set
{
if (value
== null)
{
value
= String.Empty;
}
if (!_cueText.Equals(value, StringComparison.CurrentCulture))
{
_cueText = value;
UpdateCue();
OnCueTextChanged(EventArgs.Empty);
}
}
}
///
/// Occurs when the property value changes.
///
public event EventHandler CueTextChanged;
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnCueTextChanged(EventArgs
e)
{
EventHandler handler =
CueTextChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion CueText
#region ShowCueTextOnFocus
private bool
_showCueTextWithFocus = false;
///
/// Gets or sets a value
indicating whether the will display the
/// even when the control
has focus.
///
[Description("Indicates
whether the CueText will be displayed even when the control has focus.")]
[Category("Appearance")]
[DefaultValue(false)]
[Localizable(true)]
public bool
ShowCueTextWithFocus
{
get { return
_showCueTextWithFocus; }
set
{
if (_showCueTextWithFocus != value)
{
_showCueTextWithFocus = value;
UpdateCue();
OnShowCueTextWithFocusChanged(EventArgs.Empty);
}
}
}
///
/// Occurs when the
///
public event EventHandler ShowCueTextWithFocusChanged;
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnShowCueTextWithFocusChanged(EventArgs e)
{
EventHandler handler =
ShowCueTextWithFocusChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion ShowCueTextOnFocus
#region Overrides
protected override void OnHandleCreated(EventArgs
e)
{
UpdateCue();
base.OnHandleCreated(e);
}
#endregion Overrides
private void
UpdateCue()
{
// If the handle isn't yet created,
// this will be called when it is created
if (this.IsHandleCreated)
{
SendMessage(new HandleRef(this, this.Handle),
EM_SETCUEBANNER, (_showCueTextWithFocus) ? new IntPtr(1) : IntPtr.Zero,
_cueText);
}
}
}
}
CueComboBox.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Lerch.Samples
{
public class CueComboBox : ComboBox
{
#region PInvoke Helpers
private static uint CB_SETCUEBANNER = 0x1703;
[DllImport("user32.dll",
CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr
SendMessage(HandleRef hWnd, uint Msg, IntPtr
wParam, String lParam);
#endregion PInvoke Helpers
#region CueText
private string
_cueText = String.Empty;
///
/// Gets or sets the text
the will display as a cue to the user.
///
[Description("The
text value to be displayed as a cue to the user.")]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string CueText
{
get { return
_cueText; }
set
{
if (value
== null)
{
value
= String.Empty;
}
if (!_cueText.Equals(value, StringComparison.CurrentCulture))
{
_cueText = value;
UpdateCue();
OnCueTextChanged(EventArgs.Empty);
}
}
}
///
/// Occurs when the property value changes.
///
public event EventHandler CueTextChanged;
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnCueTextChanged(EventArgs
e)
{
EventHandler handler =
CueTextChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion CueText
#region Overrides
protected override void OnHandleCreated(EventArgs
e)
{
UpdateCue();
base.OnHandleCreated(e);
}
#endregion Overrides
private void
UpdateCue()
{
// If the handle isn't yet created,
// this will be called when it is created
if (this.IsHandleCreated)
{
SendMessage(new HandleRef(this, this.Handle),
CB_SETCUEBANNER, IntPtr.Zero, _cueText);
}
}
}
}
CueToolStripTextBox.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Lerch.Samples
{
public class CueToolStripTextBox : ToolStripTextBox
{
public CueToolStripTextBox()
: base()
{
if (this.Control
!= null)
{
this.Control.HandleCreated += new EventHandler(OnControlHandleCreated);
}
}
public CueToolStripTextBox(string
name)
: base(name)
{
if (this.Control
!= null)
{
this.Control.HandleCreated += new EventHandler(OnControlHandleCreated);
}
}
protected override void Dispose(bool
disposing)
{
if (disposing)
{
if (this.Control
!= null)
{
this.Control.HandleCreated
-= new EventHandler(OnControlHandleCreated);
}
}
base.Dispose(disposing);
}
void
OnControlHandleCreated(object sender, EventArgs e)
{
UpdateCue();
}
#region PInvoke Helpers
private static uint ECM_FIRST = 0x1500;
private static uint EM_SETCUEBANNER = ECM_FIRST + 1;
[DllImport("user32.dll",
CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr
SendMessage(HandleRef hWnd, uint Msg, IntPtr
wParam, String lParam);
#endregion PInvoke Helpers
#region CueText
private string
_cueText = String.Empty;
///
/// Gets or sets the text
the will display as a cue to the user.
///
[Description("The
text value to be displayed as a cue to the user.")]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string CueText
{
get { return
_cueText; }
set
{
if (value
== null)
{
value
= String.Empty;
}
if (!_cueText.Equals(value, StringComparison.CurrentCulture))
{
_cueText = value;
UpdateCue();
OnCueTextChanged(EventArgs.Empty);
}
}
}
///
/// Occurs when the
///
public event EventHandler CueTextChanged;
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnCueTextChanged(EventArgs
e)
{
EventHandler handler =
CueTextChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion CueText
#region ShowCueTextOnFocus
private bool
_showCueTextWithFocus = false;
///
/// Gets or sets a value
indicating whether the will display the
/// even when the control
has focus.
///
[Description("Indicates
whether the CueText will be displayed even when the control has focus.")]
[Category("Appearance")]
[DefaultValue(false)]
[Localizable(true)]
public bool
ShowCueTextWithFocus
{
get { return
_showCueTextWithFocus; }
set
{
if (_showCueTextWithFocus != value)
{
_showCueTextWithFocus = value;
UpdateCue();
OnShowCueTextWithFocusChanged(EventArgs.Empty);
}
}
}
///
/// Occurs when the property value changes.
///
public event EventHandler ShowCueTextWithFocusChanged;
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnShowCueTextWithFocusChanged(EventArgs e)
{
EventHandler handler =
ShowCueTextWithFocusChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion ShowCueTextOnFocus
private void
UpdateCue()
{
// If the handle isn't yet created,
// this will be called when it is created
if ((this.Control
!= null) && (this.Control.IsHandleCreated))
{
SendMessage(new HandleRef(this.Control, this.Control.Handle),
EM_SETCUEBANNER, (_showCueTextWithFocus) ? new IntPtr(1) : IntPtr.Zero,
_cueText);
}
}
}
}
2 comments:
Cam on ban rat nhieu :)
Cũng được, nhưng bên thư viện winform (thuvienwinform.blogspot.com) họ giới thiệu cái textBox đơn giản hơn nhiều:
http://thuvienwinform.blogspot.com/2013/10/Hint-text-goi-chi-goi-y-textbox-winform.html
Đăng nhận xét