Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/17/12 23:14:17 (13 years ago)
Author:
abeham
Message:

#1614: fixed a few bugs in the combobox and made it look and behave like the standard combobox

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/ToolTipComboBox.cs

    r7482 r7484  
    2929
    3030    private ComboBoxState comboBoxState = ComboBoxState.Normal;
     31    private bool mouseOver = false;
    3132
    3233    public ToolTipComboBox()
     
    3536      SetStyle(ControlStyles.UserPaint, true);
    3637      InitializeComponent();
    37       DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
     38      DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
    3839      DropDownStyle = ComboBoxStyle.DropDownList;
    3940    }
    4041
     42    protected override void OnMeasureItem(MeasureItemEventArgs e) {
     43      base.OnMeasureItem(e);
     44      string text = GetItemText(Items[e.Index]);
     45      if (string.IsNullOrEmpty(text)) text = "test";
     46      var rect = TextRenderer.MeasureText(text, Font);
     47      e.ItemHeight = rect.Height - 2;
     48      e.ItemWidth = rect.Width;
     49    }
     50
    4151    protected override void OnDrawItem(DrawItemEventArgs e) {
     52      base.OnDrawItem(e);
    4253      var args = new ToolTipRequiredEventArgs(e.Index, Items[e.Index]);
    4354      OnToolTipRequired(args);
    4455      if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && !string.IsNullOrEmpty(args.ToolTip)) {
    45         var ttRect = e.Graphics.MeasureString(args.ToolTip, Font);
     56        var ttRect = TextRenderer.MeasureText(args.ToolTip, Font);
    4657        if (RectangleToScreen(e.Bounds).Right + ttRect.Width > Screen.FromControl(this).Bounds.Right)
    47           toolTip.Show(args.ToolTip, this, e.Bounds.Left - (int)Math.Ceiling(ttRect.Width), e.Bounds.Bottom);
     58          toolTip.Show(args.ToolTip, this, e.Bounds.Left - ttRect.Width, e.Bounds.Bottom);
    4859        else toolTip.Show(args.ToolTip, this, e.Bounds.Right, e.Bounds.Bottom);
    4960      } else toolTip.Hide(this);
    50       e.DrawBackground();
    51       DrawItemText(e.Graphics, e.Bounds, e.Index);
     61      if (!e.State.HasFlag(DrawItemState.ComboBoxEdit))
     62        e.DrawBackground();
     63      if (e.Index > -1 && Items.Count > 0) {
     64        if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && e.State.HasFlag(DrawItemState.Selected)) {
     65          using (var b = new SolidBrush(BackColor)) {
     66            e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, e.Bounds);
     67          }
     68        } else if (!e.State.HasFlag(DrawItemState.ComboBoxEdit)) {
     69          using (var b = new SolidBrush(ForeColor)) {
     70            e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, e.Bounds);
     71          }
     72        } else {
     73          using (var b = new SolidBrush(ForeColor)) {
     74            Rectangle r = e.Bounds;
     75            r.Offset(0, 1);
     76            e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, r);
     77          }
     78        }
     79      }
    5280      e.DrawFocusRectangle();
    53       base.OnDrawItem(e);
    54     }
    55     protected override void OnDropDownClosed(EventArgs e) {
    56       base.OnDropDownClosed(e);
    57       toolTip.Hide(this);
    58     }
    59 
    60     private void DrawItemText(Graphics graphics, Rectangle rectangle, int index) {
    61       if (index == -1 || Items.Count == 0)
    62         return;
    63       using (var b = new SolidBrush(ForeColor)) {
    64         graphics.DrawString(GetItemText(Items[index]), Font, b, rectangle);
    65       }
    6681    }
    6782
     
    6984      base.OnPaint(e);
    7085      if (Enabled == false) comboBoxState = ComboBoxState.Disabled;
     86      Rectangle clip = Bounds; // using e.ClipRectangle leads to strange behavior when maximizing/unmaximizing, just redraw the entire region all the time
     87      clip.Offset(-clip.X, -clip.Y); // x and y need to be 0
    7188      if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser) {
    7289        PushButtonState pb_State = PushButtonState.Normal;
     
    7794        else if (comboBoxState == ComboBoxState.Pressed)
    7895          pb_State = PushButtonState.Pressed;
    79         Rectangle r = e.ClipRectangle;
    80         r.Inflate(1, 1);
    81         ButtonRenderer.DrawButton(e.Graphics, r, pb_State);
    82         r = new Rectangle(e.ClipRectangle.Right - SystemInformation.VerticalScrollBarWidth + 2,
    83                                     e.ClipRectangle.Top,
     96
     97        ButtonRenderer.DrawButton(e.Graphics, clip, pb_State);
     98        var r = new Rectangle(clip.Right - SystemInformation.VerticalScrollBarWidth + 2,
     99                                    clip.Top,
    84100                                    SystemInformation.VerticalScrollBarWidth - 4,
    85                                     e.ClipRectangle.Height);
     101                                    clip.Height);
    86102        DrawArrow(e.Graphics, r);
    87103      } else {
    88         ComboBoxRenderer.DrawTextBox(e.Graphics, e.ClipRectangle, comboBoxState);
    89         Rectangle r = new Rectangle(e.ClipRectangle.Right - SystemInformation.VerticalScrollBarWidth,
    90                                     e.ClipRectangle.Top,
     104        ComboBoxRenderer.DrawTextBox(e.Graphics, clip, comboBoxState);
     105        var r = new Rectangle(clip.Right - SystemInformation.VerticalScrollBarWidth,
     106                                    clip.Top,
    91107                                    SystemInformation.VerticalScrollBarWidth,
    92                                     e.ClipRectangle.Height);
     108                                    clip.Height);
    93109        ComboBoxRenderer.DrawDropDownButton(e.Graphics, r, comboBoxState);
    94110      }
    95       Rectangle rect = e.ClipRectangle;
     111      Rectangle rect = clip;
    96112      rect.Inflate(-2, -4);
    97113      rect.Offset(1, 0);
    98       DrawItemText(e.Graphics, rect, SelectedIndex);
     114      if (SelectedIndex > -1 && Items.Count > 0) {
     115        using (var b = new SolidBrush(ForeColor)) {
     116          e.Graphics.DrawString(GetItemText(Items[SelectedIndex]), Font, b, rect);
     117        }
     118      }
    99119    }
    100120
     
    106126    }
    107127
     128    protected override void OnDropDown(EventArgs e) {
     129      base.OnDropDown(e);
     130      comboBoxState = ComboBoxState.Pressed;
     131      Invalidate();
     132    }
     133
     134    protected override void OnDropDownClosed(EventArgs e) {
     135      base.OnDropDownClosed(e);
     136      toolTip.Hide(this);
     137      comboBoxState = ComboBoxState.Normal;
     138      Invalidate();
     139    }
     140
    108141    protected override void OnMouseEnter(EventArgs e) {
    109142      base.OnMouseEnter(e);
    110       comboBoxState = ComboBoxState.Hot;
     143      mouseOver = true;
     144      if (comboBoxState == ComboBoxState.Normal) {
     145        comboBoxState = ComboBoxState.Hot;
     146        Invalidate();
     147      }
     148    }
     149
     150    protected override void OnMouseHover(EventArgs e) {
     151      base.OnMouseHover(e);
     152      if (comboBoxState == ComboBoxState.Normal) {
     153        comboBoxState = ComboBoxState.Hot;
     154        Invalidate();
     155      }
    111156    }
    112157
    113158    protected override void OnMouseLeave(EventArgs e) {
    114159      base.OnMouseLeave(e);
    115       comboBoxState = ComboBoxState.Normal;
     160      mouseOver = false;
     161      if (comboBoxState == ComboBoxState.Hot) {
     162        comboBoxState = ComboBoxState.Normal;
     163        Invalidate();
     164      }
    116165    }
    117166
     
    119168      base.OnMouseDown(e);
    120169      comboBoxState = ComboBoxState.Pressed;
    121     }
    122 
    123     protected override void OnMouseUp(MouseEventArgs e) {
    124       base.OnMouseUp(e);
    125       if (Top <= e.Y && Bottom >= e.Y && Left < e.X && Right > e.X)
    126         comboBoxState = ComboBoxState.Hot;
    127       else comboBoxState = ComboBoxState.Normal;
     170      Invalidate();
    128171    }
    129172
Note: See TracChangeset for help on using the changeset viewer.