c# DirectX Input 声音 加密算法等编程小例子
获取游戏设备
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DemoGameContorl;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;
using DemoGameContorl.Core;
namespace PlaySound
{
public partial class JoyHookTest : Form
{
JoyHook joyHook;
public JoyHookTest()
{
InitializeComponent();
//MessageBox.Show(sizeof(DemoGameContorl.Core.JoystickButtons).ToString());
joyHook = new JoyHook();
joyHook.JoyHook_Start();
GetJoyDevices();
//string file = "111";
//string key = "333";
//string Keyfile=Encrypt.Encrypt.DecryptString(file,key);
//MessageBox.Show(Keyfile);
//Keyfile=Encrypt.Encrypt.DecryptString(Keyfile,key);
//MessageBox.Show(Keyfile);
}
public void GetDevices()
{
TreeNode allNode = new TreeNode("All");
treeView1.Nodes.Add(allNode);
foreach (DeviceInstance di in Manager.Devices)
{
TreeNode nameNode = new TreeNode(di.InstanceName);
TreeNode t1 = new TreeNode("Attach=" + Manager.GetDeviceAttached(di.ProductGuid));
TreeNode t2 = new TreeNode("Guid=" + di.ProductGuid);
nameNode.Nodes.Add(t1);
nameNode.Nodes.Add(t2);
allNode.Nodes.Add(nameNode);
}
}
public void GetJoyDevices()
{
TreeNode allNode = new TreeNode("All");
treeView1.Nodes.Add(allNode);
foreach (DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl,EnumDevicesFlags.AttachedOnly))
{ TreeNode nameNode = new TreeNode(di.InstanceName);
TreeNode t1 = new TreeNode("Attach=" + Manager.GetDeviceAttached(di.ProductGuid));
TreeNode t2 = new TreeNode("Guid=" + di.ProductGuid);
nameNode.Nodes.Add(t1);
nameNode.Nodes.Add(t2);
allNode.Nodes.Add(nameNode);
}
}
}
}
加密解密算法
using System;
using System.Collections.Generic;
using System.Text;
namespace Encrypt
{
public class Encrypt
{
/// <summary>
/// 加密
/// </summary>
/// <param name="str">待加密的明文字符串</param>
/// <param name="key">密钥</param>
/// <returns>加密后的字符串</returns>
public static string EncryptString(string str, string key)
{
byte[] bStr = (new UnicodeEncoding()).GetBytes(str);
byte[] bKey = (new UnicodeEncoding()).GetBytes(key);
for (int i = 0; i < bStr.Length; i += 2)
{
for (int j = 0; j < bKey.Length; j += 2)
{
bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]);
}
}
return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0');
}
/// <summary>
/// 解密
/// </summary>
/// <param name="str">待解密的密文字符串</param>
/// <param name="key">密钥</param>
/// <returns>解密后的明文</returns>
public static string DecryptString(string str, string key)
{
return EncryptString(str, key);
}
}
}
多种声音播放
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PlaySound
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Label lb = new Label();
lb.Text = "sdsdsdss";
this.Controls.Add(lb);
}
string MyFileName = string.Empty;
ShockwaveFlashObjects.ShockwaveFlashClass axShockwaveFlash1 = new ShockwaveFlashObjects.ShockwaveFlashClass();
public void PlaySound(string FileName)
{//要加载COM组件:Microsoft speech object Library
if (!System.IO.File.Exists(FileName))
{
return;
}
SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
spFs.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string filename = Application.StartupPath + @"/Joy.wav";
PlaySound(filename);
}
private void button2_Click(object sender, EventArgs e)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/1000.wav");
//sndPlayer.PlayLooping();
sndPlayer.PlaySync();
}
public void Flash()
{
}
private void button3_Click(object sender, EventArgs e)
{
//openFileDialog1.Filter= "Flash文件(*.swf)|*.swf|所有文件(*.*)|*.*";
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
//{
// string MyFileName = openFileDialog1.FileName;
// this.axShockwaveFlash1.MovieData = MyFileName;
//}
MessageBox.Show(axWindowsMediaPlayer1.playState.ToString());
openFileDialog1.Filter = "VCD文件(*.dat)|*.dat|Audio文件(*.avi)|*.avi|WAV文件(*.wav)|*.wav|MP3文件(*.mp3)|*.mp3|所有文件 (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
MyFileName = openFileDialog1.FileName;
this.axWindowsMediaPlayer1.URL = MyFileName;
}
}
private void button4_Click(object sender, EventArgs e)
{
//开始播放
axShockwaveFlash1.Rewind();
axShockwaveFlash1.Play();
}
private void button5_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.close();
}
private void button6_Click(object sender, EventArgs e)
{
MessageBox.Show(axWindowsMediaPlayer1.playState.ToString());
this.axWindowsMediaPlayer1.URL = MyFileName;
MessageBox.Show(axWindowsMediaPlayer1.playState.ToString());
}
}
}