如何在文本框顶部绘图

1

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

问题描述

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

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14