Introduction
I get sick of the standard form shapes. I needed to customize my form shape according to my needs. Not only the form shape, but also any control shape. What about having a circle button or a triangle button? The implementation of all this will be described in easy steps, in this article. First I will start with the required technology.
GraphicPath
Applications use paths to draw outlines of shapes, fill the interiors of shapes, and create clipping regions. The graphics engine maintains the coordinates of geometric shapes in a path in world coordinate space. A path may be composed of any number of figures (sub paths). Each figure is either composed of a sequence of connected lines and curves, or a geometric shape primitive. The starting point of a figure is the first point in the sequence of connected lines and curves. The ending point is the last point in the sequence.
Requirements
- Namespace:
System.Drawing.Drawing2D
Region
A region is scalable because its coordinates are specified in world coordinates. On a drawing surface, however, its interior is dependent on the size and shape of the pixels representing it. An application can use regions to clamp the output of drawing operations. The window manager uses regions to define the drawing area of windows. These regions are called clipping regions. An application can also use regions in hit-testing operations, such as checking whether a point or a rectangle intersects a region. An application can fill a region by using a Brush
object.
Usage
Simply start the application, right click to choose the form background. After you choose the background right, click and select "DrawPath". Start clicking on the form drawing your path. Finally, select "Redraw form" and you will have your form shaped.
The Code
Drawing path points:
private void menuItem2_Click(object sender,
System.EventArgs e)
{
Start=true;
contextMenu1.MenuItems[1].Enabled=true;
}
private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
g= this.CreateGraphics();
p=new Pen(Color.Black);
Point pont=new Point(0,0);
if(Start==true)
{
g.DrawArc(p,e.X,e.Y,2,2,0,360);
pont.X=e.X;
pont.Y=e.Y;
pth[PontNum++]=pont;
for(int j=PontNum;j<1000;j++)
{
pth[j]=pont;
}
}
}
}
Redraw Form:
private void menuItem3_Click(object sender, System.EventArgs e)
{
GraphicsPath NewPth=new GraphicsPath();
NewPth.AddClosedCurve(pth);
this.Region = new Region(NewPth);
this.Invalidate();
Start=false;
contextMenu1.MenuItems[1].Enabled=false;
}
Moving the Form:
private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Right)
{
mov=true;
this.Cursor=Cursors.SizeAll;
x0=e.X;
y0=e.Y;
}
}
private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Right)
{
mov=false;
this.Cursor=Cursors.Default;
}
}
private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Right)
{
this.Top+=e.Y-y0;
this.Left+=e.X-x0;
}
}
Conclusion
I think this is a good base to customize your form shape and shape of other controls in your application. A more practical way to use it is like what we did for the button. You may use this code to get your path points, then add it in the form load of your application. Hope it could be useful. All comments about this are welcome.