|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhen I was looking for some fancy looking user controls for my .NET applications, I found many commercial custom user controls. And, I decided to implement my own custom looking user entry controls. This is version 1.0 of ExControls. The next versions will contain extra controls, graphics enhancements, and features. Community support and input regarding what to be in version 2.0 will be so much appreciated. If you have any difficulties downloading or running the demo, please contact me. BackgroundTo find all about the ExControls library, take a look at the Klik commercial user controls library. ExControls LibraryThe ExControls library uses round rectangle effects to render the controls as show in the screenshots in the beginning of the article. The following controls have been implemented:
Each one of these control inherits from the
The entry title label holds the title of the entry. The round panel is used to host other controls. The relation between the controls and BaseEntryThe base of all the Entry controls. The gradient drawing and sizing logic is encapsulated inside this base class. This class is an Properties
LabelEntrySimple label entry control.
Properties
TextEntryTextbox entry control.
Properties
MaskedEditEntryMasked edit entry control.
Properties
ComboBoxEntryCombo box entry control.
Properties
ListBoxEntryList box entry control.
Properties
DateTimePickerEntryDate-time picker entry control.
Properties
MonthCalendarEntryCalendar entry control.
Properties
LinkLabelEntryLink label entry control
Properties
PictureBoxEntryPicture box entry control
Properties
RoundRectFormA border-less form with round corners. This form supports dragging by mouse, by clicking any where over it. public partial class RoundCornerForm : Form
{
private bool isDrag = false;
private Size mouseDistance;
public RoundCornerForm()
{
InitializeComponent();
}
private void RoundRectForm_Resize(object sender, EventArgs e)
{
Rectangle rect = new Rectangle(0, 0, Width, Height);
GraphicsPath path = Helper.GetRoundRectPath(rect, 8);
this.Region = new Region(path);
}
private void RoundRectForm_MouseMove(object sender,
MouseEventArgs e)
{
if (isDrag)
{
this.Location = new Point(MousePosition.X -
mouseDistance.Width, MousePosition.Y -
mouseDistance.Height);
}
}
private void RoundRectForm_MouseUp(object sender,
MouseEventArgs e)
{
isDrag = !(e.Button == MouseButtons.Left);
}
private void RoundRectForm_MouseDown(object sender,
MouseEventArgs e)
{
mouseDistance = new Size(MousePosition.X -
Location.X, MousePosition.Y - Location.Y);
isDrag = e.Button == MouseButtons.Left;
}
}
RoundCornerPanelA panel with round corners to host other controls. public partial class RoundCornerPanel : Panel
{
public RoundCornerPanel()
{
InitializeComponent();
}
public RoundCornerPanel(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private void RoundCornerPanel_Resize(object sender, EventArgs e)
{
Rectangle rect = new Rectangle(0, 0, Width, Height);
GraphicsPath path = Helper.GetRoundRectPath(rect, 8);
this.Region = new Region(path);
}
}
ExLabelA label with round corners, and uses gradients to fill the background. public partial class ExLabel : Label
{
private Color backColorStart = Color.Orange;
public Color BackColorStart
{
get { return backColorStart; }
set
{
if (!value.IsEmpty)
{
backColorStart = value;
}
else
{
backColorStart = Color.Orange;
}
}
}
private Color backColorEnd = Color.FromArgb(255, 206, 157);
public Color BackColorEnd
{
get { return backColorEnd; }
set
{
if (!value.IsEmpty)
{
backColorEnd = value;
}
else
{
backColorEnd = Color.FromArgb(255, 206, 157);
}
}
}
public ExLabel()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
}
private void ExLabel_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(0, 0, Width, Height);
LinearGradientBrush b = new LinearGradientBrush(rect,
BackColorStart, BackColorEnd, 90);
GraphicsPath path = Helper.GetRoundRectPath(rect, 8);
e.Graphics.FillPath(b, path);
ContentAlignment align = this.TextAlign;
StringFormat format = new StringFormat();
switch (align)
{
case ContentAlignment.TopLeft:
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopCenter:
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopRight:
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.MiddleLeft:
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleCenter:
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleRight:
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.BottomLeft:
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomCenter:
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomRight:
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Far;
break;
}
rect.Inflate(-10, 0);
e.Graphics.DrawString(Text, Font,
new SolidBrush(ForeColor), rect, format);
}
}
ExTextBoxA text box with no restriction in sizing ( public partial class ExTextBox : TextBox
{
public ExTextBox()
{
InitializeComponent();
this.AutoSize = false;
}
}
Helper classA class with some helper functions to be used with ExControls or any other. Currently, the public class Helper
{
private Helper()
{
}
public static GraphicsPath
GetRoundRectPath(RectangleF rect, float radius)
{
return GetRoundRectPath(rect.X, rect.Y,
rect.Width, rect.Height, radius);
}
public static GraphicsPath GetRoundRectPath(float X,
float Y, float width, float height, float radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
gp.AddArc(X + width - (radius * 2), Y, radius * 2,
radius * 2, 270, 90);
gp.AddLine(X + width, Y + radius, X + width,
Y + height - (radius * 2));
gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2),
radius * 2, radius * 2, 0, 90);
gp.AddLine(X + width - (radius * 2),
Y + height, X + radius, Y + height);
gp.AddArc(X, Y + height - (radius * 2),
radius * 2, radius * 2, 90, 90);
gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
gp.CloseFigure();
return gp;
}
}
Control Properties Attributes PainThe controls property attributes was the most pain while developing the ExControls 1.0. The documentation is poor, and the resources are little. So I added this section to describe some of the attributes I used in ExControls 1.0. DefaultBindingProperty("PropertyName")Attribute to specify which control property to be used by default when binding. LookupBindingProperties("DataSource", "DataMember", "ValueMember", "LookupMember")Attribute to specify which control properties to bind list/combo control to data source. DesignerSerializationVisibility(DesignerSerializationVisibility.Content)Attribute to tell the form designer to serialize all the changes done by the control property that has internal properties. This attribute is used with control properties like: Bindable(true)Attribute to specify whether the control property can be bound to the data source or not. Data BindingThe controls can be bound to any data source like the standard .NET controls. To use ExControls for binding from VS 2005 Data Sources window, select the appropriate ExControl for each field you want to bind to. For example: Manual binding is also available via the Controls data binding properties. What's Next
History
References
|
||||||||||||||||||||||