winforms WM_PAINT中绘制的文本框在鼠标进入/离开时 Flink

g2ieeal7  于 2023-10-23  发布在  Flink
关注(0)|答案(1)|浏览(177)

我有一个自定义的文本框,我画一些地方保持器文本时,它是空的。它工作得很好,但当鼠标进入和离开文本框时,它会 Flink 。这似乎与鼠标悬停控件时边框变蓝有关(我使用的是Windows 8.1)。
知道我该怎么解决吗?
我尝试了各种SetStyles标志,但没有成功。

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) && !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);
           }
        }
     }
  }
}

我在重写OnPaint时遇到了其他问题。以下是我提出的最佳解决方案:

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) && !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);
     }
  }
}
fcwjkofz

fcwjkofz1#

使用WM_PAINT而不是OnPaint有什么特殊原因吗?在WM_PAINT中,您可以从句柄获得绘图上下文,这始终是对控件的直接访问。在OnPaint中,在事件参数中已经有了一个Graphics,它可以是一个缓冲区或直接上下文,这取决于样式。
你提到你尝试了一些风格,但没有成功。首先,我会说尝试这些 * 和 * 移动你的油漆逻辑到OnPaint

SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

如果它不起作用(一个焦点控件在Windows中可能会有奇怪的行为),你必须坚持使用WM_PAINT,然后手动创建一个缓冲区。您的原始代码首先绘制一个白色矩形,然后绘制一些文本,这会导致 Flink 。您可以通过使用缓冲区来避免这种情况:

IntPtr dc = GetWindowDC(Handle);
using (Graphics g = Graphics.FromHdc(dc))
{
    // creating a buffered context
    using (BufferedGraphicsContext context = new BufferedGraphicsContext())
    {
        // creating a buffer for the original Graphics
        using (BufferedGraphics bg = context.Allocate(g, ClientRectangle))
        {
             if (mPlaceHolderFont == null)
                mPlaceHolderFont = new Font(Font, FontStyle.Italic);

             var gBuf = bg.Graphics;
             var rect = ClientRectangle;
             rect.Inflate(-1, -1);
             gBuf.FillRectangle(Brushes.White, rect);
             gBuf.DrawString(PlaceHolder, mPlaceHolderFont, sPlaceHolderBrush, rect, sFormat);

             // copying the buffer onto the original Graphics
             bg.Render(g);
        }
    }
}

ReleaseDC(Handle, dc); // also in user32

相关问题