ComboBox Text Align Vertically Center

后端 未结 2 1153
旧时难觅i
旧时难觅i 2021-01-21 08:05

I created the custom combobox on .net framework 1.1, i can custom draw dropdown items, but i can\'t set or draw the combobox text on Middle Left , combobox text always render t

相关标签:
2条回答
  • 2021-01-21 08:32

    ok, below code doesn't answer the actual question about the Text portion; Hans got it right, as usual.

    I keep the answer because I think it does a few things better than OP code..

        if (!DesignMode)
        {
            if (e.Index > -1)
            {
               using (StringFormat fmt = new StringFormat() 
                 { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
               {
    
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    e.Graphics.FillRectangle(SystemBrushes.MenuHighlight, e.Bounds);
                    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), 
                                            e.Font,SystemBrushes.HighlightText, e.Bounds, fmt);
                }
                else
                {
                    e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), 
                                            e.Font, SystemBrushes.MenuText,e.Bounds, fmt);
                }
             }
          }
        }
    

    Instead of calculating a centered position I use the DrawString overload that takes a target rectangle and add a StringFormat to center in both directions. StringFormat was available since .Net 1.1 and indeed is IDisposable, so we should dipose of each we create, best in a using clause..

    Note that for drawing controls the use of TextRenderer is encouraged but only came with .Net 2.0.

    Also note that I substituted the Brushes for SystemBrushes..

    Also: My ComboBox doesn't place the text in its Text portion top-left but middle-left. Maybe the old .Net1.1 control is the culprit?

    0 讨论(0)
  • 2021-01-21 08:37

    In an owner draw ComboBox the text of the Edit part of the control will always be shown at top left, regardless of the height of the ItemHeight.

    To position the Edit part vertically in middle, you can find the Edit element using GetComboBoxInfo and then using SetWindowPos set a new position for it to stand vertically in middle of the ComboBox.

    You need to reposition it when the control size changes. Also you need to fill the background of ComboBox with a Color.

    Here is the code that I used:

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    public class MyComboBox : ComboBox
    {
        public MyComboBox()
        {
            SetStyle(ControlStyles.ResizeRedraw, true);
            DrawMode = DrawMode.OwnerDrawFixed;
            ItemHeight = 40;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
            public int Width { get { return Right - Left; } }
            public int Height { get { return Bottom - Top; } }
        }
    
        private const int SWP_NOSIZE = 0x0001;
        private const int SWP_NOZORDER = 0x0004;
        private const int SWP_SHOWWINDOW = 0x0040;
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, 
            int X, int Y, int cx, int cy, int uFlags);
    
        [DllImport("user32.dll")]
        public static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);
    
        [StructLayout(LayoutKind.Sequential)]
        public struct COMBOBOXINFO
        {
            public int cbSize;
            public RECT rcItem;
            public RECT rcButton;
            public int stateButton;
            public IntPtr hwndCombo;
            public IntPtr hwndEdit;
            public IntPtr hwndList;
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            SetupEdit();
            Invalidate();
        }
        private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xF)
            {
                using (var g = this.CreateGraphics())
                {
                    var r = new Rectangle(2, 2,
                        ClientRectangle.Width - buttonWidth - 2,
                        ClientRectangle.Height - 4);
                    g.FillRectangle(Brushes.White, r);
                }
            }
            base.WndProc(ref m);
        }
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            SetupEdit();
        }
        private void SetupEdit()
        {
            var info = new COMBOBOXINFO();
            info.cbSize = Marshal.SizeOf(info);
            GetComboBoxInfo(this.Handle, ref info);
            SetWindowPos(info.hwndEdit, IntPtr.Zero, 3,
                (this.Height - Font.Height) / 2,
                ClientRectangle.Width - buttonWidth - 3,
                ClientRectangle.Height - Font.Height - 4,
                SWP_NOZORDER);
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            e.DrawBackground();
            var txt = "";
            if (e.Index >= 0)
                txt = GetItemText(Items[e.Index]);
            TextRenderer.DrawText(e.Graphics, txt, Font, e.Bounds, 
                ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
        }
    }
    

    0 讨论(0)
提交回复
热议问题