简体   繁体   中英

EM_SETCUEBANNER doesn't work on RichTextBox

I've been using EM_SETCUEBANNER to implement a place-holder on my TextBox es, it was working fine until I used it on RichTextBox . It doesn't display any text.

Here's my code:

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

using it on RTB returns false but Marshal.GetLastWin32Error() has value of 0 .

I can't find anything specific to RTB on Edit Control Messages .

How can I fix this?

You can try implementing this yourself:

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

This should not surprise you too much, beyond the documentation , a multi-line TextBox also doesn't support a cue.

Nothing you can't fix, it isn't very hard to do. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing one.

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

You cannot fix it in the RichTextBox itself, because you cannot set a cue banner on multiline edit or rich text controls.

From the documentation for EM_CUEBANNER ( emphasis added):

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 because there's no error. The RichTextBox has already notified you that it's not processing the message (because SendMessage() returned false), but that's not an error - it's simply not processing the message. SendMessage returns the result of the message being sent; the meaning of that result depends on the message being sent, and in this case it means the RichTextBox doesn't support the message it received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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