仿QQ消息做了一个窗体,上面有个Label,当鼠标移出窗体后渐渐消失,但我把鼠标放到Label上,它认为鼠标离开了窗体,这个怎么解决?
解决方案 »
- Object reference not set to an instance of an object.
- ADO.NET问题 请教。
- 急求Sortedlist排序解
- 数据窗口中选中一行要求变颜色怎么实现?
- 求助! 关于用checkbox绑定的bit类型的字段的疑问
- C#做的系统,如何适应不同分辨率的屏幕?
- 还是取文本
- 大家帮忙看看这代码为什么错了?
- 刚接触C#,想找本受用终身的书??
- 如何自画带标题栏的表格?用scrollable control或者Panel实现,不许用子控件!可加到500分!
- 关于SQLite 使用 SQLiteParameter的问题
- CookieContainer.Add出错????????????
可能我问题没有说清楚吧..我给form添加了 mousemove和mouselevel事件,这样做没有问题.但是我在form里面添加了一个label 我把鼠标放到label上. 就执行了mouselevel,怎么能让form知道label也属于form内呢
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
StopRectTimer.Tick += new EventHandler(timer1_Tick);
StopRectTimer.Interval = 50;
StopRectTimer.Enabled = true;
this.TopMost = true;
StopRectTimer.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
}
else
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 8) * (-1));
break;
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 8), this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 8));
break;
}
} } internal AnchorStyles StopAanhor = AnchorStyles.None;
private void mStopAnhor()
{
if (this.Top <= 0 && this.Left <= 0)
{
StopAanhor = AnchorStyles.None;
}
else if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
else if (this.Left <= 0)
{
StopAanhor = AnchorStyles.Left;
}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
{
StopAanhor = AnchorStyles.Bottom;
}
else
{
StopAanhor = AnchorStyles.None;
}
} private void hide_LocationChanged(object sender, EventArgs e)
{
this.mStopAnhor();
}}在WinForm中添加Load事件给Form1_Load,添加LocationChanged事件给hide_LocationChanged。原理:主要是用Timer控件,在实时判断当前窗口的位置,如果在windows窗口的边上时,修改窗口的位置。当窗口隐藏后,实时判断鼠标的位置,如果在窗口附件,窗口显示。
没啥麻烦的....循环+=事件就是了
类似如下
for(Control c in Form.Controls)
{
c.事件+=xxxxxxxxxxxxxxxxxxxxxxxxxxx;
}
给你一个例子参考吧,,不过这个例子是按F5键,,方式都一样:http://www.cnprog.com/questions/14/WinForm程序中如何始终让窗口捕获键盘事件