繁体   English   中英

EM_SETCUEBANNER 不适用于 RichTextBox

[英]EM_SETCUEBANNER doesn't work on RichTextBox

我一直在使用EM_SETCUEBANNER在我的TextBox上实现一个占位符,它工作正常,直到我在RichTextBox上使用它。 它不显示任何文本。

这是我的代码:

 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError=true)]
 public static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam);

    bool SetPlaceHolder(TextBoxBase control, string text)
            {
               const int EM_SETCUEBANNER = 0x1501;
                return Natives.SendMessage(control.Handle, EM_SETCUEBANNER, 0, text) == 1;
            }

在 RTB 上使用它返回falseMarshal.GetLastWin32Error()值为0

我在Edit Control Messages上找不到任何特定于 RTB 的Edit Control Messages

我怎样才能解决这个问题?

您可以尝试自己实现:

public class RichTextWithBanner : RichTextBox {
  private const int WM_PAINT = 0xF;

  protected override void OnTextChanged(EventArgs e) {
    base.OnTextChanged(e);
    this.Invalidate();
  }

  protected override void WndProc(ref Message m) {
    base.WndProc(ref m);
    if (m.Msg == WM_PAINT && this.Text == string.Empty) {
      using (Graphics g = Graphics.FromHwnd(m.HWnd)) {
        TextRenderer.DrawText(g, "Type Something", this.Font,
            this.ClientRectangle, Color.DarkGray, Color.Empty,
            TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
      }
    }
  }
}

这不应该让您感到惊讶,除了文档之外,多行 TextBox 也不支持提示。

没有什么是你解决不了的,做起来并不难。 向您的项目添加一个新类并粘贴如下所示的代码。 编译。 将新控件从工具箱顶部拖放到窗体上,替换现有控件。

using System;
using System.Drawing;
using System.Windows.Forms;

class RichTextBoxEx : RichTextBox {
    public string Cue {
        get { return cue; }
        set {
            showCue(false);
            cue = value;
            if (this.Focused) showCue(true);
        }
    }
    private string cue;

    protected override void OnEnter(EventArgs e) {
        showCue(false);
        base.OnEnter(e);
    }

    protected override void OnLeave(EventArgs e) {
        showCue(true);
        base.OnLeave(e);
    }

    protected override void OnVisibleChanged(EventArgs e) {
        if (!this.Focused) showCue(true);
        base.OnVisibleChanged(e);
    }

    private void showCue(bool visible) {
        if (this.DesignMode) visible = false;
        if (visible) {                          
            if (this.Text.Length == 0) {
                this.Text = cue;
                this.SelectAll();
                this.SelectionColor = Color.FromArgb(87, 87, 87);
            }
        }
        else {
            if (this.Text == cue) {
                this.Text = "";
                this.SelectionColor = this.ForeColor;
            }
        }
    }
}

您无法在 RichTextBox 本身中修复它,因为您无法在多行编辑或富文本控件上设置提示横幅。

来自EM_CUEBANNER的文档(强调添加):

评论

用于开始搜索的编辑控件可能会以灰色文本显示“在此处输入搜索”作为文本提示。 当用户单击文本时,文本消失并且用户可以键入。

您不能在多行编辑控件或富编辑控件上设置提示横幅。

GetLastWin32Error()返回 false,因为没有错误。 RichTextBox 已经通知您它没有处理消息(因为SendMessage()返回 false),但这不是错误 - 它只是没有处理消息。 SendMessage返回发送消息的结果; 结果的含义取决于正在发送的消息,在这种情况下,它意味着 RichTextBox 不支持它收到的消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM