当 DropDownStyle 为 DropDown 时,ComboBox Cue Banner 不是斜体 [英] ComboBox Cue Banner not italic when DropDownStyle is DropDown

查看:19
本文介绍了当 DropDownStyle 为 DropDown 时,ComboBox Cue Banner 不是斜体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

然而,当我们将控件的 DropDownStyle 设置为 ComboBoxStyle.DropDown(即,也允许自由文本输入)时,提示横幅显示,只是不是斜体(这是它通常的显示方式).

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).

有谁知道如何在ComboBoxStyle.DropDown模式下为组合框绘制斜体提示横幅???

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

推荐答案

按设计.当 Style = DropDown 时,组合框的文本部分是一个 TextBox.它以非斜体样式显示提示横幅.您可以使用此代码进行验证.当 Style = DropDownList 时,区分横幅和实际选择是很重要的,这无疑是他们选择斜体显示的原因.TextBox 的做法不同,它在获得焦点时隐藏横幅.

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

这篇关于当 DropDownStyle 为 DropDown 时,ComboBox Cue Banner 不是斜体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆