Form in Different Shapes

selvaganapathy 1 Tallied Votes 219 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);
    }

Dani AI

Generated

This is a solid, minimal demo and a useful learning example — thanks to for the original C# sample and for the VB.NET port. The posts show how to get a pleasing non-rectangular, draggable window with a gradient fill; the items below keep that behavior while making the code more robust, performant, and maintainable.

Cache and dispose GDI objects
Create the shape Region and any brushes once (on load or after a resize) rather than recreating them every Paint. Dispose cached Region/Brush objects when the form is closing to avoid GDI leaks and subtle crashes on long-running apps. Enable double buffering and high-quality smoothing for nicer rendering without flicker. If the gradient is expensive, render it once to an offscreen bitmap and draw that cached bitmap in Paint.

Make dragging and hit-testing reliable
The simple mouse-drag approach works, but it can interfere with child controls. For a cleaner UX, handle the native hit test (WM_NCHITTEST) and return the caption hit for background areas so Windows handles dragging; leave controls alone so they continue to receive clicks. Also prefer calling Close() to end the form lifecycle so standard Closing/Closed events run; provide a keyboard escape handler for accessibility because removing the title bar removes default keyboard affordances.

Alternatives and pitfalls
Avoid relying on TransparencyKey for complex visuals (it is fragile on modern GPUs and with scaling). For true per-pixel alpha, layered windows or a WPF-based UI give better scaling, animation, and DPI behavior. Always test on multiple DPI settings and multiple monitors.

Small helpful pattern (dispose cached objects):

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        cachedBrush?.Dispose();
        cachedRegion?.Dispose();
    }
    base.Dispose(disposing);
}
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.