参考了网上的一篇博客
博客连接
感谢这篇博客,也算是让我这个小白慢慢弄懂了将窗体嵌入到任务栏中的方法。
其实这篇文也只是我对于那篇博客的理解
先看看实现效果
首先先大致了解一下系统的任务栏
系统的任务栏实质上是一个容器
在这容器之下还有好几个分区,托盘区,时钟区等等
任务栏类名叫“Shell_TaryWnd” 其区域是
在这之下还有个二级容器为“ReBarWindow32”其区域是
在它之下还有个窗口为“MSTaskSwWClass”它的作用是存放进程最小化后的窗口,他占的区域大概与ReBarWindow32相同
我们要做的就是将窗口嵌入到ReBarWindow32容器之中,为了不遮挡MSTaskSwWClass中的内容,需要将MSTaskSwWClass的位置作调整,为嵌入的窗口留出一定的位置
整个实现的流程,就是找到上述三个窗口的句柄,通过句柄获得窗口的位置以及尺寸的信息,在对其尺寸或位置作修改为窗口腾出位置。
首先引用寻找窗口句柄,设置父窗口,获得窗口尺寸,以及移动窗口的API
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SetParent")]
static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
首先使用FindWindowEx寻找三个容器的句柄
hShell = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
hBar = FindWindowEx(hShell, IntPtr.Zero, "ReBarWindow32", null);
hMin = FindWindowEx(hBar, IntPtr.Zero, "MSTaskSwWClass", null);
在获取三个容器的尺寸
GetWindowRect(hShell, ref rcShell);
GetWindowRect(hBar, ref rcBar);
GetWindowRect(hMin, ref rcMin);
以任务栏在显示器底部为例
修改MSTask容器的尺寸 留出空间嵌入窗口
MoveWindow(hMin, 0, 0, rcMin.Right - rcMin.Left - this.Width, rcMin.Bottom - rcMin.Top, true);
为嵌入窗口设置父窗体
SetParent(this.Handle, hBar);
最后设置嵌入窗口的坐标调整其在任务栏中位置
MoveWindow(this.Handle, rcMin.Right - rcMin.Left + 2, (screenH - rcShell.Y - this.Height)/2 + fixY, this.Width, this.Height, true);
上面的fixY变量是我自己定义的一个修正变量,用来调整窗口的位置来保持在任务栏中垂直居中。
在程序退出的时候重新调整MSTask容器的尺寸来回复初始状态
MoveWindow(hMin, 0, 0, rcMin.Right - rcMin.Left, rcMin.Bottom - rcMin.Top, true);
这是任务栏在显示器底部的例子,还有在左侧,右侧,顶部,就要根据实际的值做修改
——————————————分割线———————————————
任务栏在不同位置时的尺寸数据
环境(Window10 1920**1020 缩放125% 实际显示 1563*864)
左侧
/ | X | Y | W | H |
---|---|---|---|---|
Shell | 0 | 0 | 63 | 864 |
Bar | 0 | 118 | 63 | 668 |
Min | 0 | 120 | 63 | 689 |
底侧
/ | X | Y | W | H |
---|---|---|---|---|
Shell | 0 | 824 | 1536 | 864 |
Bar | 142 | 824 | 1352 | 864 |
Min | 114 | 824 | 1353 | 864 |
右侧
/ | X | Y | W | H |
---|---|---|---|---|
Shell | 1473 | 0 | 1536 | 864 |
Bar | 1473 | 118 | 1536 | 688 |
Min | 1473 | 120 | 1536 | 689 |
顶侧
/ | X | Y | W | H |
---|---|---|---|---|
Shell | 0 | 0 | 1536 | 40 |
Bar | 142 | 0 | 1352 | 40 |
Min | 144 | 0 | 1353 | 40 |
从上面的数据可以看出win10任务栏在各个位置的时候大概是这个状态。
再次感谢这篇文章
祭出连接
http://www.360doc.com/content/12/0206/17/747387_184596452.shtml