/* *__________________________________________________________ C R E A T I N G N O N - R E C T A N G U L A R F O R M S Ron Kessler Created 10/5/08 __________________________________________________________ This project shows you how to: Create an oval form Start the form in center screen Change the color and opacity of the form with the TrackBar control. Lets you move the form around by dragging it! * * * */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Oval_Forms { public partial class frmOval : Form { public frmOval() { InitializeComponent(); } private Point mouseOffset; private bool isMouseDown = false; private void frmOval_Load(object sender, EventArgs e) { //---no border/control box/buttons this.FormBorderStyle = FormBorderStyle.None; //---start with normal opacity trackBar1.Minimum = 2; //don't let it become invisible! trackBar1.Maximum = 10; trackBar1.Value = trackBar1.Maximum; //---Oval form centered on screen this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2; this.Top = (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2; System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath(); myPath.AddEllipse(0, 0, this.Width, this.Height); Region myRegion = new Region(myPath); this.Region = myRegion; } private void rdoBlue_CheckedChanged(object sender, EventArgs e) { this.BackColor = Color.Blue; } private void rdoRed_CheckedChanged(object sender, EventArgs e) { this.BackColor = Color.Red; } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void frmOval_MouseDown(object sender, MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X - SystemInformation.FrameBorderSize.Width; yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void frmOval_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; } } private void frmOval_MouseUp(object sender, MouseEventArgs e) { // Changes the isMouseDown field so that the form does //not move unless the user is pressing the left mouse button. if (e.Button == MouseButtons.Left) isMouseDown = false; } private void trackBar1_Scroll(object sender, EventArgs e) { //be sure to cast these to a float because .5 = 50% opacity, .4 = 40%, and so on this.Opacity = (float) trackBar1.Value / (float) trackBar1.Maximum; } } }