如何在文本框顶部绘图 [英] How to draw on top of a TextBox

查看:30
本文介绍了如何在文本框顶部绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 TextBox 的 winform,我想在它上面绘制一些 GDI 图形.文本框没有要挂钩的 Paint() 事件,所以我认为这一切都必须发生在表单的 Paint 事件中.作为测试,我正在从原点绘制一个矩形到文本框中的某个位置:

I have winform with a TextBox and I want to draw some GDI graphics on top of it. The text box does not have a Paint() event to hook to, so I assume it all has to happeen within the form's Paint event. As a test I am drawing a rectangle from the origin to a location within the text box with:

    private void FindForm_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Blue))
        {
            e.Graphics.DrawRectangle(pen, 0, 0, point.X, point.Y);
        }
    }

问题是当我运行时,首先绘制完成,然后在我的行之上渲染文本框.我想在文本框渲染后画线.

The problem is that when I run, the drawing is done first, and then the text box is rendered, on top of my lines. I want to draw the lines after the text box is rendered.

PS.我没有使用 Form.SetStyle() 函数对表单进行任何设置.

PS. I have not made any settings for the form with the Form.SetStyle() function.

推荐答案

所有者绘制 TextBox 控件可能相对棘手,因为它只是原生 Win32 TextBox<的包装器/code> 控制.

It can be relatively tricky to owner-draw the TextBox control because it's simply a wrapper around the native Win32 TextBox control.

捕获WM_PAINT 消息的最简单方法是从NativeWindow.

The easiest way to capture the WM_PAINT messages that you need to handle for your custom painting routines to work is by inheriting from the NativeWindow class.

这是一篇出色的分步文章,展示了如何在显示红色波浪下划线的情况下执行此操作:Owner-drawing a Windows.Forms TextBox (原链接失效; 替换为存档版本)

Here is an excellent step-by-step article that shows how to do this in the context of displaying the red wavy underlines: Owner-drawing a Windows.Forms TextBox (original link dead; replaced by archived version)

这篇关于如何在文本框顶部绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆