title: TextBox drawn in WM_PAINT flickers on mouse enter/leave
body: <p>I have a custom TextBox in which I draw some place holder text when it's empty.
  It works pretty well, but it flickers when the mouse enters and leaves the TextBox. It seems related to the border becoming blue when the mouse hovers the control (I'm on Windows 8.1).</p>
  
  <p>Any idea how I could fix this ?</p>
  
  <p>I've tried various SetStyles flags without success. </p>
  
  <pre><code>class MyTextBox : TextBox
  {
    public string PlaceHolder { get; set; }
  
    static readonly Brush sPlaceHolderBrush = new SolidBrush(Color.FromArgb(70, 70, 78));
    static readonly StringFormat sFormat = new StringFormat
    {
       Alignment = StringAlignment.Near,
       LineAlignment = StringAlignment.Center
    };
  
    private Font mPlaceHolderFont;
  
    [DllImport("user32")]
    private static extern IntPtr GetWindowDC(IntPtr hwnd);
  
    protected override void WndProc(ref Message m)
    {
       base.WndProc(ref m);
  
       if (m.Msg == 0x0F)   
       {
          if (string.IsNullOrEmpty(Text) &amp;&amp; !Focused)
          {
             IntPtr dc = GetWindowDC(Handle);
             using (Graphics g = Graphics.FromHdc(dc))
             {
                if (mPlaceHolderFont == null)
                   mPlaceHolderFont = new Font(Font, FontStyle.Italic);
  
                var rect = new RectangleF(2, 2, Width - 4, Height - 4);
                g.FillRectangle(Brushes.White, rect);
                g.DrawString(PlaceHolder, mPlaceHolderFont, sPlaceHolderBrush, rect, sFormat);
             }
          }
       }
    }
  }
  </code></pre>
  
  <hr>
  
  <p>I had other problems with overriding OnPaint. Here is the best solution I came up with : </p>
  
  <pre><code>class MyTextBox : TextBox
  {
    public string PlaceHolder { get; set; }
  
    static readonly Brush sPlaceHolderBrush = new SolidBrush(Color.FromArgb(70, 70, 78));
    static readonly StringFormat sFormat = new StringFormat
    {
       Alignment = StringAlignment.Near,
       LineAlignment = StringAlignment.Near
    };
  
    private Font mPlaceHolderFont;
    private Brush mForegroundBrush;
  
    protected override void OnHandleCreated(EventArgs e)
    {
       base.OnHandleCreated(e);
       SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
    }
  
    protected override void OnPaint(PaintEventArgs e)
    {
       var bounds = new Rectangle(-2, -2, Width, Height);
       var rect = new RectangleF(1, 0, Width - 2, Height - 2);
  
       e.Graphics.FillRectangle(Brushes.White, rect);
       if (string.IsNullOrEmpty(Text) &amp;&amp; !Focused)
       {
          if (mPlaceHolderFont == null)
             mPlaceHolderFont = new Font(Font, FontStyle.Italic);
  
          if (mForegroundBrush == null)
             mForegroundBrush = new SolidBrush(ForeColor);
  
          e.Graphics.DrawString(PlaceHolder, mPlaceHolderFont, sPlaceHolderBrush, rect, sFormat);
       }
       else
       {
          var flags = TextFormatFlags.Default | TextFormatFlags.TextBoxControl;
          if (!Multiline)
             flags |= TextFormatFlags.SingleLine | TextFormatFlags.NoPadding;
  
          TextBoxRenderer.DrawTextBox(e.Graphics, bounds, Text, Font, flags, TextBoxState.Selected);
       }
    }
  }
  </code></pre>