winform让窗体一直显示在桌面上以及FindWindow
想写个程序在电脑桌面上一直显示,类似一个小贴士,提示自己要做的事情等。遇到了技术难题就是怎样把窗体固定在桌面上,经过长时间的搜索和尝试,终于达到了想要的效果,其中涉及到windowsAPI的使用。 先贴上最后的代码: private void Form1_Load(object sender, EventArgs e) { Rectangle ScreenArea = Screen.GetBounds(this); int width = Convert.ToInt32(ScreenArea.Width * 0.7); int height = Convert.ToInt32(ScreenArea.Height * 0.1); //MessageBox.Show(width.ToString()+";"+height.ToString()); this.Location = new Point(width, height); //SetToDeskTop(); SetParent(this.Handle.ToInt32(), FindWindowW("Progman", null)); } //API [DllImport("user32.dll", EntryPoint = "SetParent")] public static extern int SetParent(int hWndChild, int hWndNewParent); [DllImport("user32.dll", EntryPoint = "FindWindowW")] public static extern int FindWindowW(string lpClassName, string lpWindowName); windows提供了操作窗体的API,SetParent函数可以改变某个子窗口的父窗口,通过它,我们可以实现让一个窗体一直显示在桌面上,因为桌面本身也是一个窗体。下面是函数的声明: [DllImport("user32.dll", EntryPoint="SetParent")] public static extern int SetParent (int hWndChild,int hWndNewParent); 第一个参数是要处理的窗体的句柄,也就是我们要编写的应用程序,第二个参数也是一个窗体句柄,就是你要把编写的这个程序放到哪个窗体上,就是哪个窗体的句柄。也就是说,我们只需要提供两个参数就可以了windows窗体,那么,第一个参数很好提供,this.Handle.ToInt32()即可,但是第二个参数需要我们获得桌面窗口的句柄,windowsAPI也提供了查找窗口句柄的函数:FindWindow (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |