?? mainform.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Drawing.Drawing2D;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
//using PaintCoordinator;
namespace PaintClient
{
/// <summary>
/// MainForm 的摘要說明。
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItemSetting;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
//自定義的變量
Coordinator coordinator;
PaintClient.MyCallbackClass callback;
//Store the lines
private ArrayList Strokes=new ArrayList();
//Store the current line
Stroke CurrentStroke=null;
//Store the begin point
Point OriginPoint;
//Store the current point
Point CurrentPoint;
//Default width of the pen
private int m_PenWidth=4;
private System.Windows.Forms.MenuItem menuItem2;
//Default color of the pen
private Color m_PenColor=Color.Black;
protected MainForm()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
try {
//Read the config file
RemotingConfiguration.Configure("../../PaintClient.exe.config");
callback=new MyCallbackClass();
coordinator=new Coordinator();
//Display the time on statusbar
statusBar1.Text="當前時間: "+coordinator.GetCurrentTime();
//訂閱NewStroke事件,并且指定它的處理程序。為事件處理程序創建一個新的委托,當事件發生時,便會觸發時間處理程序
coordinator.NewStroke +=new NewStrokeEventHandler(callback.NewStrokeCallback);
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
Close();
}
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemSetting = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemSetting,
this.menuItem2});
this.menuItem1.Text = "選項";
//
// menuItemSetting
//
this.menuItemSetting.Index = 0;
this.menuItemSetting.Text = "設置";
this.menuItemSetting.Click += new System.EventHandler(this.menuItemSetting_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "退出";
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 296);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(336, 22);
this.statusBar1.TabIndex = 0;
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(304, 23);
this.label1.TabIndex = 1;
this.label1.Text = "分布式系統繪圖";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 318);
this.Controls.Add(this.label1);
this.Controls.Add(this.statusBar1);
this.Menu = this.mainMenu1;
this.Name = "MainForm";
this.Text = "分布式系統繪圖客戶端";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseDown);
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(Instance());
}
//聲明一個靜態屬性 _instance,保證在外部類中實例化一個MainForm類時,僅有一個實例被返回
private static MainForm _instance;
public static MainForm Instance() {
if(_instance==null)
_instance=new MainForm();
return _instance;
}
private void menuItemSetting_Click(object sender, System.EventArgs e)
{
SettingDialog dlgSetting=new SettingDialog();
dlgSetting.ShowDialog();
m_PenColor=dlgSetting.PenColor;
m_PenWidth=dlgSetting.PenWidth;
dlgSetting.Close();
}
private void MainForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left) {
CurrentStroke=new Stroke(e.X,e.Y);
CurrentStroke.PenColor=m_PenColor;
CurrentStroke.PenWidth=m_PenWidth;
OriginPoint=new Point(e.X,e.Y);
CurrentPoint=new Point(e.X,e.Y);
}
}
private void MainForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if((e.Button&MouseButtons.Left)!=0&&CurrentStroke!=null) {
CurrentPoint=new Point(e.X,e.Y);
Graphics g=Graphics.FromHwnd(Handle);
Pen pen=new Pen(m_PenColor,m_PenWidth);
g.DrawLine(pen,OriginPoint,CurrentPoint);
pen.Dispose();
g.Dispose();
CurrentStroke.Add(e.X,e.Y);
OriginPoint=CurrentPoint;
}
}
private void MainForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left && CurrentStroke!=null) {
CurrentPoint=new Point(e.X,e.Y);
Graphics g=Graphics.FromHwnd(Handle);
Pen pen=new Pen(m_PenColor,m_PenWidth);
g.DrawLine(pen,OriginPoint,CurrentPoint);
pen.Dispose();
g.Dispose();
CurrentStroke.Add(e.X,e.Y);
coordinator.DrawStroke(CurrentStroke);
CurrentStroke=null;
}
}
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
lock(Strokes.SyncRoot) {
foreach(Stroke stroke in Strokes) {
stroke.DrawStroke(e.Graphics);
}
}
}
public void OnNewStroke(Stroke stroke) {
lock(Strokes.SyncRoot) {
Strokes.Add(stroke);
}
Graphics g=Graphics.FromHwnd(Handle);
stroke.DrawStroke(g);
g.Dispose();
}
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
coordinator.NewStroke -= new NewStrokeEventHandler(callback.NewStrokeCallback);
}
}//End of the class
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -