#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views { public partial class ToolTipComboBox : ComboBox { private ComboBoxState comboBoxState = ComboBoxState.Normal; public ToolTipComboBox() : base() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); InitializeComponent(); DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; DropDownStyle = ComboBoxStyle.DropDownList; } protected override void OnDrawItem(DrawItemEventArgs e) { var args = new ToolTipRequiredEventArgs(e.Index, Items[e.Index]); OnToolTipRequired(args); if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && !string.IsNullOrEmpty(args.ToolTip)) { var ttRect = e.Graphics.MeasureString(args.ToolTip, Font); if (RectangleToScreen(e.Bounds).Right + ttRect.Width > Screen.FromControl(this).Bounds.Right) toolTip.Show(args.ToolTip, this, e.Bounds.Left - (int)Math.Ceiling(ttRect.Width), e.Bounds.Bottom); else toolTip.Show(args.ToolTip, this, e.Bounds.Right, e.Bounds.Bottom); } else toolTip.Hide(this); e.DrawBackground(); DrawItemText(e.Graphics, e.Bounds, e.Index); e.DrawFocusRectangle(); base.OnDrawItem(e); } protected override void OnDropDownClosed(EventArgs e) { base.OnDropDownClosed(e); toolTip.Hide(this); } private void DrawItemText(Graphics graphics, Rectangle rectangle, int index) { if (index == -1 || Items.Count == 0) return; using (var b = new SolidBrush(ForeColor)) { graphics.DrawString(GetItemText(Items[index]), Font, b, rectangle); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (Enabled == false) comboBoxState = ComboBoxState.Disabled; if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser) { PushButtonState pb_State = PushButtonState.Normal; if (comboBoxState == ComboBoxState.Disabled) pb_State = PushButtonState.Disabled; else if (comboBoxState == ComboBoxState.Hot) pb_State = PushButtonState.Hot; else if (comboBoxState == ComboBoxState.Pressed) pb_State = PushButtonState.Pressed; Rectangle r = e.ClipRectangle; r.Inflate(1, 1); ButtonRenderer.DrawButton(e.Graphics, r, pb_State); r = new Rectangle(e.ClipRectangle.Right - SystemInformation.VerticalScrollBarWidth + 2, e.ClipRectangle.Top, SystemInformation.VerticalScrollBarWidth - 4, e.ClipRectangle.Height); DrawArrow(e.Graphics, r); } else { ComboBoxRenderer.DrawTextBox(e.Graphics, e.ClipRectangle, comboBoxState); Rectangle r = new Rectangle(e.ClipRectangle.Right - SystemInformation.VerticalScrollBarWidth, e.ClipRectangle.Top, SystemInformation.VerticalScrollBarWidth, e.ClipRectangle.Height); ComboBoxRenderer.DrawDropDownButton(e.Graphics, r, comboBoxState); } Rectangle rect = e.ClipRectangle; rect.Inflate(-2, -4); rect.Offset(1, 0); DrawItemText(e.Graphics, rect, SelectedIndex); } private void DrawArrow(Graphics g, Rectangle rect) { Point p1 = new Point(rect.X + 3, rect.Y + rect.Height / 2 - 2); Point p2 = new Point(rect.X + rect.Width - 3, rect.Y + rect.Height / 2 - 2); Point p3 = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2 + 2); g.FillPolygon(new SolidBrush(System.Drawing.SystemColors.ControlText), new Point[] { p1, p2, p3 }); } protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); comboBoxState = ComboBoxState.Hot; } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); comboBoxState = ComboBoxState.Normal; } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); comboBoxState = ComboBoxState.Pressed; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (Top <= e.Y && Bottom >= e.Y && Left < e.X && Right > e.X) comboBoxState = ComboBoxState.Hot; else comboBoxState = ComboBoxState.Normal; } public event EventHandler ToolTipRequired; protected virtual void OnToolTipRequired(ToolTipRequiredEventArgs args) { var handler = ToolTipRequired; if (handler != null) handler(this, args); } } }