Skip to content

Instantly share code, notes, and snippets.

@georg-wolflein
Created February 9, 2014 15:33
Show Gist options
  • Save georg-wolflein/8900739 to your computer and use it in GitHub Desktop.
Save georg-wolflein/8900739 to your computer and use it in GitHub Desktop.
A round Windows Form that moves along the mouse when pressed. Based on my previous gist: https://gist.github.com/georgw777/8900639.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace RoundForm
{
public partial class FormRound : Form
{
public FormRound()
{
InitializeComponent();
}
private void FormRound_Paint(object sender, PaintEventArgs e)
{
// Put this in the form paint event void.
// Create a GraphicsPath that should represent the form's shape.
GraphicsPath formShape = new GraphicsPath();
// Add an ellipse to the shape.
// Any shape is valid, including rectangles, etc.
// Check out http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath_methods%28v=vs.110%29.aspx for all possible shapes.
formShape.AddEllipse(0, 0, this.Width, this.Height);
// Set this form's region parameter to the previously created GraphicsPath.
this.Region = new Region(formShape);
}
private bool mouseDown = false;
private int xDifference = 0;
private int yDifference = 0;
private void FormRound_MouseDown(object sender, MouseEventArgs e)
{
xDifference = MousePosition.X - this.Left;
yDifference = MousePosition.Y - this.Top;
mouseDown = true;
}
private void FormRound_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void timerMouse_Tick(object sender, EventArgs e)
{
if (mouseDown)
{
this.Left = MousePosition.X - xDifference;
this.Top = MousePosition.Y - yDifference;
}
}
private void FormRound_Load(object sender, EventArgs e)
{
timerMouse.Start();
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment