示例#1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                if (m_bIsComboBox)
                {
                    Row        CurSel   = m_RowWnd.CurrSel;
                    bool       bHaveRow = m_RowWnd.CurrSel != null;
                    SolidBrush Backcolor;

                    if (m_AllowRowColorCombo && bHaveRow)
                    {
                        Backcolor = new SolidBrush(m_RowWnd.GetRowColor(ColorSelection.BackColor, m_RowWnd.CurrSel));
                    }
                    else
                    {
                        Backcolor = new SolidBrush(m_RowWnd.BackColor);
                    }

                    e.Graphics.FillRectangle(Backcolor, this.ClientRectangle);
                    Backcolor.Dispose();

                    Rectangle BtnRect = GetButtonRect();

                    if (UxThemeManager.VisualStylesEnabled())
                    {
                        IntPtr hDC = e.Graphics.GetHdc();
                        m_ThemeManager[this].DrawThemeBackground(UxThemeElements.COMBOBOX, hDC, (int)ComboBoxPart.DropDownButtonRight,
                                                                 GetComboButtonState(), ref BtnRect, IntPtr.Zero);
                        e.Graphics.ReleaseHdc(hDC);
                    }

                    int nWidth = this.ClientRectangle.Width - BtnRect.Width - 2;
                    if (nWidth > 0)
                    {
                        int nHeight = this.ClientRectangle.Height - 2;

                        if (nHeight > 0)
                        {
                            bool bNeedSelColor = (IsDroppedDown || ContainsFocus) && !ReadOnly;
                            if (bNeedSelColor)
                            {
                                SolidBrush SelBackcolor;
                                if (m_AllowRowColorCombo && bHaveRow)
                                {
                                    SelBackcolor = new SolidBrush(m_RowWnd.GetRowColor(ColorSelection.SelBackColor, CurSel));
                                }
                                else
                                {
                                    SelBackcolor = new SolidBrush(SystemColors.Highlight);
                                }

                                e.Graphics.FillRectangle(SelBackcolor, 1, 1, nWidth, nHeight);
                                SelBackcolor.Dispose();

                                Pen p = new Pen(Color.DarkGray);
                                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                e.Graphics.DrawRectangle(p, 1, 1, nWidth, nHeight);
                                p.Dispose();
                            }

                            nWidth  -= 4;
                            nHeight -= 8;
                            if (nWidth > 0 && nHeight > 0)
                            {
                                StringFormat sf = new StringFormat();
                                sf.FormatFlags   = System.Drawing.StringFormatFlags.NoWrap;
                                sf.Trimming      = System.Drawing.StringTrimming.EllipsisCharacter;
                                sf.Alignment     = StringAlignment.Near;
                                sf.LineAlignment = StringAlignment.Center;
                                Brush ForText;

                                if (bHaveRow)
                                {
                                    ForText = new SolidBrush(m_RowWnd.GetRowColor(bNeedSelColor ? ColorSelection.SelForeColor : ColorSelection.ForeColor, CurSel));
                                }
                                else
                                {
                                    ForText = new SolidBrush(bNeedSelColor ? SystemColors.HighlightText : this.ForeColor);
                                }

                                e.Graphics.DrawString(bHaveRow ? CurSel.ToString(m_strComboFormat) : m_strComboText,
                                                      this.Font, ForText, new Rectangle(5, 5, nWidth, nHeight), sf);
                                ForText.Dispose();
                                sf.Dispose();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            base.OnPaint(e);
        }