Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Drawing;
- using System.Windows.Forms;
- namespace RedLightGreenLight
- {
- public partial class Form1 : Form
- {
- private Label CoordinatesLbl = new Label ();
- private Color NeonRed = Color.FromArgb(221,0,72);
- private Color NeonGreen = Color.FromArgb(57,255,20);
- private Rectangle MyRectangle;
- private int rWidth;
- private int rHeight;
- private bool IsMouseOver;
- public Form1 ()
- {
- InitializeComponent();
- rWidth = 100;
- rHeight = rWidth;
- int centerX = this.Width / 2;
- int centerY = this.Height / 2;
- int xPosition = centerX - (rWidth / 2);
- int yPosition = centerY - (rHeight / 2);
- MyRectangle = new Rectangle (xPosition, yPosition, rWidth, rHeight);
- IsMouseOver = false;
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- Brush RedBrush = new SolidBrush(NeonRed);
- Brush GreenBrush = new SolidBrush(NeonGreen);
- if (IsMouseOver) {
- g.FillRectangle (GreenBrush, MyRectangle);
- } else {
- g.FillRectangle (RedBrush, MyRectangle);
- }
- }
- private void Mouse_Move(object sender, MouseEventArgs e) {
- CoordinatesLbl.Text = "X: " + e.X + " Y: " + e.Y;
- bool temp = IsMouseOver;
- IsMouseOver = IsInRectangle (e.X, e.Y);
- if (temp != IsMouseOver)
- this.Invalidate ();
- }
- private void Mouse_Click(object sender, MouseEventArgs e) {
- if (IsInRectangle (e.X, e.Y)) {
- /*DialogResult msg = MessageBox.Show ("Hello!"); */
- }
- }
- private bool IsInRectangle(int x, int y) {
- int Left = MyRectangle.X;
- int Right = MyRectangle.X + MyRectangle.Width;
- int Top = MyRectangle.Y;
- int Bottom = MyRectangle.Y + MyRectangle.Height;
- if (x > Left && x < Right && y < Bottom && y > Top)
- return true;
- else
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement