MainForm.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:8k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Runtime.Remoting;
  8. using System.Drawing.Drawing2D;
  9. using System.Runtime.Remoting.Channels;
  10. using System.Runtime.Remoting.Channels.Http;
  11. //using PaintCoordinator;
  12. namespace PaintClient
  13. {
  14. /// <summary>
  15. /// MainForm 的摘要说明。
  16. /// </summary>
  17. public class MainForm : System.Windows.Forms.Form
  18. {
  19. private System.Windows.Forms.MainMenu mainMenu1;
  20. private System.Windows.Forms.MenuItem menuItem1;
  21. private System.Windows.Forms.MenuItem menuItemSetting;
  22. private System.Windows.Forms.StatusBar statusBar1;
  23. private System.Windows.Forms.Label label1;
  24. /// <summary>
  25. /// 必需的设计器变量。
  26. /// </summary>
  27. private System.ComponentModel.Container components = null;
  28. //自定义的变量
  29. Coordinator coordinator;
  30. PaintClient.MyCallbackClass callback;
  31. //Store the lines
  32. private ArrayList Strokes=new ArrayList();
  33. //Store the current line
  34. Stroke CurrentStroke=null;
  35. //Store the begin point
  36. Point OriginPoint;
  37. //Store the current point
  38. Point CurrentPoint;
  39. //Default width of the pen
  40. private int m_PenWidth=4;
  41. private System.Windows.Forms.MenuItem menuItem2;
  42. //Default color of the pen
  43. private Color m_PenColor=Color.Black;
  44. protected MainForm()
  45. {
  46. //
  47. // Windows 窗体设计器支持所必需的
  48. //
  49. InitializeComponent();
  50. //
  51. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  52. //
  53. try {
  54. //Read the config file
  55. RemotingConfiguration.Configure("../../PaintClient.exe.config");
  56. callback=new MyCallbackClass();
  57. coordinator=new Coordinator();
  58. //Display the time on statusbar
  59. statusBar1.Text="当前时间: "+coordinator.GetCurrentTime();
  60. //订阅NewStroke事件,并且指定它的处理程序。为事件处理程序创建一个新的委托,当事件发生时,便会触发时间处理程序
  61. coordinator.NewStroke +=new NewStrokeEventHandler(callback.NewStrokeCallback);
  62. }
  63. catch(Exception ex) {
  64. MessageBox.Show(ex.Message);
  65. Close();
  66. }
  67. }
  68. /// <summary>
  69. /// 清理所有正在使用的资源。
  70. /// </summary>
  71. protected override void Dispose( bool disposing )
  72. {
  73. if( disposing )
  74. {
  75. if (components != null) 
  76. {
  77. components.Dispose();
  78. }
  79. }
  80. base.Dispose( disposing );
  81. }
  82. #region Windows 窗体设计器生成的代码
  83. /// <summary>
  84. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  85. /// 此方法的内容。
  86. /// </summary>
  87. private void InitializeComponent()
  88. {
  89. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  90. this.menuItem1 = new System.Windows.Forms.MenuItem();
  91. this.menuItemSetting = new System.Windows.Forms.MenuItem();
  92. this.menuItem2 = new System.Windows.Forms.MenuItem();
  93. this.statusBar1 = new System.Windows.Forms.StatusBar();
  94. this.label1 = new System.Windows.Forms.Label();
  95. this.SuspendLayout();
  96. // 
  97. // mainMenu1
  98. // 
  99. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  100.   this.menuItem1});
  101. // 
  102. // menuItem1
  103. // 
  104. this.menuItem1.Index = 0;
  105. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  106.   this.menuItemSetting,
  107.   this.menuItem2});
  108. this.menuItem1.Text = "选项";
  109. // 
  110. // menuItemSetting
  111. // 
  112. this.menuItemSetting.Index = 0;
  113. this.menuItemSetting.Text = "设置";
  114. this.menuItemSetting.Click += new System.EventHandler(this.menuItemSetting_Click);
  115. // 
  116. // menuItem2
  117. // 
  118. this.menuItem2.Index = 1;
  119. this.menuItem2.Text = "退出";
  120. // 
  121. // statusBar1
  122. // 
  123. this.statusBar1.Location = new System.Drawing.Point(0, 296);
  124. this.statusBar1.Name = "statusBar1";
  125. this.statusBar1.Size = new System.Drawing.Size(336, 22);
  126. this.statusBar1.TabIndex = 0;
  127. // 
  128. // label1
  129. // 
  130. this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  131. this.label1.Location = new System.Drawing.Point(16, 8);
  132. this.label1.Name = "label1";
  133. this.label1.Size = new System.Drawing.Size(304, 23);
  134. this.label1.TabIndex = 1;
  135. this.label1.Text = "分布式系统绘图";
  136. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  137. // 
  138. // MainForm
  139. // 
  140. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  141. this.ClientSize = new System.Drawing.Size(336, 318);
  142. this.Controls.Add(this.label1);
  143. this.Controls.Add(this.statusBar1);
  144. this.Menu = this.mainMenu1;
  145. this.Name = "MainForm";
  146. this.Text = "分布式系统绘图客户端";
  147. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseDown);
  148. this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
  149. this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseUp);
  150. this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
  151. this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseMove);
  152. this.ResumeLayout(false);
  153. }
  154. #endregion
  155. /// <summary>
  156. /// 应用程序的主入口点。
  157. /// </summary>
  158. [STAThread]
  159. static void Main() 
  160. {
  161. Application.Run(Instance());
  162. }
  163. //声明一个静态属性 _instance,保证在外部类中实例化一个MainForm类时,仅有一个实例被返回
  164. private static MainForm _instance;
  165. public static MainForm Instance() {
  166. if(_instance==null)
  167. _instance=new MainForm();
  168. return _instance;
  169. }
  170. private void menuItemSetting_Click(object sender, System.EventArgs e)
  171. {
  172. SettingDialog dlgSetting=new SettingDialog();
  173. dlgSetting.ShowDialog();
  174. m_PenColor=dlgSetting.PenColor;
  175. m_PenWidth=dlgSetting.PenWidth;
  176. dlgSetting.Close();
  177. }
  178. private void MainForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  179. {
  180. if(e.Button==MouseButtons.Left) {
  181. CurrentStroke=new Stroke(e.X,e.Y);
  182. CurrentStroke.PenColor=m_PenColor;
  183. CurrentStroke.PenWidth=m_PenWidth;
  184. OriginPoint=new Point(e.X,e.Y);
  185. CurrentPoint=new Point(e.X,e.Y);
  186. }
  187. }
  188. private void MainForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  189. {
  190. if((e.Button&MouseButtons.Left)!=0&&CurrentStroke!=null) {
  191. CurrentPoint=new Point(e.X,e.Y);
  192. Graphics g=Graphics.FromHwnd(Handle);
  193. Pen pen=new Pen(m_PenColor,m_PenWidth);
  194. g.DrawLine(pen,OriginPoint,CurrentPoint);
  195. pen.Dispose();
  196. g.Dispose();
  197. CurrentStroke.Add(e.X,e.Y);
  198. OriginPoint=CurrentPoint;
  199. }
  200. }
  201. private void MainForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  202. {
  203. if(e.Button==MouseButtons.Left && CurrentStroke!=null) {
  204. CurrentPoint=new Point(e.X,e.Y);
  205. Graphics g=Graphics.FromHwnd(Handle);
  206. Pen pen=new Pen(m_PenColor,m_PenWidth);
  207. g.DrawLine(pen,OriginPoint,CurrentPoint);
  208. pen.Dispose();
  209. g.Dispose();
  210. CurrentStroke.Add(e.X,e.Y);
  211. coordinator.DrawStroke(CurrentStroke);
  212. CurrentStroke=null;
  213. }
  214. }
  215. private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  216. {
  217. lock(Strokes.SyncRoot) {
  218. foreach(Stroke stroke in Strokes) {
  219. stroke.DrawStroke(e.Graphics);
  220. }
  221. }
  222. }
  223. public void OnNewStroke(Stroke stroke) {
  224. lock(Strokes.SyncRoot) {
  225. Strokes.Add(stroke);
  226. }
  227. Graphics g=Graphics.FromHwnd(Handle);
  228. stroke.DrawStroke(g);
  229. g.Dispose();
  230. }
  231. private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  232. {
  233. coordinator.NewStroke -= new NewStrokeEventHandler(callback.NewStrokeCallback);
  234. }
  235. }//End of the class
  236. }