EM_SETCUEBANNER不适用于RichTextBox

Jac*_*ack 0 c# winapi richtextbox winforms

我一直在使用EM_SETCUEBANNER我的TextBoxes 实现一个占位符,它一直正常工作,直到我使用它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;
            }
Run Code Online (Sandbox Code Playgroud)

在RTB返回时使用它falseMarshal.GetLastWin32Error()具有值0.

我无法找到任何特定于RTB的内容Edit Control Messages.

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

Lar*_*ech 5

您可以尝试自己实现:

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);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)