我们有一个WinForms控件,它是ComboBox
的扩展版本,在没有选择或文本时支持“cue banners”(也就是水印)。我们的控制与此implementation making use of CB_SETCUEBANNER类似。
但是,当我们将控件的DropDownStyle
设置为ComboBoxStyle.DropDown
(也就是说,也允许自由文本输入)时,提示横幅显示,而不是斜体(通常显示的方式)
有谁知道如何在ComboBoxStyle.DropDown
模式下以斜体绘制cue横幅?
答案 0 :(得分:7)
按设计。当Style = DropDown时,组合框的文本部分是TextBox。以非斜体样式显示提示横幅。您可以使用this code进行验证。当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);
}
答案 1 :(得分:0)
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
}
}
}
之后,您可以轻松地将属性文本设置为斜体,并在用户单击或键入时更改它。
例如:
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
}
}