WinForm 은 TextBox 에 워 터 마크 문자 기능 을 설정 합 니 다.

본 고 는 WinForm 이 TextBox 에 워 터 마크 문자 기능 을 설정 하고 매우 실 용적 인 기 교 를 보 여 주 며 여러분 께 참고 하 시기 바 랍 니 다.
키 코드 는 다음 과 같 습 니 다:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinFormUtilHelpV2
{
  /// <summary>
  ///   .NET 2.0 TextBox   
  /// </summary>
  public static class TextBoxToolV2
  {
    private const int EM_SETCUEBANNER = 0x1501;
    [DllImport("user32.dll", CharSet = CharSet.Auto)]

    private static extern Int32 SendMessage
     (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

    /// <summary>
    ///  TextBox      
    /// </summary>
    /// <param name="textBox">TextBox</param>
    /// <param name="watermark">    </param>
    public static void SetWatermark(this TextBox textBox, string watermark)
    {
      SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark);
    }
    /// <summary>
    ///       
    /// </summary>
    /// <param name="textBox">TextBox</param>
    public static void ClearWatermark(this TextBox textBox)
    {
      SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty);
    }
  }
}

테스트 코드 는 다음 과 같 습 니 다:

using System;
using System.Windows.Forms;
using WinFormUtilHelpV2;

namespace WinFormUtilHelpV2Test
{
  public partial class WinTextBoxToolV2Test : Form
  {
    public WinTextBoxToolV2Test()
    {
      InitializeComponent();
    }

    private void WinTextBoxToolV2Test_Load(object sender, EventArgs e)
    {
      textBox1.SetWatermark("       ....");
      textBox2.SetWatermark("       ....");
    }

    private void button1_Click(object sender, EventArgs e)
    {
      textBox1.ClearWatermark();
      textBox2.ClearWatermark();
    }
  }
}

테스트 효 과 는 다음 그림 과 같다.

본 고 에서 말 한 TextBox 에 워 터 마크 문자 기능 예 시 를 설정 하 는 것 이 여러분 의 C\#프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다!

좋은 웹페이지 즐겨찾기