簡體   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