Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:4k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace catchMe
  8. {
  9. /// <summary>
  10. /// Summary description for Form1.
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14.         private System.Windows.Forms.Button btn_catchMe;
  15. private System.Windows.Forms.StatusBar statusBar1;
  16. /// <summary>
  17. /// Required designer variable.
  18. /// </summary>
  19. private System.ComponentModel.Container components = null;
  20. public Form1()
  21. {
  22. //
  23. // Required for Windows Form Designer support
  24. //
  25. InitializeComponent();
  26. //
  27. // TODO: Add any constructor code after InitializeComponent call
  28. //
  29. }
  30. /// <summary>
  31. /// Clean up any resources being used.
  32. /// </summary>
  33. protected override void Dispose( bool disposing )
  34. {
  35. if( disposing )
  36. {
  37. if (components != null) 
  38. {
  39. components.Dispose();
  40. }
  41. }
  42. base.Dispose( disposing );
  43. }
  44. #region Windows Form Designer generated code
  45. /// <summary>
  46. /// Required method for Designer support - do not modify
  47. /// the contents of this method with the code editor.
  48. /// </summary>
  49. private void InitializeComponent()
  50. {
  51. this.btn_catchMe = new System.Windows.Forms.Button();
  52. this.statusBar1 = new System.Windows.Forms.StatusBar();
  53. this.SuspendLayout();
  54. // 
  55. // btn_catchMe
  56. // 
  57. this.btn_catchMe.Location = new System.Drawing.Point(100, 100);
  58. this.btn_catchMe.Name = "btn_catchMe";
  59. this.btn_catchMe.TabIndex = 0;
  60. this.btn_catchMe.Text = "移动的按钮";
  61. this.btn_catchMe.Click += new System.EventHandler(this.btn_catchMe_Click);
  62. // 
  63. // statusBar1
  64. // 
  65. this.statusBar1.Location = new System.Drawing.Point(0, 257);
  66. this.statusBar1.Name = "statusBar1";
  67. this.statusBar1.Size = new System.Drawing.Size(292, 16);
  68. this.statusBar1.TabIndex = 1;
  69. this.statusBar1.Text = "statusBar1";
  70. // 
  71. // Form1
  72. // 
  73. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  74. this.ClientSize = new System.Drawing.Size(292, 273);
  75. this.Controls.Add(this.statusBar1);
  76. this.Controls.Add(this.btn_catchMe);
  77. this.Name = "Form1";
  78. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  79. this.Text = "catch me";
  80. this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
  81. this.ResumeLayout(false);
  82. }
  83. #endregion
  84. /// <summary>
  85. /// The main entry point for the application.
  86. /// </summary>
  87. [STAThread]
  88. static void Main() 
  89. {
  90. Application.Run(new Form1());
  91. }
  92.         private void btn_catchMe_Click(object sender, System.EventArgs e)
  93.         {
  94.             MessageBox.Show("成功捕捉");
  95.         }
  96.         private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  97.         {
  98.             int border = 50;
  99.             int x = e.X;
  100.             int y = e.Y;
  101.             int left = btn_catchMe.Left;
  102.             int right = btn_catchMe.Right;
  103.             int top = btn_catchMe.Top;
  104.             int bottom = btn_catchMe.Bottom;
  105.             /* 鼠标到按钮附近(20个象素) */
  106.             if( x > left - border && x < right + border && y > top - border && y < bottom + border)
  107.             {
  108.                 
  109. string prompt = "鼠标位置("+e.X.ToString()+","+e.Y.ToString()+")";
  110. statusBar1.Text = prompt;
  111. //接近了顶端 则减20
  112. btn_catchMe.Top += (y > top ? -20 : 20);
  113.                 if(btn_catchMe.Top > Form1.ActiveForm.Size.Height || btn_catchMe.Bottom < 0)
  114.                 {
  115.                     btn_catchMe.Top = Form1.ActiveForm.Size.Height/2;
  116.                 }
  117.                 //接近了最左端 也减20
  118. btn_catchMe.Left += (x > left ? -20 : 20);
  119.                 if(btn_catchMe.Left > Form1.ActiveForm.Size.Width || btn_catchMe.Right < 0)
  120.                 {
  121.                     btn_catchMe.Left = Form1.ActiveForm.Size.Width/2;
  122.                 }
  123.             }
  124.         }
  125. }
  126. }