ComboBox Cue Banner not italic when DropDownStyle

2019-01-28 10:11发布

问题:

We have a WinForms control that is an extended version of ComboBox that supports "cue banners" (aka watermarks) when there is no selection or text. Our control is similar to this implementation making use of CB_SETCUEBANNER.

However, when we set DropDownStyle for the control to ComboBoxStyle.DropDown (that is, also allows free text entry) the cue banner is showing, just not in italics (which is how it usually shows).

Does anyone know how to draw the cue banner in italics for a combo box in ComboBoxStyle.DropDown mode???

回答1:

By design. When the Style = DropDown, the text portion of the combobox is a TextBox. Which displays the cue banner in non-italic style. You can verify with this code. It is otherwise important to make the distinction between the banner and the actual selection visible when the Style = DropDownList, no doubt the reason they chose to display it italic. TextBox does it differently, it hides the banner when it gets the focus.

Throwing in a non exhausting version:

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


回答2:

Simpler version for C# WinForms:

using System;
using System.Runtime.InteropServices; //Reference for Cue Banner
using System.Windows.Forms;

namespace Your_Project
{
    public partial class Form1 : Form
    {
        private const int TB_SETCUEBANNER = 0x1501; //Textbox Integer
        private const int CB_SETCUEBANNER = 0x1703; //Combobox Integer
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SendMessage(IntPtr hWnd, int msg,
            int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam); //Main Import for Cue Banner

        public Form1()
        {
            InitializeComponent();
            SendMessage(textBox1.Handle, TB_SETCUEBANNER, 0, "Type Here..."); //Cue Banner for textBox1
            SendMessage(comboBox1.Handle, CB_SETCUEBANNER, 0, "Type Here..."); //Cue Banner for comboBox1
        }
    }
}

After that you can easily set the property text to italic and change it for when the user clicks or types.

For Example:

public Form1()
{
    InitializeComponent();
    textBox1.Font = new Font(textBox1.Font, FontStyle.Italic); //Italic Font for textBox1
    comboBox1.Font = new Font(comboBox1.Font, FontStyle.Italic); //Italic Font for comboBox1
    SendMessage(textBox1.Handle, TB_SETCUEBANNER, 0, "Type Here..."); //Cue Banner for textBox1
    SendMessage(comboBox1.Handle, CB_SETCUEBANNER, 0, "Type Here..."); //Cue Banner for comboBox1
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text != "")
    {
        textBox1.Font = new Font(textBox1.Font, FontStyle.Regular); //Regular Font for textBox1 when user types
    }
    else
    {
        textBox1.Font = new Font(textBox1.Font, FontStyle.Italic); //Italic Font for textBox1 when theres no text
    }
}