日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
C#工具欄的編程實現淺析

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

創(chuàng)新互聯建站自2013年起,是專業(yè)互聯網技術服務公司,擁有項目網站制作、成都網站制作網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元岷縣做網站,已為上家服務,為岷縣各地企業(yè)和個人服務,聯系電話:18980820575

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

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

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

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

FormBorderStyle = FixedToolWindow 邊框樣式

ShowInTaskbar = false 不在任務欄顯示

ShowIcon = false 不顯示窗口圖標

TopMost = true 在所有窗口之上

為了解決第二個問題,我們查閱MSDN獲知,當用鼠標拖拽ToolStrip對象釋放鼠標時會觸發(fā)其EndDrag事件。 我們在這個事件的處理方法中判斷當ToolStrip對象的位置被移動到所在的ToolStripPanel之外的時候,創(chuàng)建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 漂浮狀態(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#工具欄漂浮實現  
  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#對象序列化應用淺析

網站題目:C#工具欄的編程實現淺析
轉載源于:http://www.dlmjj.cn/article/dhpjhpi.html