自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#工具欄的編程實(shí)現(xiàn)淺析

開發(fā) 后端
C#工具欄的編程實(shí)現(xiàn)的原理是什么呢?C#工具欄的編程實(shí)現(xiàn)實(shí)例應(yīng)用是如何的呢?那么本文就向你介紹C#工具欄的編程實(shí)現(xiàn)相關(guān)的內(nèi)容。

C#工具欄的編程實(shí)現(xiàn)是如何的呢?DotNet2.0開發(fā)框架中提供的ToolStrip和ToolStripPanel控件可以方便開發(fā)具有可停靠C#工具欄功能的Windows應(yīng)用程序, ToolStrip對象可以在各個(gè)ToolStripPanel間完成拖拽使用,但是如果想實(shí)現(xiàn)類似VS IDE 或Office中可以浮動的C#工具欄必須借助于DevExpress等一些第三方的控件或編寫一定的代碼。 這里介紹一種C#工具欄的編程實(shí)現(xiàn)比較簡單的方法,只需繼承ToolStrip類即可實(shí)現(xiàn)上述的效果。

放置到ToolStripPanel上的,當(dāng)C#工具欄浮動的時(shí)候,事實(shí)上是改變了其所在的容器對象,從其所在的ToolStripPanel移動到一個(gè)漂浮的容器上,因此要實(shí)現(xiàn)C#工具欄的浮動必須解決以下兩個(gè)問題:

◆必須有一個(gè)浮動的容器來承載ToolStrip對象。

◆須知道ToolStrip對象何時(shí)改變其所在的容器,即在浮動的容器和主窗口上ToolStripPanel之間???。

對于第一個(gè)問題,我們的解決方案是動態(tài)的創(chuàng)建一個(gè)Form類作為浮動的容器,命名為ToolStripFloatWindow,該Form對象具有以下的屬性:

FormBorderStyle = FixedToolWindow 邊框樣式

ShowInTaskbar = false 不在任務(wù)欄顯示

ShowIcon = false 不顯示窗口圖標(biāo)

TopMost = true 在所有窗口之上

為了解決第二個(gè)問題,我們查閱MSDN獲知,當(dāng)用鼠標(biāo)拖拽ToolStrip對象釋放鼠標(biāo)時(shí)會觸發(fā)其EndDrag事件。 我們在這個(gè)事件的處理方法中判斷當(dāng)ToolStrip對象的位置被移動到所在的ToolStripPanel之外的時(shí)候,創(chuàng)建ToolStripFloatWindow對象,并將ToolStrip對象移動到ToolStripFloatWindow上;要使ToolStrip對象恢復(fù)到原來的窗體上只要判斷ToolStripFloatWindow對象的位置是否移動到了ToolStripPanel上, 當(dāng)條件滿足時(shí)將ToolStrip對象移動回ToolStripPanel中并銷毀ToolStripFloatWindow對象。

此外,還要解決當(dāng)ToolStrip對象放置到ToolStripFloatWindow對象上時(shí), ToolStripFloatWindow對象必須與ToolStrip對象的尺寸一致。 還有ToolStripFloatWindow對象被點(diǎn)擊了關(guān)閉按鈕時(shí)不能將自己關(guān)閉。我們可以做兩個(gè)類來實(shí)現(xiàn)上述的思路。

ToolStripFloatWindow類繼承自Form類。

MyToolStrip 繼承自ToolStrip類。增加了相應(yīng)的屬性和方法。

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. // 調(diào)用基類 OnPaint  
  26. base.OnPaint(pe);   
  27. }  
  28.  
  29. #region 漂浮狀態(tài)  
  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#工具欄漂浮實(shí)現(xiàn)  
  77.  
  78. private void floatWindow_LocationChanged(  
  79. object sender, EventArgs e)  
  80. ...{  
  81. //當(dāng)floatwindws的位置移動到   
  82. //toolstrippanel中時(shí),將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. //判斷移出時(shí)  
  119. if (this.tsPanel == null)  
  120. ...{  
  121. MessageBox.Show("請先設(shè)置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#工具欄編程實(shí)現(xiàn)實(shí)例結(jié)論:該方法實(shí)現(xiàn)較簡單, 當(dāng)不愿意使用功能較強(qiáng)大的第三方控件庫時(shí)可以采用這種方法,缺點(diǎn)是負(fù)責(zé)浮動的容器是一個(gè)窗口,不大美觀。

C#工具欄編程實(shí)現(xiàn)的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#工具欄有所幫助。

【編輯推薦】

  1. .net泛型類的學(xué)習(xí)總結(jié)
  2. 深度剖析C#序列化和反序列化
  3. 深入探討C#序列化和反序列化
  4. C# XML序列化應(yīng)用淺析
  5. C#對象序列化應(yīng)用淺析
責(zé)任編輯:仲衡 來源: pin5i.com
相關(guān)推薦

2009-08-27 14:12:02

C# interfac

2009-01-16 09:58:07

C#編程C#內(nèi)存管理垃圾收集

2009-03-10 13:59:41

C#套接字編程

2009-09-09 18:00:55

C# XML編程

2009-09-02 17:24:44

C#關(guān)機(jī)代碼

2009-09-07 09:36:29

C# DisposeDispose方法

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2009-08-20 17:30:56

C#異步編程模式

2009-09-01 18:29:24

C#實(shí)現(xiàn)多個(gè)接口

2009-08-21 17:53:25

C#網(wǎng)絡(luò)編程客戶端程序

2009-08-31 16:48:02

C#實(shí)現(xiàn)IDispos

2009-09-02 15:34:37

C#實(shí)現(xiàn)插件構(gòu)架

2009-08-31 17:02:28

C#接口編程

2011-07-21 16:10:48

jQuery Mobi工具欄

2009-11-13 10:06:22

Visual Stud

2009-08-20 17:47:54

C#異步編程模式

2009-09-03 09:44:02

DropDownLisC#遞歸

2009-09-07 14:00:57

C#抓取網(wǎng)頁

2009-08-27 18:09:49

C#接口的實(shí)現(xiàn)

2009-08-12 16:26:30

C#讀取XML文檔
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號