Winforms TextBox 可以设置背景图片吗?

发布于 2024-10-06 14:15:39 字数 127 浏览 0 评论 0原文

是否可以在 C# 中更改 Windows 窗体 TextBox 的背景图像?没有 BackgroundImage 属性。我应该以某种方式重写 Paint 方法吗?

Is it possible to change the background image of a Windows Forms TextBox in C#? There is no BackgroundImage property. Should I override the Paint method somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦冥 2024-10-13 14:15:39

这是不可能的。如果您尝试在构造函数中重写 TextBox 并调用 SetStyle(ControlStyles.UserPaint, true) 以便重写 OnPaintBackground 并绘制图像,您将会遇到一些粗鲁的意外。回退到传统渲染模式只是其中之一。

TextBox 可以追溯到 Windows 的早期阶段,当时它仍然必须在 386SUX 硬件上运行。它在如此有限的硬件上合理工作的一个特殊罪行是在不使用 WM_PAINT 事件的情况下绘制自己。这会破坏背景图像。

CodeProject.com 上有一个项目提供了这样的功能。我不能推荐它。

It isn't possible. If you try by overriding TextBox and calling SetStyle(ControlStyles.UserPaint, true) in the constructor so you can override OnPaintBackground and draw the image, you'll be in for several rude surprises. Falling back to legacy rendering mode is just one of them.

TextBox dates from the very early days of Windows, back when it still had to run on 386SUX hardware. One particular crime it commits to work reasonably on such limited hardware was to draw itself without using the WM_PAINT event. This destroys the background image.

There's a project at CodeProject.com that provides one. I cannot recommend it.

紫﹏色ふ单纯 2024-10-13 14:15:39

如果您愿意进行一些本机编码,您可以尝试使用此处提到的技术:http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-文本框
(编辑:原始链接已关闭,thnx Zyo,将其替换为回程机器中存档副本的链接。)

尽管本文是关于在绘制文本框之后(而不是之前)在文本框中绘制某些内容。 ,也许可以对其进行修改以完成您所需要的。

If you're up to some native coding, you could try to use the technique mentioned here: http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox
(Edit: original link is down, thnx Zyo, replaced it with link to archived copy in the wayback machine.)

Although the article is about drawing something in a textbox after the textbox is drawn (not before), perhaps it could be modified to accomplish what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文