효과 :
설명 :
마찬가지로 자리 표시 자 속성의 HTML 태그 입력 태그, 제어 정보 _txtPlaceHolder 설명 프롬프트 필드, 메시지가 컨트롤을 그립니다 창에서 전송되는 경우, 메시지 처리 기능의 WndProc를 재 작성이, 텍스트 상자를 상속, 여기서주의 할 그리기 시작했다 this.Invalidate (에 TxtPlaceHolder 설정 방법)이며, 이것은 상기 제어 편차를보고 할 것이다 형태에 이러한 제어를 끌지 코드 경우 추첨 묘화되며, 컨트롤 그려 않으면 실패이다.
예외 :
이유 :
내 실험 후, 나는 한 _txtPlaceHolder로이 필드에 할당 된 null 값이 초기 후에 제거되지 않은 것을 발견 "this.Invalidate ();"문구, 프로그램은 실행할 수 있습니다. 그 이유는
_txtPlaceHolder.Length> 0
코드 :
public sealed class MyCustomTextBox:TextBox { private const int WM_PAINT = 0x000F; private string _txtPlaceHolder=""; [Category("自定义属性"), Description("文本框里的提示文字"), DefaultValue("请在此输入"), Browsable(true)] public string TxtPlaceHolder { get { return _txtPlaceHolder; } set { if (value == null) throw new ArgumentNullException("value"); _txtPlaceHolder = value; this.Invalidate(); } } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT && !this.Focused && (this.TextLength == 0) && (_txtPlaceHolder.Length > 0)) { TextFormatFlags tff = (TextFormatFlags.EndEllipsis | TextFormatFlags.NoPrefix | TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.NoPadding); using (Graphics g = this.CreateGraphics()) { Rectangle rect = this.ClientRectangle; rect.Offset(4, 1); TextRenderer.DrawText(g, _txtPlaceHolder, this.Font, rect, SystemColors.GrayText, tff); } } } }