C# 将窗体嵌入到任务栏中

参考了网上的一篇博客
博客连接
感谢这篇博客,也算是让我这个小白慢慢弄懂了将窗体嵌入到任务栏中的方法。
其实这篇文也只是我对于那篇博客的理解
先看看实现效果
在这里插入图片描述在这里插入图片描述

首先先大致了解一下系统的任务栏
系统的任务栏实质上是一个容器
在这容器之下还有好几个分区,托盘区,时钟区等等

任务栏类名叫“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)
左侧

/XYWH
Shell0063864
Bar011863668
Min012063689

底侧

/XYWH
Shell08241536864
Bar1428241352864
Min1148241353864

右侧

/XYWH
Shell147301536864
Bar14731181536688
Min14731201536689

顶侧

/XYWH
Shell00153640
Bar1420135240
Min1440135340

从上面的数据可以看出win10任务栏在各个位置的时候大概是这个状态。
在这里插入图片描述

再次感谢这篇文章
祭出连接
http://www.360doc.com/content/12/0206/17/747387_184596452.shtml

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值