Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sm-abdullah
Created February 29, 2016 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sm-abdullah/26a39d10baff8542d5a3 to your computer and use it in GitHub Desktop.
Save sm-abdullah/26a39d10baff8542d5a3 to your computer and use it in GitHub Desktop.
How to create an animated start with C# winform.
public partial class Form1 : Form
{
int radius = 0;
bool isIncreasing = true;
public Form1()
{
InitializeComponent();
//set the startup position of star as center of the screen
this.StartPosition = FormStartPosition.CenterScreen;
//subscirbe mosue down event so we can move star by mouse move
this.MouseDown += Form1_MouseDown;
//subscribe the form load event
this.Load += Form1_Load;
//subscribe the form paint event
this.Paint += Form1_Paint;
//set the form border styel to hide title bar
this.FormBorderStyle = FormBorderStyle.None;
}
// On mouse down, start moving the form.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
this.Capture = false;
Message msg = Message.Create(this.Handle,
WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
WndProc(ref msg);
}
/// <summary>
/// Provide the
/// </summary>
/// <param name="radius"></param>
/// <param name="xCenter"></param>
/// <param name="yCenter"></param>
/// <returns></returns>
private PointF[] DrawStar(float radius, float xCenter, float yCenter)
{
// r: determines the size of the star.
// xc, yc: determine the location of the star.
float sin36 = (float)Math.Sin(36.0 * Math.PI / 180.0);
float sin72 = (float)Math.Sin(72.0 * Math.PI / 180.0);
float cos36 = (float)Math.Cos(36.0 * Math.PI / 180.0);
float cos72 = (float)Math.Cos(72.0 * Math.PI / 180.0);
float r1 = radius * cos72 / cos36;
// Fill the star:
PointF[] pts = new PointF[10];
pts[0] = new PointF(xCenter, yCenter - radius);
pts[1] = new PointF(xCenter + r1 * sin36, yCenter - r1 * cos36);
pts[2] = new PointF(xCenter + radius * sin72, yCenter - radius * cos72);
pts[3] = new PointF(xCenter + r1 * sin72, yCenter + r1 * cos72);
pts[4] = new PointF(xCenter + radius * sin36, yCenter + radius * cos36);
pts[5] = new PointF(xCenter, yCenter + r1);
pts[6] = new PointF(xCenter - radius * sin36, yCenter + radius * cos36);
pts[7] = new PointF(xCenter - r1 * sin72, yCenter + r1 * cos72);
pts[8] = new PointF(xCenter - radius * sin72, yCenter - radius * cos72);
pts[9] = new PointF(xCenter - r1 * sin36, yCenter - r1 * cos36);
return pts;
}
private void Form1_Load(object sender, EventArgs e)
{
//set the Back Color to White
this.BackColor = Color.White;
//set Transparency key to White in order to make form fullay Transparent.
this.TransparencyKey = Color.White; //as transperency key is white so everything with color white will be transparent.
//set height width
this.Height = 220;
this.Width = 220;
//set a timer which will invlidate the form so we can repaint it
Timer timer = new Timer();
timer.Interval = 100;
timer.Tick += Timer_Tick;
timer.Enabled = true;
}
private void Timer_Tick(object sender, EventArgs e)
{
//trigger form paint event
this.Invalidate();
//if growing then increase radius by 5
if (isIncreasing)
{
radius += 5;
}
else //if reducing then decrease radius by 5 pixel
{
radius -= 5;
}
//if radius more then 80 then change direction
if (radius > 80)
{
isIncreasing = false;
}
//if radius les then 7 then let the start to grow again
if (radius < 7)
{
isIncreasing = true;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//get Star Points
var pts = DrawStar(radius, 90, 90);
//creat a BOld Pen and draw blue start..
//as blue is not our transparent color so start will be visible to us
Pen p = new Pen(Color.Blue, 5);
//draw a polygon
e.Graphics.DrawPolygon(p, pts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment