EM_SETCUEBANNER 不适用于 RichTextBox

EM_SETCUEBANNER doesn't work on RichTextBox

我一直在使用 EM_SETCUEBANNER to implement a place-holder on my TextBoxes, it was working fine until I used it on 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 returns false 上使用它,但 Marshal.GetLastWin32Error() 的值为 0

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

我该如何解决这个问题?

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

来自 EM_CUEBANNER 的文档(添加了重点):

Remarks

An edit control that is used to begin a search may display "Enter search here" in gray text as a textual cue. When the user clicks the text, the text goes away and the user can type.

You cannot set a cue banner on a multiline edit control or on a rich edit control.

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

您可以尝试自己实现:

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);
      }
    }
  }
}

除了 the documentation 之外,您应该不会感到太惊讶,多行 TextBox 也不支持提示。

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

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;
            }
        }
    }
}