【问题标题】:WinForms Multiline TextBox: how to paint small image in the corner of TextBox?WinForms多行文本框:如何在文本框的角落绘制小图像?
【发布时间】:2015-01-15 12:10:46
【问题描述】:

我有一个 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);
            }
        }

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

任何想法,为什么会发生这种情况以及如何克服它?

【问题讨论】:

  • 你不能得到这个可靠的。 TextBox 犯下的罪行可以追溯到 1980 年代,当时 Windows 必须在 386sux 机器上运行。它不使用 WM_PAINT 进行绘制。这会挫败任何在 ownerdraw 上的尝试。可以使覆盖控件的透明顶层窗口之类的东西起作用。

标签: c# winforms textbox ownerdrawn


【解决方案1】:

首先,你应该处理你的图形

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

虽然,绘制一个文本框可能很棘手,但对于您的具体问题,我会将您的 WndProc() 修改为 Refresh() 您的控件(它将强制控件使其客户区无效并立即重绘自身)如下Windows 消息 WM_VSCROLLWM_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;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2011-12-03
    • 2011-06-25
    相关资源
    最近更新 更多