日期:2014-05-17 浏览次数:20874 次
//窗体
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "MainForm";
this.AutoScaleBaseSize =new System.Drawing.Size(216,14);
this.ClientSize=new System.Drawing.Size(380,280);
this.Text="纯手工打造C#窗体程序";
this.ResumeLayout(false);
//文本框
this.textBox1=new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location=new System.Drawing.Point(24,48);
this.textBox1.Name="textBox1";
this.textBox1.Size=new System.Drawing.Size(224,21);
this.textBox1.TabIndex=0;
this.textBox1.Text="按住鼠标右键在窗体上移动! ";
this.Controls.Add(this.textBox1);
//label1
this.label1=new System.Windows.Forms.Label();
this.label1.Location=new System.Drawing.Point(24,24);
this.label1.Name="label1";
this.label1.Size=new System.Drawing.Size(168,16);
this.label1.TabIndex=1;
this.label1.Text="鼠标的当前应用程序屏幕坐标";
this.Controls.Add(this.label1);
//定义三个鼠标事件处理程序的委托关联
this.MouseDown +=new System.Windows.Forms.MouseEventHandler (this.MainForm_MouseDown);
this.MouseUp+=new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseUp );
this.MouseMove+=new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseMove);
//具体的委托处理程序实现
//鼠标按下
private void MainForm_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Right)
textBox1.Text="横坐标(X):"+e.X+" 纵坐标(Y):"+e.Y;
}
//鼠标移动
private void MainForm_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)
{
if(e.Button== MouseButtons.Right)
textBox1.Text="横坐标(X):"+e.X+" 纵坐标(Y):"+e.Y;
}
//鼠标放开
private void MainForm_MouseUp(object sender,System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Right)
textBox1.Text="";
}