Created
October 10, 2015 12:51
-
-
Save SajjadArifGul/fb02e6f40c717966988c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
// The previous mouse location. | |
private Point OldMousePos = new Point(-1, -1); | |
// See if the mouse has moved. | |
private void MyTimer_Tick(object sender, EventArgs e) | |
{ | |
// See if the cursor has moved. | |
Point new_pos = Control.MousePosition; | |
if (new_pos.Equals(OldMousePos)) return; | |
OldMousePos = new_pos; | |
// Redraw. | |
this.Invalidate(); | |
} | |
// Draw the eyes. | |
private void DrawEyes(Graphics gr) | |
{ | |
// Convert the cursor position into form units. | |
Point local_pos = this.PointToClient(OldMousePos); | |
// Calculate the size of the eye. | |
int hgt = (int)(this.ClientSize.Height * 0.2); | |
int wid = (int)(this.ClientSize.Width * 0.1); | |
// Find the positions of the eyes. | |
int y = (int) ((this.ClientSize.Height - hgt) / 1.9); | |
int x1 = (int)((this.ClientSize.Width) / 2.5); | |
int x2 = wid + 1 * x1; | |
// Create a Bitmap on which to draw. | |
gr.SmoothingMode = SmoothingMode.AntiAlias; | |
// Draw the eyes. | |
DrawEye(gr, local_pos, x1, y, wid, hgt); | |
DrawEye(gr, local_pos, x2, y, wid, hgt); | |
} | |
// Draw an eye here. | |
private void DrawEye(Graphics gr, Point local_pos, int x1, int y1, int wid, int hgt) | |
{ | |
// Draw the outside. | |
gr.FillEllipse(Brushes.White, x1, y1, wid, hgt); | |
gr.DrawEllipse(Pens.Black, x1, y1, wid, hgt); | |
// Find the center of the eye. | |
int cx = x1 + wid / 2; | |
int cy = y1 + hgt / 2; | |
// Get the unit vector pointing towards the mouse position. | |
double dx = local_pos.X - cx; | |
double dy = local_pos.Y - cy; | |
double dist = Math.Sqrt(dx * dx + dy * dy); | |
dx /= dist; | |
dy /= dist; | |
// This point is 1/4 of the way | |
// from the center to the edge of the eye. | |
double px = cx + dx * wid / 4; | |
double py = cy + dy * hgt / 4; | |
// Draw an ellipse 1/2 the size of the eye | |
// centered at (px, py). | |
gr.FillEllipse(Brushes.Black, (int)(px - wid / 4), | |
(int)(py - hgt / 4), wid / 2, hgt / 2); | |
} | |
private void Form1_Paint(object sender, PaintEventArgs e) | |
{ | |
DrawEyes(e.Graphics); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment