目的:要在状态栏中显示当前单击的是鼠标左键、中建还是右键,并同时显示鼠标的位置;
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace statusStrip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X,e.Y);
string eventString = null;
switch (e.Button)
{
case MouseButtons.Left:
eventString = "L";
break;
case MouseButtons.Right:
eventString = "R";
break;
case MouseButtons.Middle:
eventString = "M";
break;
case MouseButtons.XButton1:
eventString = "X1";
break;
case MouseButtons.XButton2:
eventString = "X2";
break;
case MouseButtons.None:
default:
break;
}
if (eventString != null)
{
this.toolStripStatusLabel1.Text = "鼠标按下的键为:" + eventString.ToString() +
"鼠标落下的位置为:" + mouseDownLocation.ToString();
}
else
{ this.toolStripStatusLabel1.Text = "鼠标落下的位置为:" + mouseDownLocation.ToString(); }
}
}
}
但调试时状态栏什么都没有显示,怎么回事?
其实是因为我把窗体设置的太小了,只要稍微把窗体拓宽一下下就ok!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace statusStrip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X,e.Y);
string eventString = null;
switch (e.Button)
{
case MouseButtons.Left:
eventString = "L";
break;
case MouseButtons.Right:
eventString = "R";
break;
case MouseButtons.Middle:
eventString = "M";
break;
case MouseButtons.XButton1:
eventString = "X1";
break;
case MouseButtons.XButton2:
eventString = "X2";
break;
case MouseButtons.None:
default:
break;
}
if (eventString != null)
{
this.toolStripStatusLabel1.Text = "鼠标按下的键为:" + eventString.ToString() +
"鼠标落下的位置为:" + mouseDownLocation.ToString();
}
else
{ this.toolStripStatusLabel1.Text = "鼠标落下的位置为:" + mouseDownLocation.ToString(); }
}
}
}
但调试时状态栏什么都没有显示,怎么回事?
其实是因为我把窗体设置的太小了,只要稍微把窗体拓宽一下下就ok!