当DropDownStyle是DropDown时,ComboBox Cue Banner不是斜体

Red*_*dog 4 .net c# combobox winforms-interop winforms

我们有一个WinForms控件,它是一个扩展版本,ComboBox在没有选择或文本时支持"cue banners"(又名水印).我们的控制与使用CB_SETCUEBANNER的实现类似.

但是,当我们DropDownStyle为控件设置ComboBoxStyle.DropDown(也就是说,也允许自由文本输入)时,cue横幅显示,而不是斜体(这通常是如何显示).

有谁知道如何在ComboBoxStyle.DropDown模式中以斜体绘制一个组合框的提示横幅???

Han*_*ant 7

按设计.当Style = DropDown时,组合框的文本部分是TextBox.以非斜体样式显示提示横幅.您可以使用此代码进行验证.当Style = DropDownList时,横幅和实际选择之间的区别是可见的,这无疑是他们选择将其显示为斜体的原因.TextBox以不同的方式完成它,它在获得焦点时隐藏了横幅.

抛出一个不疲惫的版本:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class CueComboBox : ComboBox {
    private string mCue;
    public string Cue {
        get { return mCue; }
        set {
            mCue = value;
            updateCue();
        }
    }
    private void updateCue() {
        if (this.IsHandleCreated && mCue != null) {
            SendMessage(this.Handle, 0x1703, (IntPtr)0, mCue);
        }
    }
    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        updateCue();
    }
    // P/Invoke
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, string lp);
}
Run Code Online (Sandbox Code Playgroud)

  • 不可以,您不能拥有一个文本框的所有权。在我看来,您已经准备好使用WPF。 (2认同)