Form in Different Shapes

selvaganapathy 1 Tallied Votes 208 Views Share

This simple code shows to display the form in different shape. Also using the Gradient Brush to add Look And Feel to your form. Moving the form is also possible.

Requirements
Just draw a Form (Form1) and add Event handler for Load, MouseDown, MouseMove, Paint and DoubleClick for Form1. Then Try this Code

anoop4real commented: Fantastic, I will try it out for sure +0
//Set the Shape for the Form
    private void Form1_Load(object sender, EventArgs e)
    {
        this.FormBorderStyle = FormBorderStyle.None;
        GraphicsPath graphicsPath = new GraphicsPath();
        graphicsPath.AddEllipse(this.ClientRectangle);
        Region region = new Region(graphicsPath);
        this.Region = region;

    }
    //Point object to store the Starting mouse down position
    Point Starting;
    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.Left +=  e.Location.X - Starting.X;
            this.Top += e.Location.Y - Starting.Y;
        }
    }
    //Here Store the Starting mouse down position
    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        Starting = e.Location;
    }
    //To Close the form
    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        this.Dispose();
    }
    //Draw the Backgroud of the Form
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        GraphicsPath graphicsPath = new GraphicsPath();
        graphicsPath.AddEllipse(this.ClientRectangle);
        PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
        pathGradientBrush.CenterColor = Color.White;
        //pathGradientBrush.CenterPoint = new PointF(this.Width / 2, 0.0f);
        pathGradientBrush.SurroundColors = new Color[] { Color.Green };
        e.Graphics.FillEllipse(pathGradientBrush, this.ClientRectangle);
    }
anoop4real 0 Newbie Poster

I know it is too late...... but wanted to share my code too :-)..... VB.Net implementation

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = FormBorderStyle.None
        Dim graphicsPath As GraphicsPath = New GraphicsPath()
        graphicsPath.AddEllipse(Me.ClientRectangle)
        Dim region As Region = New Region(graphicsPath)
        Me.Region = region
    End Sub
    'Point object to store the Starting mouse down position    
    Dim Starting As New Point
    Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If (e.Button = MouseButtons.Left) Then
            Me.Left += e.Location.X - Starting.X
            Me.Top += e.Location.Y - Starting.Y
        End If
    End Sub

    'Here Store the Starting mouse down position   
    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Starting = e.Location
    End Sub
    'To Close the form    
    Private Sub Form1_DoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDoubleClick
        Me.Dispose()
    End Sub

    'Draw the Backgroud of the Form  
    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Paint
        Dim graphicsPath As GraphicsPath = New GraphicsPath()
        graphicsPath.AddEllipse(Me.ClientRectangle)
        'Dim pathGradientBrush As PathGradientBrush = New PathGradientBrush(graphicsPath)
        'pathGradientBrush.CenterColor = Color.White
    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.