繁体   English   中英

WinForms Multiline TextBox:如何在TextBox的角落绘制小图像?

[英]WinForms Multiline TextBox: how to paint small image in the corner of TextBox?

我有一个WinForms应用程序,C#、. NET 4.0。

我在Form上有一个Multiline TextBox 除了普通的文本和TextBox本身,我想在TextBox的角落看到一个三角形:

在此处输入图片说明

为此,我通过以下方式重写TextBox WndProc方法:

        private const int WM_PAINT = 0x000f;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_PAINT:
                    base.WndProc(ref m);
                    paintInnerButton();
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        private void paintInnerButton()
        {
            Point innerButtonLocation = ClientRectangle.Location + (ClientSize - UITools.InnerButtonSize);
            var innerButtonRect = new Rectangle(innerButtonLocation, UITools.InnerButtonSize);
            drawTriangle(CreateGraphics(), BackColor, innerButtonRect.Location);
        }

        private static void drawTriangle(Graphics gr, Color backColor, Point location)
        {
            Color innerButtonColor = _activeInnerButtonColor;

            Point[] points = {
            new Point(location.X, location.Y + InnerButtonSize.Height), // LEFT BOTTOM
            new Point(location.X + InnerButtonSize.Width, location.Y), // RIGHT TOP
            new Point(location.X + InnerButtonSize.Width, location.Y + InnerButtonSize.Height) // RIGHT BOTTOM
        };

            using (var brush = new LinearGradientBrush(
                new Point(
                    location.X + (int)(InnerButtonSize.Width / 2.0),
                    location.Y + (int)(InnerButtonSize.Height / 2.0)),
                new Point(location.X - 1 + InnerButtonSize.Width, location.Y + InnerButtonSize.Height),
                backColor,
                innerButtonColor
                ))
            {
                gr.FillPolygon(brush, points);
            }
        }

问题来了,当文本较长时,我按下键盘上的“向下”按钮将其向下滚动。 出现一些较小的三角形:

在此处输入图片说明

有什么想法,为什么会发生以及如何克服?

首先,您应该放置图形

using (Graphics g = this.CreateGraphics())
{
    drawTriangle(g, BackColor, innerButtonRect.Location);
}

而且,尽管绘制文本框可能很棘手,但对于您的特定问题,我将在以下Windows消息WM_VSCROLL上将WndProc()修改为Refresh()您的控件(它将强制控件使其客户区无效并立即重绘自身) , WM_HSCROLLWM_MOUSEWHEEL 如下:

    private const int WM_PAINT = 0x000f;
    private const int WM_HSCROLL = 0x114;
    private const int WM_VSCROLL = 0x115;
    private const int WM_MOUSEWHEEL = 0x20A;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_PAINT:
                base.WndProc(ref m);
                paintInnerButton();
                break;
            case WM_VSCROLL:
            case WM_HSCROLL:
            case WM_MOUSEWHEEL:
                this.Refresh();
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM