|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article mainly explains how to use a custom Paint event handler to draw your own Windows control (in this case, a Form control). The The class properties The
How it worksFirst of all, we need to override the form events such as protected override void OnPaint(PaintEventArgs e)
{
...
}
protected override void OnMouseDown(MouseEventArgs e)
{
...
}
The Form's style must be set using the method this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.StandardClick, true);
this.SetStyle(ControlStyles.StandardDoubleClick, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, false);
this.UpdateStyles();
The method private bool isMousePointerInArea(Point mousePosition, Rectangle area)
{
Point relativePoint = new Point(0, 0);
relativePoint.X = mousePosition.X - this.Location.X;
relativePoint.Y = mousePosition.Y - this.Location.Y;
return area.Contains(relativePoint);
}
The custom Form painting is all done by the method // Create a new Pen object
p = new Pen(this.OutlineColor, this.OutlineSize);
// Draw the form outline
g.DrawArc(p, rectLeftCorner, 180, 90);
g.DrawArc(p, rectRightCorner, 270, 90);
g.DrawLine(p, edgeRadius, 0, this.Width - edgeRadius, 0);
g.DrawLine(p, 0, edgeRadius, 0, this.Height);
g.DrawLine(p, this.Width - 1, edgeRadius,
this.Width - 1, this.Height);
g.DrawLine(p, 0, this.Height - 1,
this.Width, this.Height - 1);
// Dispose the Pen object
p.Dispose();
p = null;
A custom // Create GraphicsPath to be used to crop the region required
gpRegion = new GraphicsPath();
// Loop through every pixel in the top left corner.
// Create a 1 x 1 rectangle regions of pixels
// that do not match the transparent color
for (int x = rectLeftCorner.X; x < rectLeftCorner.Width; x++)
{
for (int y = rectLeftCorner.Y; y < rectLeftCorner.Height / 2; y++)
{
if (isSameColor(bmp.GetPixel(x, y),
this.transparentColor) == false)
{
gpRegion.AddRectangle(new Rectangle(x, y, 1, 1));
}
}
}
// Loop through every pixel in the top right corner.
// Create a 1 x 1 rectangle regions of pixels
// that do not match the transparent color
for (int x = rectRightCorner.X + 1; x <
rectRightCorner.X +
rectRightCorner.Width + 1; x++)
{
for (int y = rectRightCorner.Y; y <
rectRightCorner.Y + rectRightCorner.Height / 2; y++)
{
if (isSameColor(bmp.GetPixel(x, y),
this.transparentColor) == false)
{
gpRegion.AddRectangle(new Rectangle(x, y, 1, 1));
}
}
}
// Create the remaining rectangular regions
// to complete cover all the windows form area
gpRegion.AddRectangle(new Rectangle(rectLeftCorner.Width, 0,
this.Width - (edgeRadius * 4), rectLeftCorner.Height / 2));
gpRegion.AddRectangle(new Rectangle(0,
rectLeftCorner.Height / 2, this.Width, this.Height));
// Apply region
this.Region = new Region(gpRegion);
History
|
||||||||||||||||||||||