此功能在借鉴以下链接博文后验证实现,特此感谢,并做补充转发分享!
http://blog.csdn.net/why_dx/article/details/8751976
http://blog.csdn.net/jun502525164/article/details/9079481
http://blog.sina.com.cn/s/blog_9136935e0102vr38.html
http://blog.csdn.net/zzzhaohaizi/article/details/7064823
-------------------------------------------------------------- 分割线 ----------------------------------------------------
利用DockPanel与C#制作窗体浮动和停靠
点击功能窗 然后鼠标拖动form2的效果图如下:
解压文件得到如下图文件:
2、构建主窗体(父窗体):frmMain的。
(1)新建工程:FloatingForm
(2)将DockPanel.config和WeifenLuo.WinFormsUI.Docking.dll复制到当前项目的
FloatingForm\FloatingForm\bin\Debug文件下。
(3)首先添加引用WeifenLuo.WinFormsUI.Docking。
然后点击工具箱右键添加DockPanel控件到工具箱中。
(4)添加主窗体frmMain中,并设置主窗体的IsMdiContainer =true;
(5)在主窗体中添加的DockPanel控件:DockPanel1,并设置DockPanel中的documentstyle:
dockPanel.DocumentStyle = DocumentStyle.DockingMdi;
frmMain的界面如下:(如果添加dockpanel控件到frmMain出现错误则转到注意事项查看)
后台代码如下:
3. 构建需要浮动显示的窗体:FrmFunction。
(1)在当前工程中添加窗体:FrmFunction;(注意:浮动窗体和标签窗体需要继承自DockContent);
(2)为了保证在关闭某一浮动窗体之后,再打开时能够在原位置显示,要对浮动窗体处理,处理窗体的 DockstateChanged事件,标签窗体dock位置改变,记录到公共类;
frmFunction界面如下:(所要浮动的窗体)
后台代码如下:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.ComponentModel; 6 7 using System.Data; 8 9 using System.Drawing; 10 11 using System.Linq; 12 13 using System.Text; 14 15 usingSystem.Windows.Forms; 16 17 usingWeifenLuo.WinFormsUI.Docking; 18 19 20 21 namespace FloatingForm 22 23 { 24 25 public partial class frmFunction : DockContent 26 27 { 28 29 private static frmFunctionInstance; 30 31 32 33 public frmFunction() 34 35 { 36 37 InitializeComponent(); 38 39 } 40 41 public static frmFunction GetInstance() 42 43 { 44 45 if (Instance == null) 46 47 { 48 49 Instance = new frmFunction(); 50 51 } 52 53 return Instance; 54 55 } 56 57 //为了保证在关闭某一浮动窗体之后,再打开时能够在原位置显示,要对浮动窗体处理,处理窗体的DockstateChanged事件,标签窗体dock位置改变,记录到公共类 58 59 private void FrmFunction_DockStateChanged(object sender, EventArgs e) 60 61 { 62 63 //关闭时(dockstate为unknown) 不把dockstate保存 64 65 if (Instance != null) 66 67 { 68 69 if (this.DockState ==DockState.Unknown || this.DockState == DockState.Hidden) 70 71 { 72 73 return; 74 75 } 76 77 AppConfig.ms_FrmFunction =this.DockState; 78 79 } 80 81 } 82 83 private void FrmFunction_FormClosing(object sender, FormClosingEventArgse) 84 85 { 86 87 Instance = null; // 否则下次打开时报错,提示“无法访问已释放对象” 88 89 } 90 91 92 93 } 94 95 }
4. 在当前工程中添加类AppConfig(公共类)。
代码如下:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; 6 7 using System.Text; 8 9 using WeifenLuo.WinFormsUI.Docking; 10 11 12 13 namespace FloatingForm 14 15 { 16 17 class AppConfig 18 19 { 20 21 public static DockState ms_FrmFunction =DockState.DockLeft; // 功能窗体,左端停靠 22 23 } 24 25 }