Advertisement
Guest User

h

a guest
Feb 9th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3.  
  4. namespace RedLightGreenLight
  5. {
  6. public partial class Form1 : Form
  7. {
  8. private Label CoordinatesLbl = new Label ();
  9. private Color NeonRed = Color.FromArgb(221,0,72);
  10. private Color NeonGreen = Color.FromArgb(57,255,20);
  11. private Rectangle MyRectangle;
  12. private int rWidth;
  13. private int rHeight;
  14. private bool IsMouseOver;
  15.  
  16. public Form1 ()
  17. {
  18. InitializeComponent();
  19.  
  20. rWidth = 100;
  21. rHeight = rWidth;
  22. int centerX = this.Width / 2;
  23. int centerY = this.Height / 2;
  24. int xPosition = centerX - (rWidth / 2);
  25. int yPosition = centerY - (rHeight / 2);
  26.  
  27. MyRectangle = new Rectangle (xPosition, yPosition, rWidth, rHeight);
  28.  
  29.  
  30. IsMouseOver = false;
  31. }
  32.  
  33.  
  34. private void Form1_Paint(object sender, PaintEventArgs e)
  35. {
  36. Graphics g = e.Graphics;
  37. Brush RedBrush = new SolidBrush(NeonRed);
  38. Brush GreenBrush = new SolidBrush(NeonGreen);
  39.  
  40. if (IsMouseOver) {
  41. g.FillRectangle (GreenBrush, MyRectangle);
  42. } else {
  43. g.FillRectangle (RedBrush, MyRectangle);
  44. }
  45. }
  46.  
  47. private void Mouse_Move(object sender, MouseEventArgs e) {
  48.  
  49.  
  50. CoordinatesLbl.Text = "X: " + e.X + " Y: " + e.Y;
  51.  
  52. bool temp = IsMouseOver;
  53. IsMouseOver = IsInRectangle (e.X, e.Y);
  54.  
  55. if (temp != IsMouseOver)
  56. this.Invalidate ();
  57.  
  58. }
  59.  
  60. private void Mouse_Click(object sender, MouseEventArgs e) {
  61. if (IsInRectangle (e.X, e.Y)) {
  62. /*DialogResult msg = MessageBox.Show ("Hello!"); */
  63. }
  64. }
  65.  
  66. private bool IsInRectangle(int x, int y) {
  67. int Left = MyRectangle.X;
  68. int Right = MyRectangle.X + MyRectangle.Width;
  69. int Top = MyRectangle.Y;
  70. int Bottom = MyRectangle.Y + MyRectangle.Height;
  71.  
  72. if (x > Left && x < Right && y < Bottom && y > Top)
  73. return true;
  74. else
  75. return false;
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement