Make a window that pops up from taskbar

2016-01-29 13:26 4 1 收藏

Make a window that pops up from taskbar,Make a window that pops up from taskbar

【 tulaoshi.com - ASP.NET 】

Make a window that pops up from taskbar使用过MSN Messager的朋友都知道,当有好友连接到Internet的时候,在taskbar(任务栏)的右上方,也就是整个屏幕的右下方会缓缓升起一个提示窗口。其实,要使用C#来实现这种效果并不难。整个思想就是不断地改变窗口的位置,在C#中,就是设置Form的Location属性。下面我们来实现它吧JStep 1):制作窗口 要完成设计,一个窗体自然是不可或缺的。新建一个Windows Application,将窗体的FormBorderStyle属性设置为None,ShowInTaskbar属性设置为False.并做适当的界面布置(比如设置BackgroundImage什么的). 至此,窗体制作完毕。Step 2):为窗体添加LostFocus事件处理由于在Step 1)中的设置,这个窗口不能被关闭掉(当然,你可以在窗体上布局一些控件,进而实现窗体的关闭,这里不采用此方法),你得在窗体失去焦点的时候将其关闭。LostFocus事件可以帮助你实现这一点。具体实现如下: //注册事件this.LostFocus +=new EventHandler(MyDialog_LostFocus); //处理private void MyDialog_LostFocus(object sender, EventArgs e) { this.Close(); //关闭窗体即可 }Step 3)实现动画效果要实现我们的目标,表面上看来似乎很容易。其实不然,一个难缠的问题是如何获取taskbar的位置及其高度。我们知道,taskbar可能会位于屏幕四周的某一个位置上,同时taskbar的高度可以动态调整。这里,我采用的解决办法是使用Win32 API,其中使用FindWindow函数来获得taskbar的句柄(handle),并通过GetWindowRect函数来获取其窗口矩形。通过这两个函数的配合使用,实现了动画效果。首先添加一个类NativeAPI,用来声明要使用到的API,如下: public class NativeAPI{[DllImport("user32.dll", EntryPoint="FindWindow")] public static extern IntPtr FindWindow ( string lpClassName, string lpWindowName ); [DllImport("user32.dll", EntryPoint="GetWindowRect")] public static extern bool GetWindowRect ( IntPtr hwnd, out Rectangle lpRect );} 当然,为了能够通过编译,你要添加上:using System;using System.Runtime.InteropServices;using System.Drawing; 我们知道,taskbar的类名为"Shell_TrayWnd",从而调用FindWindow函数获取其窗口句柄不是什么难事。有了这个句柄,调用GetWindowRect获取其窗口矩形也不成问题,一旦获得了这个窗口矩形,根据窗口矩形的位置便可以判断出taskbar所处的位置了。似乎一切都很明了了,不过这里有个小问题你得注意.C#定义的Rectangle实例是使用顶点坐标,宽度和高度表达的,而从API返回的Rectangle实例(本来不是该结构实例,而是RECT结构,这里偷懒,省去该结构的定义J)是采用两个点的方式来定义的,即是(Left,Top)和(Right,Bottom)两点。因此,在使用返回的Rectangle结构实例的时候,要小心这点。下面是实现的代码: private Rectangle taskbarBound;//包围taskbar 的矩形 //……private void ShowIt(){ //获取taskbar的窗口句柄IntPtr handle = NativeAPI.FindWindow("Shell_TrayWnd",null); if(handle == IntPtr.Zero) return; //获取失败,return//获取屏幕分辨率 Size screenSize = SystemInformation.PrimaryMonitorSize; //获取taskbar的窗口矩形if(NativeAPI.GetWindowRect(handle,out taskbarBound)){ //获取成功 if(taskbarBound.X == 0 && taskbarBound.Y == 0){ if(taskbarBound.Width == screenSize.Width) ShowItWhenTaskbarTop();//taskbar在上面 else ShowItWhenTaskbarLeft();//taskbar在左边 } else{ if(taskbarBound.X == 0) ShowItWhenTaskbarBottom();//taskbar在下面 else ShowItWhenTaskbarRight();//taskbar在右边 }} }//若taskbar在上面private void ShowItWhenTaskbarTop(){ //设置窗体的初始位置为右上方,并隐藏在taskbar内 this.Location = new Point(taskbarBound.Width - this.Width,taskbarBound.Height - this.Height);//向下运动 while(this.Location.Y < taskbarBound.Height) this.Location = new Point(this.Location.X,this.Location.Y + 1);}//若taskbar在左边private void ShowItWhenTaskbarLeft(){//设置窗体的初始位置为左下方,并全部在屏幕外 this.Location = new Point(taskbarBound.Width,taskbarBound.Height);//向上移出 while(this.Location.Y > taskbarBound.Height - this.Height) this.Location = new Point(this.Location.X,this.Location.Y - 1);}//若taskbar在右边private void ShowItWhenTaskbarRight(){//设置窗体的初始位置为右下方,并全部在屏幕外this.Location = new Point(taskbarBound.X - this.Width,taskbarBound.Height);//向上运动 while(this.Location.Y > taskbarBound.Height - this.Height) this.Location = new Point(this.Location.X,this.Location.Y - 1);}//若taskbar在下方private void ShowItWhenTaskbarBottom(){//设置窗体的初始位置为右下方,并隐藏在taskbar内this.Location = new Point(taskbarBound.Width - this.Wi

来源:https://www.tulaoshi.com/n/20160129/1490141.html

延伸阅读
标签: ASP
  <% Conn_String = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("link.mdb") Set Conn=Server.CreateObject("ADODB.Connection") Conn.Open Conn_String sql = "select * from cat order by ID" set rs = Conn.Execute (sql) 'Connect to databa...
integer to String : int i = 42;String str = Integer.toString(i); or String str = "" + i double to String : String str = Double.toString(i); long to String : String str = Long.toString(l); float to String : String str = Float.toString(f); String to integ...
标签: 电脑入门
Please send me to stay in the best time 请把我留在最好的时光里 We read the world wrong and say that it deceives us 。我们看错了世界,却说世界欺骗了我们。 However long the night, the dawn will break. A man falls in love with a person。一个人爱上一个人 Laugh Until You Cry; Cry Until You Laugh 笑到终于哭出来;哭到终于...
棒棒糖蛋糕cake pops 1. 将各种食材称量好备用 2. 将鸡蛋打散 3. 加入白砂糖打匀 ...

经验教程

38

收藏

88

精华推荐

javascript对象之window对象

javascript对象之window对象

小可爱小默

Win Setup From USB使用教程

Win Setup From USB使用教程

寂静的香气

Make me some音乐CD专辑包装设计

Make me some音乐CD专辑包装设计

chen_long1024

热门标签

ASP
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部