Winforms TextBox 可以设置背景图片吗?
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的。如果您尝试在构造函数中重写 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.
如果您愿意进行一些本机编码,您可以尝试使用此处提到的技术:
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.