日期:2014-05-19  浏览次数:21385 次

请问类似于QQ界面吸附于桌面边缘是怎么做的?
如题。

------解决方案--------------------
首先得到自身窗口的x,y
其次得到操作系统窗口的x,y
以上都是可以取得的,
判断当两个x,两个y的差小于什么数值的时候,隐藏之,

得到当前鼠标的x,y
判断当此x,y与操作系统窗口的x,y的差小于什么数值的时候,显示之,

哈哈,我是光说不练,想来思路应该差不多,关键是用好SystemHook
------解决方案--------------------
OnMouseDragBegin事件里,不断判断主窗口右边x坐标和上边y坐标的位置,如果达到了窗口边缘(比如y=0或者x=1024),则不允许过界的移动,同时设一个Tag,表示设定自动隐藏。

OnMouseLeave事件的sender如果是主窗口,且Tag为自动隐藏,这时候重绘主窗口,将其变成一条线,读取窗口坐标,如果y=0,则绘制横线,长度为主窗口宽度;如果x=1024,则绘制竖线,如果y=0,则绘制横线。

OnMouseEnter事件的sender如果是主窗口,且Tag为自动隐藏,则重绘主窗口。窗口就出来了。
------解决方案--------------------
要像QQ窗体那样,到达屏幕的边缘时自动缩到一边吗,可以在你的窗体上拖个Timer控件上去,然后在Tick事件里写代码...
private void timer1_Tick(object sender, EventArgs e)
{
if (this.WindowState != System.Windows.Forms.FormWindowState.Minimized)
{
if (Cursor.Position.X > this.Left && Cursor.Position.X < this.Right && Cursor.Position.Y > this.Top && Cursor.Position.Y < this.Bottom)
{
if (this.Top < 0)
{
this.Top = -5;
this.Show();
}
else if (this.Left < 0)
{
this.Left = -5;
this.Show();
}
else if (this.Left + this.Width > = Screen.PrimaryScreen.WorkingArea.Width)
{
this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 5;
this.Show();
}
}
else
{
if (this.Top <= 4)
{
this.Top = 5 - this.Height;
if (this.Left <= 4)
{
this.Left = -5;
}
else if (this.Left + this.Width > = Screen.PrimaryScreen.WorkingArea.Width - 4)
{
this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 5;
}
}
else if (this.Left <= 4)
{
this.Left = 5 - this.Width;
}
else if (this.Left + this.Width > = Screen.PrimaryScreen.WorkingArea.Width - 4)
{
this.Left = Screen.PrimaryScreen.WorkingArea.Width - 5;
}
}
}

}

LZ把窗口拖到屏幕边缘试试看,是不是要这种效果