Changeset 7484
- Timestamp:
- 02/17/12 23:14:17 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/ToolTipComboBox.cs
r7482 r7484 29 29 30 30 private ComboBoxState comboBoxState = ComboBoxState.Normal; 31 private bool mouseOver = false; 31 32 32 33 public ToolTipComboBox() … … 35 36 SetStyle(ControlStyles.UserPaint, true); 36 37 InitializeComponent(); 37 DrawMode = System.Windows.Forms.DrawMode.OwnerDraw Fixed;38 DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; 38 39 DropDownStyle = ComboBoxStyle.DropDownList; 39 40 } 40 41 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 41 51 protected override void OnDrawItem(DrawItemEventArgs e) { 52 base.OnDrawItem(e); 42 53 var args = new ToolTipRequiredEventArgs(e.Index, Items[e.Index]); 43 54 OnToolTipRequired(args); 44 55 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); 46 57 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); 48 59 else toolTip.Show(args.ToolTip, this, e.Bounds.Right, e.Bounds.Bottom); 49 60 } 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 } 52 80 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 }66 81 } 67 82 … … 69 84 base.OnPaint(e); 70 85 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 71 88 if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser) { 72 89 PushButtonState pb_State = PushButtonState.Normal; … … 77 94 else if (comboBoxState == ComboBoxState.Pressed) 78 95 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, 84 100 SystemInformation.VerticalScrollBarWidth - 4, 85 e.ClipRectangle.Height);101 clip.Height); 86 102 DrawArrow(e.Graphics, r); 87 103 } 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, 91 107 SystemInformation.VerticalScrollBarWidth, 92 e.ClipRectangle.Height);108 clip.Height); 93 109 ComboBoxRenderer.DrawDropDownButton(e.Graphics, r, comboBoxState); 94 110 } 95 Rectangle rect = e.ClipRectangle;111 Rectangle rect = clip; 96 112 rect.Inflate(-2, -4); 97 113 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 } 99 119 } 100 120 … … 106 126 } 107 127 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 108 141 protected override void OnMouseEnter(EventArgs e) { 109 142 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 } 111 156 } 112 157 113 158 protected override void OnMouseLeave(EventArgs e) { 114 159 base.OnMouseLeave(e); 115 comboBoxState = ComboBoxState.Normal; 160 mouseOver = false; 161 if (comboBoxState == ComboBoxState.Hot) { 162 comboBoxState = ComboBoxState.Normal; 163 Invalidate(); 164 } 116 165 } 117 166 … … 119 168 base.OnMouseDown(e); 120 169 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(); 128 171 } 129 172
Note: See TracChangeset
for help on using the changeset viewer.