⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 Windows Mobile用户签名程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.textDecrypt.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textDecrypt.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textDecrypt.Location = new System.Drawing.Point(8, 24);
			this.textDecrypt.Multiline = true;
			this.textDecrypt.Name = "textDecrypt";
			this.textDecrypt.ReadOnly = true;
			this.textDecrypt.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textDecrypt.Size = new System.Drawing.Size(352, 172);
			this.textDecrypt.TabIndex = 2;
			this.textDecrypt.Text = "";
			// 
			// checkSmooth
			// 
			this.checkSmooth.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.checkSmooth.Location = new System.Drawing.Point(8, 233);
			this.checkSmooth.Name = "checkSmooth";
			this.checkSmooth.Size = new System.Drawing.Size(112, 24);
			this.checkSmooth.TabIndex = 1;
			this.checkSmooth.Text = "Use smooth lines";
			this.checkSmooth.CheckedChanged += new System.EventHandler(this.checkSmooth_CheckedChanged);
			// 
			// label4
			// 
			this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(134, 238);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(119, 16);
			this.label4.TabIndex = 2;
			this.label4.Text = "Encryption passphrase";
			// 
			// textPassphrase
			// 
			this.textPassphrase.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textPassphrase.Location = new System.Drawing.Point(248, 236);
			this.textPassphrase.Name = "textPassphrase";
			this.textPassphrase.Size = new System.Drawing.Size(120, 20);
			this.textPassphrase.TabIndex = 3;
			this.textPassphrase.Text = "";
			this.textPassphrase.TextChanged += new System.EventHandler(this.textPassphrase_TextChanged);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(376, 282);
			this.Controls.Add(this.textPassphrase);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.checkSmooth);
			this.Controls.Add(this.tabControl);
			this.Controls.Add(this.statusBar);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "MainForm";
			this.Text = "Desktop Signature";
			this.Resize += new System.EventHandler(this.MainForm_Resize);
			this.Load += new System.EventHandler(this.MainForm_Load);
			((System.ComponentModel.ISupportInitialize)(this.panelMain)).EndInit();
			this.tabControl.ResumeLayout(false);
			this.tabSignature.ResumeLayout(false);
			this.tabPoints.ResumeLayout(false);
			this.tabEncrypt.ResumeLayout(false);
			this.tabDecrypt.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}


		//
		// events
		//

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			// init settings from config file
			textPassphrase.Text = Global.Settings.GetString(SettingKeys.CryptPassphrase);
			checkSmooth.Checked = Global.Settings.GetBool(SettingKeys.SmoothLines);

			// hookup callbacks
			_processCommand = new ProcessSocketCommandHandler(ProcessSocketCommand);
			_server.Notify += new ServerSocket.NotifyEventHandler(NotifyCallback);
			
			// listen for client socket
			_server.Start(Global.Settings.GetInt(SettingKeys.PortNumber));		
		}	
		
		private void pictSignature_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			// draw the signature segments
			DrawSignature(e.Graphics);
		}
		
		private void MainForm_Resize(object sender, System.EventArgs e)
		{
			// redraw the signature when window is resized
			pictSignature.Invalidate();
		}

		private void checkSmooth_CheckedChanged(object sender, System.EventArgs e)
		{
			// save setting and update the signature view
			Global.Settings.SetValue(SettingKeys.SmoothLines, checkSmooth.Checked);
			pictSignature.Invalidate();
		}

		private void textPassphrase_TextChanged(object sender, System.EventArgs e)
		{
			// save setting and update the views
			Global.Settings.SetValue(SettingKeys.CryptPassphrase, textPassphrase.Text);
			UpdateViews();
		}

		/// <summary>
		/// Called from socket object when a network event occurs.
		/// </summary>
		private void NotifyCallback(NotifyCommand command, object data)
		{
			// need to call Invoke before can update UI elements
			object[] args = {command, data};
			Invoke(_processCommand, args);
		}


		// 
		// helper methods
		//

		/// <summary>
		/// Received a socket notification.
		/// </summary>
		private void ProcessSocketCommand(NotifyCommand command, object data)
		{
			// holds the status message for the command
			string status = "";
			switch (command)
			{
				case NotifyCommand.Listen:
					status = string.Format("Listening for client on {0} ...", (string)data);
					break;
					
				case NotifyCommand.Connected:
					status = "Connected to client " + (string)data;
					break;
					
				case NotifyCommand.Disconnected:
					status  = "Disconnected from client";
					break;
					
				case NotifyCommand.ReceivedData:
					status = "Received signature at " + 
						DateTime.Now.ToLongTimeString();

					// store the encrypted data
					_encryptData = (byte[])data;
					UpdateViews();
					break;
			}
			
			// update status
			statusBar.Panels[0].Text = status;
		}
		
		/// <summary>
		/// Update different views of signature data.
		/// </summary>
		private void UpdateViews()
		{
			// return right away if don't have any data
			if (_encryptData == null)
				return;
				
			try
			{
				// update the encrypted view
				textEncrypt.Text = string.Format(
					"Stream length : {0} bytes\r\n\r\n{1}",
					_encryptData.Length, 
					BitConverter.ToString(_encryptData).Replace("-", " "));

				// decrypt the signature data
				byte[] data = Crypto.Decrypt(textPassphrase.Text, _encryptData);
				
				// update the decrypted view
				textDecrypt.Text = string.Format(
					"Stream length : {0} bytes\r\n\r\n{1}",
					data.Length, 
					BitConverter.ToString(data).Replace("-", " "));
		
				// update the signature view
				_signature = new SignatureData(data);
				pictSignature.Invalidate();
					
				// update the segments view
				StringBuilder sb = new StringBuilder();
				sb.AppendFormat("Number of segments : {0}", _signature.Lines.Count);
				
				// loop through each segment and build up string
				for (int i=0; i < _signature.Lines.Count; i++)
				{
					Point[] points = (Point[])_signature.Lines[i];
					sb.AppendFormat("\r\n\r\nSegment {0} ({1} points)\r\n", i+1, points.Length);
					foreach (Point pt in points)
					{
						sb.AppendFormat("({0},{1}) ", pt.X, pt.Y);
					}
				}

				textPoints.Text = sb.ToString();
			}
			catch (Exception ex)
			{
				// something went wrong, this happens if the
				// wrong encryption passphrase is used
				
				string msg = "The signature data could not be decrypted. Additional information:\r\n\r\n" +
					ex.Message;

				// display error message
				textPoints.Text = msg;
				textDecrypt.Text = msg;
				
				// clear signature
				_signature = null;
				pictSignature.Invalidate();
			}
		}
		
		private void DrawSignature(Graphics g)
		{
			// background
			g.Clear(Color.Cornsilk);
			
			// border
			g.DrawRectangle(Pens.Gray, 0, 0, pictSignature.Width-1, pictSignature.Height-1);

			// return if don't have a signature
			if (_signature == null || _signature.Width == 0 || _signature.Height == 0)
				return;

			// setup drawing surface
			g.SmoothingMode = SmoothingMode.AntiAlias;
			
			// scale the signature
			Matrix matrix = new Matrix();
			matrix.Scale(
				(float)pictSignature.Width / (float)_signature.Width,
				(float)pictSignature.Height / (float)_signature.Height);
			g.Transform = matrix;

			// draw each line segment
			foreach (Point[] line in _signature.Lines)
			{
				if (line != null && line.Length > 0)
				{
					// draw lines or curves
					if (checkSmooth.Checked)
						g.DrawCurve(Pens.Firebrick, line, 0.5F);
					else
						g.DrawLines(Pens.Firebrick, line);	
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -