轉帖|其它|編輯:郝浩|2011-09-26 15:22:26.000|閱讀 1068 次
概述:C#畫線控件的應用實例介紹之前我們要明白在C#中沒有畫線的控件,這里寫了一個,大家分享。共有兩個控件分別是畫橫線和畫豎線的,關于怎么畫斜線我還沒沒有,有興趣的可以做一個大家分享。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
C#畫線控件的應用實例介紹之前我們要明白在C#中沒有畫線的控件,這里寫了一個,大家分享。共有兩個控件分別是畫橫線和畫豎線的,關于怎么畫斜線我還沒沒有,有興趣的可以做一個大家分享。
C#畫線控件之橫線
1. u?s?i?n?g ?S?y?s?t?e?m?;? ?
2. u?s?i?n?g ?S?y?s?t?e?m?.?C?o?l?l?e?c?t?i?o?n?s?;? ?
3. u?s?i?n?g ?S?y?s?t?e?m?.?C?o?m?p?o?n?e?n?t?M?o?d?e?l?;? ?
4. u?s?i?n?g ?S?y?s?t?e?m?.?D?r?a?w?i?n?g?;? ?
5. u?s?i?n?g ?S?y?s?t?e?m?.?D?a?t?a?;? ?
6. u?s?i?n?g ?S?y?s?t?e?m?.?W?i?n?d?o?w?s?.?F?o?r?m?s?;? ?
7.
8. n?a?m?e?s?p?a?c?e ?J?i?a?s?h?i?.?W?i?n?C?o?n?t?r?o?l?s? ?
9. {? ?
10. /?/?/? ?
11. /?/?/? ?L?i?n?e?X? ?畫?橫?線?控?件?
12. /?/?/? ?
13. p?u?b?l?i?c c?l?a?s?s ?L?i?n?e?X? ?:? ?S?y?s?t?e?m?.?W?i?n?d?o?w?s?.?F?o?r?m?s?.?U?s?e?r?C?o?n?t?r?o?l? ?
14. ?{?
15.
16. #?r?e?g?i?o?n? ?屬?性?定?義?
17. p?r?i?v?a?t?e ?S?y?s?t?e?m?.?D?r?a?w?i?n?g?.?C?o?l?o?r? ?l?i?n?e?C?o?l?o?r?;? ?
18. p?r?i?v?a?t?e i?n?t ?l?i?n?e?W?i?d?t?h?;? ?
19. /?/?/? ?
20. /?/?/? ?線?的?顏?色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v?a?t?e ?S?y?s?t?e?m?.?C?o?m?p?o?n?e?n?t?M?o?d?e?l?.?C?o?n?t?a?i?n?e?r? ?c?o?m?p?o?n?e?n?t?s? ?=? n?u?l?l;? ?
51.
52. /?/?/? ?
53. /?/?/? ?構?造?函?數?初?始?顏?色?和?線?粗?細?
54. /?/?/? ?
55. p?u?b?l?i?c ?L?i?n?e?X?(?)? ?
56. {? ?
57. ?I?n?i?t?i?a?l?i?z?e?C?o?m?p?o?n?e?n?t?(?)?;? ?
58. t?h?i?s.?l?i?n?e?C?o?l?o?r?=t?h?i?s.?F?o?r?e?C?o?l?o?r?;? ?
59. t?h?i?s.?l?i?n?e?W?i?d?t?h?=?1?;? ?
60.
61. }? ?
62.
63. /?/?/? ?
64. /?/?/? ?清?理?所?有?正?在?使?用?的?資?源?。?
65. /?/?/? ?
66. p?r?o?t?e?c?t?e?d o?v?e?r?r?i?d?e v?o?i?d ?D?i?s?p?o?s?e?(? b?o?o?l ?d?i?s?p?o?s?i?n?g? ?)? ?
67. {? ?
68. i?f(? ?d?i?s?p?o?s?i?n?g? ?)? ?
69. ?{? ?
70. i?f(?c?o?m?p?o?n?e?n?t?s? ?!?=? n?u?l?l)? ?
71. {? ?
72. ?c?o?m?p?o?n?e?n?t?s?.?D?i?s?p?o?s?e?(?)?;? ?
73. }? ?
74. ?}? ?
75. b?a?s?e.?D?i?s?p?o?s?e?(? ?d?i?s?p?o?s?i?n?g? ?)?;? ?
76. }?
77.
78. #?r?e?g?i?o?n? ?組?件?設?計?器?生?成?的?代?碼?
79. /?/?/? ?
80. /?/?/? ?設?計?器?支?持?所?需?的?方?法? ?-? ?不?要?使?用?代?碼?編?輯?器? ?
81. /?/?/? ?修?改?此?方?法?的?內?容?。?
82. /?/?/? ?
83. p?r?i?v?a?t?e v?o?i?d ?I?n?i?t?i?a?l?i?z?e?C?o?m?p?o?n?e?n?t?(?)? ?
84. {? ?
85. /?/? ?
86. /?/? ?L?i?n?e?X?
87. /?/? ?
88. t?h?i?s.?N?a?m?e? ?=? "?L?i?n?e?X?";? ?
89. t?h?i?s.?R?e?s?i?z?e? ?+?=? n?e?w ?S?y?s?t?e?m?.?E?v?e?n?t?H?a?n?d?l?e?r?(t?h?i?s.?L?i?n?e?X?_?R?e?s?i?z?e?)?;? ?
90. t?h?i?s.?P?a?i?n?t? ?+?=? ? ?
91. n?e?w ?S?y?s?t?e?m?.?W?i?n?d?o?w?s?.?F?o?r?m?s?.?P?a?i?n?t?E?v?e?n?t?H?a?n?d?l?e?r?(t?h?i?s.?L?i?n?e?X?_?P?a?i?n?t?)?;? ?
92.
93. }?
94. #?e?n?d?r?e?g?i?o?n?
95.
96. p?r?i?v?a?t?e v?o?i?d ?L?i?n?e?X?_?P?a?i?n?t?(o?b?j?e?c?t ?s?e?n?d?e?r?,? ?
97. ?S?y?s?t?e?m?.?W?i?n?d?o?w?s?.?F?o?r?m?s?.?P?a?i?n?t?E?v?e?n?t?A?r?g?s? ?e?)? ?
98. {? ?
99. ?G?r?a?p?h?i?c?s? ?g?=?e?.?G?r?a?p?h?i?c?s?;? ?
100. ?P?e?n? ?m?y?P?e?n? ?=? n?e?w ?P?e?n?(t?h?i?s.?l?i?n?e?C?o?l?o?r?)?;? ?
101. ?m?y?P?e?n?.?W?i?d?t?h?=t?h?i?s.?l?i?n?e?W?i?d?t?h?*?2?;? ?
102. t?h?i?s.?H?e?i?g?h?t?=t?h?i?s.?L?i?n?e?W?i?d?t?h?;? ?
103. ?g?.?D?r?a?w?L?i?n?e?(?m?y?P?e?n?,?0?,?0?,?e?.?C?l?i?p?R?e?c?t?a?n?g?l?e?.?R?i?g?h?t?,?0?)?;? ?
104. }? ?
105.
106. p?r?i?v?a?t?e v?o?i?d ?L?i?n?e?X?_?R?e?s?i?z?e?(object sender, System.EventArgs e)
107. {
108. this.Height=this.lineWidth;
109. }
110. }
111. }
C#畫線控件之豎線
1. u?s?i?n?g ?S?y?s?t?e?m?;? ?
2. u?s?i?n?g ?S?y?s?t?e?m?.?C?o?l?l?e?c?t?i?o?n?s?;? ?
3. u?s?i?n?g ?S?y?s?t?e?m?.?C?o?m?p?o?n?e?n?t?M?o?d?e?l?;? ?
4. u?s?i?n?g ?S?y?s?t?e?m?.?D?r?a?w?i?n?g?;? ?
5. u?s?i?n?g ?S?y?s?t?e?m?.?D?a?t?a?;? ?
6. u?s?i?n?g ?S?y?s?t?e?m?.?W?i?n?d?o?w?s?.?F?o?r?m?s?;? ?
7.
8. n?a?m?e?s?p?a?c?e ?J?i?a?s?h?i?.?W?i?n?C?o?n?t?r?o?l?s? ?
9. {? ?
10. /?/?/? ?
11. /?/?/? ?L?i?n?e?Y? ?畫?豎?線?控?件?
12. ///
13. public class LineY : System.Windows.Forms.UserControl
14. {
15.
16. #region 屬性定義
17. private System.Drawing.Color lineColor;
18. private int lineWidth;
19. ///
20. /// 線的顏色屬性
21. ///
22. public System.Drawing.Color LineColor
23. {
24. set
25. {
26. this.lineColor=value;
27. System.Windows.Forms.PaintEventArgs ep=
28. new PaintEventArgs(this.CreateGraphics(),
29. this.ClientRectangle);
30. this.LineY_Paint(this,ep);
31. }
32. get{return this.lineColor;}
33. }
34. ///
35. /// 線的粗細
36. ///
37. public int LineWidth
38. {
39. set
40. {
41. this.lineWidth=value;
42. System.Windows.Forms.PaintEventArgs ep=
43. new PaintEventArgs(this.CreateGraphics(),
44. this.ClientRectangle);
45. this.LineY_Paint(this,ep);
46. }
47. get{return this.lineWidth;}
48. }
49. #endregion
50. private System.ComponentModel.Container components = null;
51.
52. ///
53. /// 構造函數初始顏色和線粗細
54. ///
55. public LineY()
56. {
57. InitializeComponent();
58. this.lineColor=this.ForeColor;
59. this.lineWidth=1;
60.
61. }
62.
63. ///
64. /// 清理所有正在使用的資源。
65. ///
66. protected override void Dispose( bool disposing )
67. {
68. if( disposing )
69. {
70. if(components != null)
71. {
72. components.Dispose();
73. }
74. }
75. base.Dispose( disposing );
76. }
77.
78. #region 組件設計器生成的代碼
79. ///
80. /// 設計器支持所需的方法 - 不要使用代碼編輯器
81. /// 修改此方法的內容。
82. ///
83. private void InitializeComponent()
84. {
85. //
86. // LineY
87. //
88. this.Name = "LineY";
89. this.Resize +=
90. new System.EventHandler(this.LineY_Resize);
91. this.Paint +=
92. new System.Windows.Forms.PaintEventHandler(this.LineY_Paint);
93.
94. }
95. #endregion
96.
97. private void LineY_Paint(
98. object sender, System.Windows.Forms.PaintEventArgs e)
99. {
100. Graphics g=e.Graphics;
101. Pen myPen = new Pen(this.lineColor);
102. myPen.Width=this.lineWidth*2;
103. this.Width=this.LineWidth;
104. g.DrawLine(myPen,0,0,0,e.ClipRectangle.Bottom);
105. }
106.
107. private void LineY_Resize(
108. object sender, System.EventArgs e)
109. {
110. this.Width=this.lineWidth;
111. }
112. }
113. }
C#畫線控件的開發就向你介紹到這里,希望對你了解和學習C#畫線控件有所幫助。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載