none
重写控件:给ComboBox增加PlaceHolder RRS feed

  • 问题

  • 重写了ComboBox控件,以下代码增加placeholder不行。请帮忙看看如何修改,谢谢!

    运行的效果如图所示。

        class ComboBoxEx : ComboBox
        {
            public int CustHeight { get; set; } = 16;
            public ComboBoxEx()
            {
                this.DrawMode = DrawMode.OwnerDrawVariable;
            }
    
            protected override void WndProc(ref Message m)
            {
                if (m.Msg != 0x020A)
                {
                    base.WndProc(ref m);
                }
    
                if (m.Msg == 0xF || m.Msg == 0x133)
                {
                    WmPaint(ref m);
                }
            }
    
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                e.DrawBackground();
                e.DrawFocusRectangle();
                if (this.Items.Count > 0)
                {
                    e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), e.Bounds);
                }
            }
    
            protected override void OnMeasureItem(MeasureItemEventArgs e)
            {
                base.OnMeasureItem(e);
    
                e.ItemHeight = CustHeight;
            }
    
            #region PlaceHoler
            public string PlaceHolder { get; set; }
            private void WmPaint(ref Message m)
            {
                Graphics g = Graphics.FromHwnd(base.Handle);
                if (!string.IsNullOrEmpty(this.PlaceHolder) && string.IsNullOrEmpty(this.Text))
                    g.DrawString(this.PlaceHolder, this.Font, new SolidBrush(Color.LightGray), 0, 0);
            }
            #endregion
        }

    2020年5月22日 7:56

答案

  • Hi leon1526,

    根据我的搜索以及研究,我发现重写Combobox似乎很难做到添加PlaceHolder。

    但是我找到下面的方法可以实现相同的功能。

    以下是代码:

       public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private const int CB_SETCUEBANNER = 0x1703;
    
            [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
            private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]string lParam);
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
                SendMessage(comboBox1.Handle, CB_SETCUEBANNER, 0, "Please select an item...");
                comboBox1.Items.Add("test11");
            }
        }

    结果示意图:

    希望这会帮到你。

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 leon1526 2020年5月25日 13:17
    2020年5月25日 8:46
    版主