.NET 图形文本输入

发布于 2024-08-19 05:04:13 字数 304 浏览 4 评论 0原文

假设我正在显示一个位图并希望允许将文本放置在该位图上。我可以在该位置显示 RichEdit 或 TextBox 控件,并允许以特定字体/大小等输入文本...但随后用户可以“看到”矩形文本输入控件在输入/编辑时出现和消失,并且编辑取消。输入/编辑文本时它还会覆盖位图。我的问题是,如何做到这一点以使文本输入控制窗口“透明”或不“可见”。

同样,如果位图上有文本,用户单击文本,并且插入符号会出现在文本中,不显示可见的矩形控件,如文本框、richedit 等...,文本只是“神奇地”变得可编辑。

有没有办法在.NET 中执行“透明背景”文本输入?

Suppose I am displaying a bitmap and want to allow text to be placed on that bitmap. I can display a RichEdit or TextBox control at the location and allow the text to be typed in at a certain font/size etc... but then the user can "see" the rectangular text entry control appear and disappear upon entry/edit and the edit cancel. It also covers the bitmap while entering/editing the text. My question is, how is this done such that the text entry control window is "transparent" or not "visible".

Again, if there is text on the bitmap, the user clicks on the text and viola the caret appears in the text, without displaying a visible rectangular control like textbox, richedit etc..., the text just "magically" becomes editable.

Is there a way to do this "transparent background" text entry in .NET?

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

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

发布评论

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

评论(3

美人迟暮 2024-08-26 05:04:13

我想到了几个选项:

  • 显示文本输入框可能不是一件坏事。这使用户清楚地知道,单击位图后,他们已进入新的“模式”并且现在可以输入文本。例如,这与在树视图中编辑项目名称或在 Windows 文件资源管理器中编辑文件名是一致的。

  • TextBox 支持 BorderStyle.None,但不允许透明背景(大概是因为它必须在编辑操作期间保留背景)。很容易尝试一下,看看它是否足以满足您的需求。

  • 将您想要的行为精确地写入您的显示中将非常容易 - 即完全省去第 3 方控件。您只需渲染文本,并在编辑过程中添加插入符号显示(即在插入位置绘制一条简单的线),并处理按键以在插入符号位置添加或删除文本。您只需将文本和背景的渲染分开,以便在文本更改时重新渲染它。

A few options come to mind:

  • It may not be a bad thing to show a text entry box. This makes it clear to the user that having clicked on the bitmap they have entered a new "mode" and are now able to enter text. This is consistent with editing item names in a treeview, or editing a filename in a Windows file explorer, for example.

  • TextBox supports BorderStyle.None, but doesn't allow a transparent background (presumably because it would have to preserve the background during editing operations). It's easy to give this a try to see if it works well enough for your needs.

  • It would be very easy to write precisely the behaviour you want into your display - i.e. dispense with 3rd party controls entirely. You just have to render the text, and during editing add in the caret display (i.e. draw a simple line at the insertion position), and handle keypresses to add or remove text at the caret position. You just have to separate the rendering of the text and the background so that you can re-render it as the text is changed.

辞取 2024-08-26 05:04:13

执行此操作的简单方法是使用 WPF 的 TextBox 控件。您可以轻松地将背景设置为透明:

<TextBox Text="{Binding MyProperty}" Background="Transparent" />

要使除内容之外的整个文本框透明,只需使用 ControlTemplate:

<TextBox Text="{Binding MyProperty}">
  <TextBox.Template>

    <ControlTemplate>
      <ScrollViewer Name="PART_ContentHost" />
    </ControlTemplate>

  </TextBox.Template>
</TextBox>

如果您仍在使用 WinForms,则要困难得多。您有三个选择:

  • 听起来您正在做的应用程序类型已经是 WPF 的绝佳候选者,所以如果您不太热衷的话,我肯定会切换到 WPF。除了透明文本框之外,WPF 与 WinForms 相比还有许多其他优势,可以显着提高开发人员的工作效率,例如高级数据绑定和模板。

  • 您无法使用 WinForms,但可以在 WPF 中渲染 TextBox 后面的区域,请使用 WPF ElementHost 来使用 WPF 显示 UI 的整个部分,并将其余代码保留为 WinForms。

  • 否则编写您自己的 TextBox 替换控件可能是您能做的最好的事情。

The easy way to do this is to use WPF's TextBox control. You can easily set the background to be transparent:

<TextBox Text="{Binding MyProperty}" Background="Transparent" />

To make the entire textbox transparent except for its content, just use a ControlTemplate:

<TextBox Text="{Binding MyProperty}">
  <TextBox.Template>

    <ControlTemplate>
      <ScrollViewer Name="PART_ContentHost" />
    </ControlTemplate>

  </TextBox.Template>
</TextBox>

If you are still using WinForms it is much more difficult. You have three options:

  • It sounds like the type of application you are doing is already a great candidate to WPF so I would definitely switch to WPF if you aren't too far into it. Besides transparent textboxes, WPF has many other advantages over WinForms that can dramatically increase your developer productivity, such as advanced data binding and templating.

  • If you're stuck with WinForms but can render the area behind the TextBox in WPF, use a WPF ElementHost to display that whole section of the UI using WPF and leave the rest of your code as WinForms.

  • Otherwise writing your own TextBox replacement control is probably the best you can do.

多情癖 2024-08-26 05:04:13

一种方法是通过继承 TextBox 创建一个新的用户控件,然后重写 OnPaint 方法?我将使用 .NET Reflector 来查看原始方法中发生的情况,并尝试找出如何在没有背景/框架的情况下渲染文本。

One way would be to create a new user control by inheriting from TextBox and then overriding the OnPaint method? I would use .NET Reflector to see what's going on in the original method and try to figure out how to render the text without the background/frame.

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