📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
namespace MySquirtCF
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.TextBox textFileName;
private System.Windows.Forms.ListBox listOut;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
private bool m_fPocketPC;
private bool isRunning;
private Thread thServ;
private MenuItem menuFile;
private MenuItem menuExit;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// See if we're running on a Pocket PC
if (MyMsgWindow.IsPocketPC())
m_fPocketPC = true;
if (m_fPocketPC)
{
// On a Pocket PC, adjust control position since
// Commandbar isn't used.
this.label1.Location = new Point (4, 5);
this.textFileName.Location = new Point (46, 4);
this.btnBrowse.Location = new Point (4, 30);
this.btnGo.Location = new Point (100, 30);
this.listOut.Location = new Point (4, 54);
// Set size of listbox depending on SIP
int height = this.inputPanel1.VisibleDesktop.Height -
this.listOut.Top;
if (!this.inputPanel1.Enabled)
height -= 26;
this.listOut.Size = new Size (this.listOut.Width, height);
// Place OK button on Nav Bar to close application
this.ControlBox = true;
this.MinimizeBox = false;
}
else
{
// On an embedded device, add a File | Exit menu item
menuFile = new MenuItem();
menuExit = new MenuItem();
menuExit.Text = "Exit";
menuExit.Click += new EventHandler(menuExit_Click);
menuFile.Text = "File";
menuFile.MenuItems.Add (menuExit);
mainMenu1.MenuItems.Add (menuFile);
}
int widthC = this.ClientRectangle.Width;
int heightC = this.ClientRectangle.Height;
this.Text = "IrSquirt";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.btnGo = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textFileName = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.listOut = new System.Windows.Forms.ListBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(160, 58);
this.btnGo.Size = new System.Drawing.Size(80, 20);
this.btnGo.Text = "Send";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 33);
this.label1.Size = new System.Drawing.Size(32, 20);
this.label1.Text = "File:";
//
// textFileName
//
this.textFileName.Location = new System.Drawing.Point(48, 32);
this.textFileName.Size = new System.Drawing.Size(192, 22);
this.textFileName.Text = "";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(8, 58);
this.btnBrowse.Size = new System.Drawing.Size(80, 20);
this.btnBrowse.Text = "Browse";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// listOut
//
this.listOut.Location = new System.Drawing.Point(8, 82);
this.listOut.Size = new System.Drawing.Size(232, 198);
//
// inputPanel1
//
this.inputPanel1.EnabledChanged += new System.EventHandler(this.inputPanel1_EnabledChanged);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(250, 292);
this.Controls.Add(this.listOut);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.textFileName);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnGo);
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
isRunning = true;
this.thServ = new Thread (new ThreadStart(this.SrvRoutine));
this.thServ.Start();
}
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
isRunning = false;
Thread.Sleep(550);
}
private void menuExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr != DialogResult.OK)
return;
this.textFileName.Text = openFileDialog1.FileName;
}
private void inputPanel1_EnabledChanged(object sender,
System.EventArgs e)
{
// Adjust the listbox to avoid being covered by the SIP.
if (m_fPocketPC)
{
int height = this.inputPanel1.VisibleDesktop.Height -
this.listOut.Top;
if (!this.inputPanel1.Enabled)
height -= 26;
this.listOut.Size = new Size (this.listOut.Width,
height);
}
}
private void btnGo_Click(object sender, System.EventArgs e)
{
string strFileName = this.textFileName.Text;
if (strFileName.Length > 0)
SendFileToIR (strFileName);
}
private void StringOut (string str)
{
this.listOut.Items.Add (str);
return;
}
/// <summary>
/// Sends a file to the other device
/// </summary>
/// <param name="strFileName"></param>
private void SendFileToIR (string strFileName)
{
Stream s;
FileStream fs;
int rc;
IrDAClient irClient;
try
{
fs = new FileStream (strFileName, FileMode.Open,
FileAccess.Read);
}
catch (IOException ex)
{
StringOut (string.Format("Error opening file {0}",
ex.Message));
return;
}
StringOut ("File opened");
irClient = new IrDAClient ();
//
// Connect to service
//
StringOut ("Waiting at connect");
try
{
irClient.Connect("MySqurt");
}
catch (SocketException ex)
{
StringOut (string.Format ("Sock connect exception {0}",
ex.ErrorCode));
fs.Close();
return;
}
StringOut ("Connected");
//
// Start transfer
//
try
{
s = irClient.GetStream();
}
catch (SocketException ex)
{
StringOut (string.Format ("Sock GetStream exception {0}",
ex.ErrorCode));
fs.Close();
irClient.Close();
return;
}
// Parse path to get only the file name and extension
char[] parse = "\\".ToCharArray();
string[] fnParts = strFileName.Split (parse);
string strNameOnly = fnParts[fnParts.Length-1];
int nLen = strNameOnly.Length;
// Allocate transfer buffer
byte[] buff = new byte[4096];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -