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

📄 form1.cs

📁 蓝牙遥控器的代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Net.Sockets;
using OpenNETCF.Net;
using OpenNETCF.Net.Sockets;
using OpenNETCF.Net.Bluetooth;

namespace OpenNETCF
{
	/// <summary>
	/// used to send keys
	/// </summary>
	public class RemoteControlForm : System.Windows.Forms.Form
	{
		
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuClose;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.ComboBox cmbDevices;
		private System.Windows.Forms.MenuItem mnuSearch;
		private System.Windows.Forms.MenuItem mnuConnect;
		
		private Guid ServiceGuid = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-98BCD91BF93B}");
        

		public RemoteControlForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
		/// <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.mnuClose = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.mnuSearch = new System.Windows.Forms.MenuItem();
			this.mnuConnect = new System.Windows.Forms.MenuItem();
			this.cmbDevices = new System.Windows.Forms.ComboBox();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.mnuClose);
			this.mainMenu1.MenuItems.Add(this.menuItem2);
			// 
			// mnuClose
			// 
			this.mnuClose.Text = "Close";
			this.mnuClose.Click += new System.EventHandler(this.mnuClose_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.MenuItems.Add(this.mnuSearch);
			this.menuItem2.MenuItems.Add(this.mnuConnect);
			this.menuItem2.Text = "Menu";
			// 
			// mnuSearch
			// 
			this.mnuSearch.Text = "Search";
			this.mnuSearch.Click += new System.EventHandler(this.mnuSearch_Click);
			// 
			// mnuConnect
			// 
			this.mnuConnect.Text = "Connect";
			this.mnuConnect.Click += new System.EventHandler(this.mnuConnect_Click);
			// 
			// cmbDevices
			// 
			this.cmbDevices.Location = new System.Drawing.Point(8, 8);
			this.cmbDevices.Size = new System.Drawing.Size(160, 26);
			this.cmbDevices.Visible = false;
			// 
			// RemoteControlForm
			// 
			this.Controls.Add(this.cmbDevices);
			this.Menu = this.mainMenu1;
			this.Text = "Remote";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.RemoteControlForm_KeyUp);

		}
		#endregion

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

		private void Form1_Load(object sender, System.EventArgs e)
		{		
			//turn on bt radio
			BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
			bc = new BluetoothClient();
		}


		private void mnuSearch_Click(object sender, System.EventArgs e)
		{
			//this will take a while...
			Cursor.Current = Cursors.WaitCursor;
			BluetoothDeviceInfo[] bdi = bc.DiscoverDevices(12);
			//bind the combo
			cmbDevices.DataSource = bdi;
			cmbDevices.DisplayMember = "DeviceName";
			cmbDevices.ValueMember = "DeviceID";
			cmbDevices.Visible = true;
			cmbDevices.Focus();
			Cursor.Current = Cursors.Default;

			if(bdi.Length > 0)
			{
				mnuConnect.Enabled = true;
			}
		}

		private void mnuConnect_Click(object sender, System.EventArgs e)
		{
			if(cmbDevices.SelectedValue != null)
			{
				try
				{
					bc.Connect(new BluetoothEndPoint((BluetoothAddress)cmbDevices.SelectedValue, ServiceGuid)); 
					mnuConnect.Enabled = false;
					this.Controls.Remove(cmbDevices);
					this.BackColor = Color.PaleGreen;
					this.Focus();
				}
				catch
				{
					//error connecting
					this.BackColor = Color.Salmon;
				}
			}
		}

		private void mnuClose_Click(object sender, System.EventArgs e)
		{
			bc.Close();
			this.Close();
		}

		private void RemoteControlForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			try
			{
				System.IO.Stream s = bc.GetStream();
				s.Write(BitConverter.GetBytes((short)e.KeyCode), 0, 1);
			}
			catch
			{
			}
		
		}
	}
}

⌨️ 快捷键说明

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