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

我在Multiline上有一个TextBox Form
除了普通的文字和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消息中将WndProc()修改为Refresh()您的控件(它将强制控件使其客户区无效并立即重绘自身)。 cc>,WM_VSCROLLWM_HSCROLL。如下:

    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;
        }
    }

关于c# - WinForms Multiline TextBox:如何在TextBox的角落绘制小图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27963142/

10-17 02:32