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)
这篇关于如何在文本框顶部绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在文本框顶部绘图
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 如果条件可以为空 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 将数据集转换为列表 2022-01-01
