在C#

时间:2018-06-04 12:56:40

标签: c# gdi+ flicker

我正在处理一组UI元素,而ComboBox让我很难过。

我的目标是在元素周围绘制1px边框,当鼠标悬停在其上时会改变颜色。

我使用WndProc()方法对WM_PAINT消息作出反应并绘制边框:

protected override void WndProc(ref Message m)
{
        if (m.Msg == (int)WindowsMessages.Win32Messages.WM_PAINT)
        {
            base.WndProc(ref m);
            if (hovered || Focused)
            {
                //Paint frame with hovered color when being hovered over
                PaintHelper.PaintFrame(this, _frameColorHovered, _frameWidth);
            }
            else
            {
                //Paint frame with standart color
                PaintHelper.PaintFrame(this, _frameColor, _frameWidth);
            }

        }
        .
        .
        .
}

Paint helper方法如下所示:

public static void PaintFrame(Control target, Color color, int frameWidth)
{
        IntPtr dc = GetWindowDC(target.Handle);
        using (Graphics g = Graphics.FromHdc(dc))
        {
            using (Pen p = new Pen(color, frameWidth))
                g.DrawRectangle(p, frameWidth / 2, frameWidth / 2, target.Width - frameWidth, target.Height - frameWidth);
        }
 }

到目前为止一直很好但是当鼠标向外移动或在Elements边界内部时,边框会一直闪烁!我做了一些研究,但每个人都使用UserPaint标志,这不是我的选择。

所以: 有没有办法消除闪烁而不用自己绘制整个控件?

0 个答案:

没有答案
相关问题