实例介绍
【实例简介】打开game2.exe输入 xxx 即可测试,按键 zs 退出程序
【实例截图】
退出时候输入 zs
【核心代码】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Configuration;
using Microsoft.Win32;
using System.Reflection;
namespace Game2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//String m_strRunPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
public string m_strRunPath = AppDomain.CurrentDomain.BaseDirectory "files\\";
public SerialPort serialPort;//串口对象类
private MediaPlayer m_Media = new MediaPlayer();
private System.Windows.Threading.DispatcherTimer m_Timer;
private bool m_bSerival = false;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string sCOM = System.Configuration.ConfigurationSettings.AppSettings["串口号"];
string bRun = System.Configuration.ConfigurationSettings.AppSettings["AutoRun"];
if (bRun == "0")
{
RegRun("Game2", false);
}
else if(bRun == "1")
{
RegRun("Game2");
}
btnImage.ImageSource = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory "btn.png", UriKind.Relative));
SetFullScreen();
SetBackgroundImage( AppDomain.CurrentDomain.BaseDirectory "back.jpg");
if (!InitCOM(sCOM))
{
MessageBox.Show("串口异常,当前配置串口" sCOM "请检查配置和设备连接。", "警告");
return;
}
else
{
m_bSerival = true;
m_Timer = new System.Windows.Threading.DispatcherTimer();
m_Timer.Interval = TimeSpan.FromSeconds(1);
m_Timer.Tick = dtimer_Tick;
//m_Timer.Start();
}
TextBox.Focus();
}
void dtimer_Tick(object sender, EventArgs e)
{
SendCommand("0xaa");
}
private void SetFullScreen()
{
this.Left = 0.0;
this.Top = 0.0;
this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
//this.Topmost = true;
}
private void SetBackgroundImage(string path)
{
try
{
ImageBrush b = new ImageBrush();
b.ImageSource = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
this.Background = b;
}
catch { };
}
private void BtnSelect_Click(object sender, RoutedEventArgs e)
{
string strText = TextBox.Text;
if (strText == "")
{
return;
}
string[] strFiles = System.IO.Directory.GetFileSystemEntries(m_strRunPath);
if (strFiles.Length > 0)
{
for (int i = 0; i < strFiles.Length; i )
{
string strTemp = strFiles[i];
string str1 = strTemp.Substring(0, strTemp.Length - 4);
string str2 = str1.Remove(0, m_strRunPath.Length);
if (strText == str2)
{
SendCommand("0xaa");
if (System.IO.File.Exists(strTemp))
{
//转换成小写一并处理
string strFileStyle = strTemp.Substring(strTemp.Length - 3).ToString().ToLower();
//如果mp3文件则直接播放音频不弹出相应窗口
if (strFileStyle == "mp3")
{
m_Media.Open(new Uri(strTemp, UriKind.Relative));
m_Media.Play();
return;
}
}
WindowPop pop = new WindowPop();
pop.m_FilePath = strTemp;
pop.ShowDialog();
return;
}
}
}
else
{
MessageBox.Show("files中没有找到任何文件。","警告");
return;
}
m_Media.Open(new Uri(AppDomain.CurrentDomain.BaseDirectory "xxx.mp3", UriKind.Relative));
m_Media.Play();
MessageBox.Show("非法输入!", "警告");
m_Media.Stop();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
WindowInput WinInput = new WindowInput();
WinInput.ShowDialog();
}
}
/// 串口接收通信配置方法
/// <param name="PortName">端口名称</param>
public bool InitCOM(string PortName)
{
serialPort = new SerialPort(PortName, 9600, Parity.None, 8, StopBits.One);
serialPort.DataReceived = new SerialDataReceivedEventHandler(serialPort_DataReceived);//DataReceived事件委托
serialPort.ReceivedBytesThreshold = 1;
serialPort.RtsEnable = true;
return OpenPort();//串口打开
}
/// 数据接收事件
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[serialPort.ReadBufferSize];
serialPort.Read(readBuffer, 0, readBuffer.Length);
string str = System.Text.Encoding.Default.GetString(readBuffer);
MessageBox.Show(str);
}
//打开串口的方法
public bool OpenPort()
{
try//这里写成异常处理的形式以免串口打不开程序崩溃
{
serialPort.Open();
}
catch { }
if (serialPort.IsOpen)
{
return true;
}
else
{
return false;
}
}
//向串口发送数据
public void SendCommand(string CommandString)
{
try
{
byte[] WriteBuffer1 = Encoding.ASCII.GetBytes(CommandString);
byte[] WriteBuffer = new byte[] { 0xaa };
serialPort.Write(WriteBuffer, 0, WriteBuffer.Length);
}
catch { }
}
private bool AltDown = false;
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
{
AltDown = true;
}
else if (e.SystemKey == Key.F4 && AltDown)
{
e.Handled = true;
}
if (e.Key == Key.R && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
{
//MessageBox.Show("CTRL SHIFT TAB");
if (m_bSerival)
{
SendCommand("0xaa");
}
else
{
MessageBox.Show("复位失败,请检查串口连接和通讯。");
}
}
}
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
{
AltDown = false;
}
}
void RegRun(string appName, bool f = true)
{
RegistryKey HKCU = Registry.CurrentUser;
RegistryKey Run = HKCU.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
bool b = false;
foreach (string i in Run.GetValueNames())
{
if (i == appName)
{
b = true;
break;
}
}
try
{
if (f)
{
Run.SetValue(appName, Assembly.GetExecutingAssembly().Location);
}
else
{
Run.DeleteValue(appName);
}
}
catch
{ }
HKCU.Close();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论