您的位置:首页 > 编程语言 > C#

C#实现仿QQ窗口靠边自动隐藏

2014-05-10 21:05 1031 查看
今晚做网络通讯的实验,想实现下类似QQ滴窗口靠近屏幕上方自动隐藏,上网搜了下代码,发现这个比较简单,而且又很好的实现了所需功能,特记录如下,主要用了三个计时器来实现,开始时设置计时器1启用,计时器2 和计时器3禁用,代码很简单但很强大:

/* 其中全局变量:private bool isHiding = false;
private int startY = 0;*/
/// <summary>
/// 监控鼠标和窗口位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
int mouse_x = Cursor.Position.X,mouse_y = Cursor.Position.Y;/*鼠标当前位置*/
int window_x = this.Location.X,window_y = this.Location.Y;/*窗体当前位置*/
int window_width = this.Size.Width,window_height = this.Size.Height;/*窗体宽度高度*/
if (isHiding == false && window_y == 0)/*如果窗体靠近上边缘且未隐藏*/
{
if (window_x - mouse_x > 10 || mouse_x - window_x - window_width > 10
|| mouse_y - window_y - window_height > 10)/*如果鼠标位于窗体之外*/
{
timer1.Enabled = false;
timer2.Enabled = true;
}
}
if (isHiding == true && mouse_y <= 1 && mouse_x > window_x && /*窗体已隐藏且鼠标位于窗体范围内*/
mouse_x < window_x + window_width)
{
timer1.Enabled = false;
timer3.Enabled = true;
}
}
/// <summary>
/// 隐藏界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
int window_height = this.Size.Height;//窗体高度
startY += window_height / 10;
if (startY < window_height)
{
/*产生连续滴效果*/
this.Location = new Point(this.Location.X, -startY);

}
else
{
this.Location = new Point(this.Location.X, 1 - window_height);/*设置窗体在屏幕上的显示的高度为1像素*/
isHiding = true;
timer2.Enabled = false;
timer1.Enabled = true;
}
}
/// <summary>
/// 显示界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer3_Tick(object sender, EventArgs e)
{
int window_height = this.Size.Height;//窗体高度
startY -= window_height / 10;
if (startY > 0)
{
this.Location = new Point(this.Location.X, -startY);
}
else
{
this.Location = new Point(this.Location.X, 0);/*窗体完全在屏幕上显示*/
isHiding = false;
timer3.Enabled = false;
timer1.Enabled = true;
}
}

如果你需要完整实现源代码,请移步:http://download.csdn.net/detail/lgd_yyf/4434075#comment这个是原作者滴代码,
另外,如果有需要,也可以参考这篇文章:http://blog.csdn.net/crystal_lz/article/details/8170491关于C#绘制qq好友列表控件),比较好的绘制了QQ好友列表的控件,可以直接引用作者的dll文件O(∩_∩)O哈哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: