How to draw on top of a TextBox(如何在文本框顶部绘图)
问题描述
我有一个带有 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)
这篇关于如何在文本框顶部绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在文本框顶部绘图


- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01