1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace ComboBoxRendererSample
{
class Form1 : Form
{
public Form1()
: base()
{
this.Size = new Size(300, 300);
CustomComboBox ComboBox1 = new CustomComboBox();
Controls.Add(ComboBox1);
}
[STAThread]
static void Main()
{
// The call to EnableVisualStyles below does not affect
// whether ComboBoxRenderer.IsSupported is true; as long as visual
// styles are enabled by the operating system, IsSupported is true.
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
public class CustomComboBox : Control
{
private Size arrowSize;
private Rectangle arrowRectangle;
private Rectangle topTextBoxRectangle;
private Rectangle bottomTextBoxRectangle;
private ComboBoxState textBoxState = ComboBoxState.Normal;
private ComboBoxState arrowState = ComboBoxState.Normal;
private String bottomText = "Using ComboBoxRenderer";
private bool isActivated = false;
private const int minHeight = 38;
private const int minWidth = 40;
public CustomComboBox()
: base()
{
this.Location = new Point(10, 10);
this.Size = new Size(140, 38);
this.Font = SystemFonts.IconTitleFont;
this.Text = "Click the button";
// Initialize the rectangles to look like the standard combo
// box control.
arrowSize = new Size(18, 20);
arrowRectangle = new Rectangle(ClientRectangle.X +
ClientRectangle.Width - arrowSize.Width - 1,
ClientRectangle.Y + 1,
arrowSize.Width,
arrowSize.Height);
topTextBoxRectangle = new Rectangle(ClientRectangle.X,
ClientRectangle.Y,
ClientRectangle.Width,
arrowSize.Height + 2);
bottomTextBoxRectangle = new Rectangle(ClientRectangle.X,
ClientRectangle.Y + topTextBoxRectangle.Height,
ClientRectangle.Width,
topTextBoxRectangle.Height - 6);
}
// Draw the combo box in the current state.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (!ComboBoxRenderer.IsSupported)
{
this.Parent.Text = "Visual Styles Disabled";
return;
}
this.Parent.Text = "CustomComboBox Enabled";
// Always draw the main text box and drop down arrow in their
// current states
ComboBoxRenderer.DrawTextBox(e.Graphics, topTextBoxRectangle,
this.Text, this.Font, textBoxState);
ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle,
arrowState);
// Only draw the bottom text box if the arrow has been clicked
if (isActivated)
{
ComboBoxRenderer.DrawTextBox(e.Graphics,
bottomTextBoxRectangle, bottomText, this.Font,
textBoxState);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
// Check whether the user clicked the arrow.
if (arrowRectangle.Contains(e.Location) &&
ComboBoxRenderer.IsSupported)
{
// Draw the arrow in the pressed state.
arrowState = ComboBoxState.Pressed;
// The user has activated the combo box.
if (!isActivated)
{
this.Text = "Clicked!";
textBoxState = ComboBoxState.Pressed;
isActivated = true;
}
// The user has deactivated the combo box.
else
{
this.Text = "Click here";
textBoxState = ComboBoxState.Normal;
isActivated = false;
}
// Redraw the control.
Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (arrowRectangle.Contains(e.Location) &&
ComboBoxRenderer.IsSupported)
{
arrowState = ComboBoxState.Normal;
Invalidate();
}
}
}
} |
Partager