using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CtimerwCs;

namespace StopWatch
{
	/// <summary>
	/// StopWatch ̊Tv̐łB
	/// </summary>
	public class StopWatch : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label Label_ProductName;
		private System.Windows.Forms.TextBox Text_DeviceName;
		private System.Windows.Forms.Label Label_Time;
		private System.Windows.Forms.Button Button_TimInit;
		private System.Windows.Forms.Button Button_CountStart;
		private System.Windows.Forms.Button Button_CountLap;
		private System.Windows.Forms.Button Button_CountReset;
		private System.Windows.Forms.Button Button_CountStop;
		private System.Windows.Forms.Button Button_TimExit;
		private System.Windows.Forms.Button Button_Exit;
		private System.Windows.Forms.TextBox Text_Ret;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

		Ctimerw timerw = new Ctimerw();
		short	CountId;

		public StopWatch()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.Text_DeviceName = new System.Windows.Forms.TextBox();
			this.Label_ProductName = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.Label_Time = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.Text_Ret = new System.Windows.Forms.TextBox();
			this.Button_TimInit = new System.Windows.Forms.Button();
			this.Button_CountStart = new System.Windows.Forms.Button();
			this.Button_CountLap = new System.Windows.Forms.Button();
			this.Button_CountReset = new System.Windows.Forms.Button();
			this.Button_CountStop = new System.Windows.Forms.Button();
			this.Button_TimExit = new System.Windows.Forms.Button();
			this.Button_Exit = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Text_DeviceName,
																					this.Label_ProductName,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(232, 59);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "";
			// 
			// Text_DeviceName
			// 
			this.Text_DeviceName.Location = new System.Drawing.Point(72, 15);
			this.Text_DeviceName.Name = "Text_DeviceName";
			this.Text_DeviceName.Size = new System.Drawing.Size(136, 19);
			this.Text_DeviceName.TabIndex = 3;
			this.Text_DeviceName.Text = "";
			// 
			// Label_ProductName
			// 
			this.Label_ProductName.Location = new System.Drawing.Point(72, 41);
			this.Label_ProductName.Name = "Label_ProductName";
			this.Label_ProductName.Size = new System.Drawing.Size(136, 11);
			this.Label_ProductName.TabIndex = 2;
			this.Label_ProductName.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 41);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "iF";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 19);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "foCXF";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label5,
																					this.Label_Time});
			this.groupBox2.Location = new System.Drawing.Point(8, 72);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(232, 112);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "oߎ";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(184, 48);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(32, 16);
			this.label5.TabIndex = 2;
			this.label5.Text = "[us]";
			// 
			// Label_Time
			// 
			this.Label_Time.Location = new System.Drawing.Point(16, 48);
			this.Label_Time.Name = "Label_Time";
			this.Label_Time.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.Label_Time.Size = new System.Drawing.Size(160, 16);
			this.Label_Time.TabIndex = 0;
			this.Label_Time.Text = "0";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Text_Ret});
			this.groupBox3.Location = new System.Drawing.Point(8, 188);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(336, 45);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "߂l";
			// 
			// Text_Ret
			// 
			this.Text_Ret.Location = new System.Drawing.Point(8, 16);
			this.Text_Ret.Name = "Text_Ret";
			this.Text_Ret.Size = new System.Drawing.Size(320, 19);
			this.Text_Ret.TabIndex = 0;
			this.Text_Ret.Text = "";
			// 
			// Button_TimInit
			// 
			this.Button_TimInit.Location = new System.Drawing.Point(248, 8);
			this.Button_TimInit.Name = "Button_TimInit";
			this.Button_TimInit.Size = new System.Drawing.Size(96, 20);
			this.Button_TimInit.TabIndex = 3;
			this.Button_TimInit.Text = "TimInit";
			this.Button_TimInit.Click += new System.EventHandler(this.Button_TimInit_Click);
			// 
			// Button_CountStart
			// 
			this.Button_CountStart.Location = new System.Drawing.Point(248, 34);
			this.Button_CountStart.Name = "Button_CountStart";
			this.Button_CountStart.Size = new System.Drawing.Size(96, 20);
			this.Button_CountStart.TabIndex = 4;
			this.Button_CountStart.Text = "CountStart";
			this.Button_CountStart.Click += new System.EventHandler(this.Button_CountStart_Click);
			// 
			// Button_CountLap
			// 
			this.Button_CountLap.Location = new System.Drawing.Point(248, 60);
			this.Button_CountLap.Name = "Button_CountLap";
			this.Button_CountLap.Size = new System.Drawing.Size(96, 20);
			this.Button_CountLap.TabIndex = 5;
			this.Button_CountLap.Text = "CountLap";
			this.Button_CountLap.Click += new System.EventHandler(this.Button_CountLap_Click);
			// 
			// Button_CountReset
			// 
			this.Button_CountReset.Location = new System.Drawing.Point(248, 86);
			this.Button_CountReset.Name = "Button_CountReset";
			this.Button_CountReset.Size = new System.Drawing.Size(96, 20);
			this.Button_CountReset.TabIndex = 6;
			this.Button_CountReset.Text = "CountReset";
			this.Button_CountReset.Click += new System.EventHandler(this.Button_CountReset_Click);
			// 
			// Button_CountStop
			// 
			this.Button_CountStop.Location = new System.Drawing.Point(248, 112);
			this.Button_CountStop.Name = "Button_CountStop";
			this.Button_CountStop.Size = new System.Drawing.Size(96, 20);
			this.Button_CountStop.TabIndex = 7;
			this.Button_CountStop.Text = "CountStop";
			this.Button_CountStop.Click += new System.EventHandler(this.Button_CountStop_Click);
			// 
			// Button_TimExit
			// 
			this.Button_TimExit.Location = new System.Drawing.Point(248, 138);
			this.Button_TimExit.Name = "Button_TimExit";
			this.Button_TimExit.Size = new System.Drawing.Size(96, 20);
			this.Button_TimExit.TabIndex = 8;
			this.Button_TimExit.Text = "TimExit";
			this.Button_TimExit.Click += new System.EventHandler(this.Button_TimExit_Click);
			// 
			// Button_Exit
			// 
			this.Button_Exit.Location = new System.Drawing.Point(248, 164);
			this.Button_Exit.Name = "Button_Exit";
			this.Button_Exit.Size = new System.Drawing.Size(96, 20);
			this.Button_Exit.TabIndex = 9;
			this.Button_Exit.Text = "Exit";
			this.Button_Exit.Click += new System.EventHandler(this.Button_Exit_Click);
			// 
			// StopWatch
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(352, 238);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Button_Exit,
																		  this.Button_TimExit,
																		  this.Button_CountStop,
																		  this.Button_CountReset,
																		  this.Button_CountLap,
																		  this.Button_CountStart,
																		  this.Button_TimInit,
																		  this.groupBox3,
																		  this.groupBox2,
																		  this.groupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "StopWatch";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "XgbvEHb`";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new StopWatch());
		}

		//==============================================================================
		// TimInit
		//==============================================================================
		private void Button_TimInit_Click(object sender, System.EventArgs e)
		{
			string	DeviceName;
			string	ProductName;
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// foCX擾
			//-----------------------------------
			DeviceName = Text_DeviceName.Text;
			//-----------------------------------
			// 
			//-----------------------------------
			ret = timerw.Init(ref DeviceName, out ProductName);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimInit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS){
				Text_DeviceName.Text = DeviceName;
				Label_ProductName.Text = ProductName;
			}
		}

		//==============================================================================
		// JEgJn
		//==============================================================================
		private void Button_CountStart_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// JEgJn
			//-----------------------------------
			ret = timerw.CountStart(out CountId);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountStart ret = " + System.Convert.ToString(ret) + " " + ErrorString;
		}

		//==============================================================================
		// bv^C擾
		//==============================================================================
		private void Button_CountLap_Click(object sender, System.EventArgs e)
		{
			uint	Microseconds;
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// bv^C擾
			//-----------------------------------
			ret = timerw.CountLap(CountId, out Microseconds);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountLap ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS){
				Label_Time.Text = System.Convert.ToString(Microseconds);
			}
		}

		//==============================================================================
		// JEgZbg
		//==============================================================================
		private void Button_CountReset_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// JEgZbg
			//-----------------------------------
			ret = timerw.CountReset(CountId);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountReset ret = " + System.Convert.ToString(ret) + " " + ErrorString;
		}

		//==============================================================================
		// JEg~
		//==============================================================================
		private void Button_CountStop_Click(object sender, System.EventArgs e)
		{
			uint	Microseconds;
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// JEg~
			//-----------------------------------
			ret = timerw.CountStop(CountId, out Microseconds);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountStop ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS){
				Label_Time.Text = System.Convert.ToString(Microseconds);
			}
		}

		//==============================================================================
		// TimExit
		//==============================================================================
		private void Button_TimExit_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// I
			//-----------------------------------
			ret = timerw.Exit();
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimExit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
		}

		//==============================================================================
		// I
		//==============================================================================
		private void Button_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
