C#工具栏的编程实现浅析

开发 后端
C#工具栏的编程实现的原理是什么呢?C#工具栏的编程实现实例应用是如何的呢?那么本文就向你介绍C#工具栏的编程实现相关的内容。

C#工具栏的编程实现是如何的呢?DotNet2.0开发框架中提供的ToolStrip和ToolStripPanel控件可以方便开发具有可停靠C#工具栏功能的Windows应用程序, ToolStrip对象可以在各个ToolStripPanel间完成拖拽使用,但是如果想实现类似VS IDE 或Office中可以浮动的C#工具栏必须借助于DevExpress等一些第三方的控件或编写一定的代码。 这里介绍一种C#工具栏的编程实现比较简单的方法,只需继承ToolStrip类即可实现上述的效果。

放置到ToolStripPanel上的,当C#工具栏浮动的时候,事实上是改变了其所在的容器对象,从其所在的ToolStripPanel移动到一个漂浮的容器上,因此要实现C#工具栏的浮动必须解决以下两个问题:

◆必须有一个浮动的容器来承载ToolStrip对象。

◆须知道ToolStrip对象何时改变其所在的容器,即在浮动的容器和主窗口上ToolStripPanel之间停靠。

对于第一个问题,我们的解决方案是动态的创建一个Form类作为浮动的容器,命名为ToolStripFloatWindow,该Form对象具有以下的属性:

FormBorderStyle = FixedToolWindow 边框样式

ShowInTaskbar = false 不在任务栏显示

ShowIcon = false 不显示窗口图标

TopMost = true 在所有窗口之上

为了解决第二个问题,我们查阅MSDN获知,当用鼠标拖拽ToolStrip对象释放鼠标时会触发其EndDrag事件。 我们在这个事件的处理方法中判断当ToolStrip对象的位置被移动到所在的ToolStripPanel之外的时候,创建ToolStripFloatWindow对象,并将ToolStrip对象移动到ToolStripFloatWindow上;要使ToolStrip对象恢复到原来的窗体上只要判断ToolStripFloatWindow对象的位置是否移动到了ToolStripPanel上, 当条件满足时将ToolStrip对象移动回ToolStripPanel中并销毁ToolStripFloatWindow对象。

此外,还要解决当ToolStrip对象放置到ToolStripFloatWindow对象上时, ToolStripFloatWindow对象必须与ToolStrip对象的尺寸一致。 还有ToolStripFloatWindow对象被点击了关闭按钮时不能将自己关闭。我们可以做两个类来实现上述的思路。

ToolStripFloatWindow类继承自Form类。

MyToolStrip 继承自ToolStrip类。增加了相应的属性和方法。

C#工具栏之MyToolStrip类的源代码如下:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Text;   
  7. using System.Windows.Forms;   
  8. using System.Runtime.InteropServices;   
  9.  
  10. namespace FloatingToolStrip  
  11. ...{  
  12. public partial class MyToolStrip : ToolStrip  
  13. ...{  
  14. public MyToolStrip()  
  15. ...{  
  16. InitializeComponent();   
  17. this.EndDrag += new EventHandler(MyToolStrip_EndDrag);   
  18. this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);   
  19. }  
  20.  
  21. protected override void OnPaint(PaintEventArgs pe)  
  22. ...{  
  23. // TODO: 在此处添加自定义绘制代码  
  24.  
  25. // 调用基类 OnPaint  
  26. base.OnPaint(pe);   
  27. }  
  28.  
  29. #region 漂浮状态  
  30.  
  31. private ToolStripFloatWindow floatWindow;   
  32.  
  33. public ToolStripFloatWindow FloatWindow  
  34. ...{  
  35. get 
  36. ...{  
  37. return this.floatWindow;   
  38. }  
  39. set 
  40. ...{  
  41. floatWindow = value;   
  42. if (FloatWindow != null)  
  43. ...{  
  44. floatWindow.LocationChanged +=   
  45. new EventHandler(floatWindow_LocationChanged);   
  46. floatWindow.FormClosing +=   
  47. new FormClosingEventHandler(floatWindow_FormClosing);   
  48. }  
  49. }  
  50. }  
  51.  
  52. public bool isFloating  
  53. ...{  
  54. get 
  55. ...{  
  56. return (floatWindow != null);   
  57. }  
  58. }  
  59.  
  60. private ToolStripPanel tsPanel;   
  61.  
  62. public ToolStripPanel ToolStripPanel  
  63. ...{  
  64. get 
  65. ...{  
  66. return this.tsPanel;   
  67. }  
  68. set 
  69. ...{  
  70. tsPanel = value;   
  71. }  
  72. }  
  73.  
  74. #endregion  
  75.  
  76. #region C#工具栏漂浮实现  
  77.  
  78. private void floatWindow_LocationChanged(  
  79. object sender, EventArgs e)  
  80. ...{  
  81. //当floatwindws的位置移动到   
  82. //toolstrippanel中时,将this放置到 toolstripPanel上  
  83. if (this.floatWindow == null)  
  84. ...{  
  85. return;   
  86. }  
  87. Point currentPt = new Point(  
  88. floatWindow.Location.X, floatWindow.Location.Y);   
  89. Point minpt = this.tsPanel.PointToScreen(tsPanel.Location);   
  90. Point maxpt;   
  91. if(this.tsPanel.Height <= 20)...{  
  92. maxpt = new Point(minpt.X +   
  93. this.tsPanel.Width, minpt.Y + 20);   
  94. }else...{  
  95. maxpt = new Point(minpt.X +   
  96. this.tsPanel.Width, minpt.Y + this.tsPanel.Height);   
  97. }  
  98.  
  99. if ((currentPt.X > minpt.X) &&   
  100. (currentPt.X < maxpt.X) &&   
  101. (currentPt.Y > minpt.Y) &&  
  102.  (currentPt.Y < maxpt.Y))  
  103. ...{  
  104. this.floatWindow.Controls.Remove(this);   
  105. this.tsPanel.SuspendLayout();   
  106. this.tsPanel.Controls.Add(this);   
  107. this.Location = this.tsPanel.PointToClient(currentPt);   
  108. this.tsPanel.ResumeLayout();   
  109. this.floatWindow.Dispose();   
  110. this.floatWindow = null;   
  111.  
  112. }   
  113. }  
  114.  
  115. private void MyToolStrip_EndDrag(  
  116. object sender, EventArgs e)  
  117. ...{  
  118. //判断移出时  
  119. if (this.tsPanel == null)  
  120. ...{  
  121. MessageBox.Show("请先设置ToolStripPanel属性");   
  122. return;   
  123. }  
  124. Point endPoint = Cursor.Position;   
  125. int openX, openY;   
  126. openX = endPoint.X;   
  127. openY = endPoint.Y;   
  128. Point clientPt =   
  129. this.tsPanel.Parent.PointToClient(endPoint);   
  130. if (clientPt.Y > tsPanel.Height)  
  131. ...{  
  132. ToolStripFloatWindow fw = new ToolStripFloatWindow();   
  133. this.tsPanel.Controls.Remove(this);   
  134. fw.Controls.Add(this);   
  135. this.Left = 0;   
  136. this.Top = 0;   
  137. this.FloatWindow = fw;   
  138. Point newLoc = new Point(openX, openY);   
  139. fw.Show();   
  140. fw.Location = newLoc;   
  141. fw.SetBounds(newLoc.X, newLoc.Y,   
  142. this.ClientSize.Width, this.ClientSize.Height);   
  143. }  
  144. }  
  145.  
  146. private void floatWindow_FormClosing(  
  147. object sender, FormClosingEventArgs e)  
  148. ...{  
  149. e.Cancel = true;   
  150. }  
  151.  
  152. private void MyToolStrip_SizeChanged(  
  153. object sender, EventArgs e)  
  154. ...{  
  155. if (this.isFloating)  
  156. ...{  
  157. this.floatWindow.Width = this.ClientSize.Width;   
  158. }  
  159. }  
  160.  
  161. #endregion  
  162. //C#工具栏  
  163. }  
  164. }   

C#工具栏编程实现实例结论:该方法实现较简单, 当不愿意使用功能较强大的第三方控件库时可以采用这种方法,缺点是负责浮动的容器是一个窗口,不大美观。

C#工具栏编程实现的基本内容就向你介绍到这里,希望对你了解和学习C#工具栏有所帮助。

【编辑推荐】

  1. .net泛型类的学习总结
  2. 深度剖析C#序列化和反序列化
  3. 深入探讨C#序列化和反序列化
  4. C# XML序列化应用浅析
  5. C#对象序列化应用浅析
责任编辑:仲衡 来源: pin5i.com
相关推荐

2009-08-27 14:12:02

C# interfac

2011-07-21 16:10:48

jQuery Mobi工具栏

2009-11-13 10:06:22

Visual Stud

2009-09-09 18:00:55

C# XML编程

2009-01-16 09:58:07

C#编程C#内存管理垃圾收集

2009-03-10 13:59:41

C#套接字编程

2009-09-07 09:36:29

C# DisposeDispose方法

2009-09-02 17:24:44

C#关机代码

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-20 17:30:56

C#异步编程模式

2011-02-22 17:29:24

konqueror

2009-08-31 16:48:02

C#实现IDispos

2009-09-02 15:34:37

C#实现插件构架

2009-09-01 18:29:24

C#实现多个接口

2009-08-21 17:53:25

C#网络编程客户端程序

2009-08-20 17:47:54

C#异步编程模式

2009-08-27 18:09:49

C#接口的实现

2009-09-07 14:00:57

C#抓取网页

2009-09-03 09:44:02

DropDownLisC#递归

2009-08-12 16:26:30

C#读取XML文档
点赞
收藏

51CTO技术栈公众号