英創(chuàng)嵌入式ARM9主板采用微軟的Windows CE操作系統(tǒng),可以采用eVC或者VS2005進(jìn)行應(yīng)用開發(fā)。C#作為一種簡單易用的編程語言工具,由于其在結(jié)構(gòu)構(gòu)建和API界面上的優(yōu)勢,受到廣大客戶的喜愛,并為很多用戶所選用。為了讓廣大選用C#的用戶能夠方便快捷地完成應(yīng)用開發(fā),英創(chuàng)提供了一些C#的應(yīng)用例程。一些例程我們已經(jīng)提供了一些說明文檔,發(fā)表在英創(chuàng)網(wǎng)站上,它們是:
·基于WINCE平臺C#編程要點(diǎn)之一
·基于WINCE平臺C#編程要點(diǎn)之二
·使用C#進(jìn)行CAN總線編程—基于WINCE平臺C#編程要點(diǎn)之三
·CAN接口COM組件在C#語言中的使用
·SQL CE數(shù)據(jù)庫的C#編程
·C#使用COM組件接口操作精簡ISA總線
·C#使用COM組件接口進(jìn)行串口操作
·C#調(diào)用COM組件的效率分析
英創(chuàng)不停地根據(jù)客戶的需求添加相應(yīng)的應(yīng)用例程,本文將對下面兩個(gè)新的要點(diǎn)進(jìn)行淺析說明(光盤上均有相關(guān)例程):
·使用C#播放MP3或WAV音頻文件
·如何定義和使用鍵盤熱鍵
1、使用C#播放MP3或WAV音頻文件
英創(chuàng)的EM9000和EM9161等嵌入式工控主板進(jìn)行音頻硬件擴(kuò)展后,可以支持音頻的播放,如果客戶要在應(yīng)用產(chǎn)品中播放MP3和WAV等音頻文件,我們的例程采用了著名的第三方音頻動(dòng)態(tài)鏈接庫fmodce.dll,客戶可以按照如下方法進(jìn)行使用:
首先,可以在應(yīng)用工程中創(chuàng)建一個(gè)新的音頻類文件,如sound.cs,在這個(gè)類文件里定義一個(gè)音頻類,對fmodce.dll的音頻函數(shù)進(jìn)行DllImport定義,以便在應(yīng)用程序中可以調(diào)用:
public static IntPtr GetStream(string filename) // 獲得音頻文件的IntPtr
{
byte[] filenamebytes = System.Text.Encoding.Default.GetBytes(filename + null);
GCHandle hfile = GCHandle.Alloc(filenamebytes, GCHandleType.Pinned);
if (Environment.Version.Major == 1) return new IntPtr(hfile.AddrOfPinnedObject().ToInt32() + 4);
else return hfile.AddrOfPinnedObject();
}
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Init’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern bool Init(int mixrate, int maxsoftwarechannels, int flags); // 初始化
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Stream_GetLength’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern int GetLength(IntPtr fstream); // 獲得流媒體的長度
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Stream_GetPosition’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern UInt32 GetPosition(IntPtr fstream); // 獲得流媒體當(dāng)前播放位置
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Stream_Open’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr Open(IntPtr data, int mode, int offset, int length); // 打開音頻文件
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Stream_Play’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern int Play(int channel, IntPtr fstream); // 播放音頻文件
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Stream_SetPosition’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern bool SetPosition(IntPtr fstream, UInt32 position); // 定位音頻文件播放位置
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Stream_Stop’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern bool Stop(IntPtr fstream); // 停止播放
[DllImport(‘fmodce.dll’, EntryPoint = ‘FSOUND_Close’, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
public static extern void Close(); // 關(guān)閉音頻文件
[DllImport(‘coredll.dll’, SetLastError = true)]
public static extern int SetSystemPowerState(string psState, int StateFlags, int Options);
const int POWER_FORCE = 0x1000;
const int POWER_STATE_ON = 0x10000;
有了這些方便易用的音頻函數(shù),而后就可以在應(yīng)用程序里面自由操作音頻文件了。比如,播放一首songmother.mp3歌曲:
string currentSoundTrack = @‘\NandFlash\songmother.mp3’;
Sound.Init(44100, 16, 0); // 初始化為44.1kHz
IntPtr soundStream = Sound.GetStream(currentSoundTrack);
IntPtr soundHandle = Sound.Open(soundStream, 16 | 32 | 256, 0, 0);
Sound.Play(0, soundHandle);
2、如何定義和使用鍵盤熱鍵
在客戶的應(yīng)用開發(fā)中,很多終端設(shè)備是使用小鍵盤相應(yīng)熱鍵來執(zhí)行相應(yīng)的應(yīng)用操作的。WinCE操作系統(tǒng)有很完善的消息傳遞機(jī)制,我們提供了一個(gè)熱鍵例程來說明這個(gè)過程。
首先,可以在應(yīng)用工程中創(chuàng)建一個(gè)新的MessageWindow類文件,如MyMessageWindow.cs,以便監(jiān)視鍵盤操作并作出實(shí)時(shí)響應(yīng):
class MyMessageWindow : MessageWindow
{
private Form1 msgform = null;
// 注意,程序的主窗體名稱是Form1,所以在此定義一個(gè)msgform的Form1以便接收傳遞的消息
// 下面是構(gòu)造函數(shù),注意在Form1.cs是通過
// keyUsage = new MyMessageWindow(this);彼此建立關(guān)聯(lián)的
public MyMessageWindow(Form1 msgform)
{
this.msgform = msgform;
}
protected override void WndProc(ref Message m) // 監(jiān)視Windows消息
{
const int WM_HOTKEY = 0x0312; // 如果m.Msg的值為0x0312那么表示用戶按下了熱鍵
switch (m.Msg)
{
case WM_HOTKEY:
Form1.ProcessHotkey(m); // 按下熱鍵時(shí)調(diào)用Form1主窗體的ProcessHotkey()函數(shù)
break;
}
base.WndProc(ref m); // 將系統(tǒng)消息傳遞自父類的WndProc
}
}
在主窗體需要對Win32 API熱鍵函數(shù)進(jìn)行聲明:
[DllImport(‘coredll.dll’)] // 定義一個(gè)系統(tǒng)范圍的熱鍵
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk);
[DllImport(‘coredll.dll’)] // 在系統(tǒng)中注消熱鍵
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
// 下面的fsModifoers:定義為了產(chǎn)生WM_HOTKEY消息而必須與由nVirtKey參數(shù)定義的鍵一起按下的鍵
public enum KeyModifiers
{
None = 0, // 該鍵不按下
Alt = 1, // 該鍵為ALT鍵
Control = 2, // 該鍵為CTL鍵
Shift = 4, // 該鍵為Shift鍵
Windows = 8 // 該鍵為任意Windows鍵
}
在主窗體函數(shù)中定義熱鍵:
public Form1()
{
InitializeComponent();
keyUsage = new MyMessageWindow(this);
// 通過構(gòu)造函數(shù)中參數(shù)this的傳遞,主窗體被傳入MessageWindow中,主窗體的消息循環(huán)按照新的消息循環(huán)進(jìn)行
RegisterHotKey(keyUsage.Hwnd, 100, 0, Keys.D1);
// 定義熱鍵數(shù)字鍵‘1’,請注意句柄是keyUsage.Hwd(而不是Handle)
RegisterHotKey(keyUsage.Hwnd, 200, 0, Keys.D2); // 定義熱鍵數(shù)字鍵‘2’
RegisterHotKey(keyUsage.Hwnd, 300, 0, Keys.D3); // 定義熱鍵數(shù)字鍵‘3’
RegisterHotKey(keyUsage.Hwnd, 400, 0, Keys.D4); // 定義熱鍵數(shù)字鍵‘4’
}
接收MyMessageWindow傳遞過來的熱鍵消息,并作進(jìn)一步處理的函數(shù):
public static void ProcessHotkey(Message m)
{
IntPtr id = m.WParam; // IntPtr用于表示指針或句柄的平臺特定類型
string sid = id.ToString();
switch (sid)
{
case ‘100’: // 按下了熱鍵‘1’,執(zhí)行action1()函數(shù)
{
action1();
break;
}
case ‘200’: // 按下了熱鍵‘2’,執(zhí)行action2()函數(shù)
{
action2();
break;
}
case ‘300’: // 按下了熱鍵‘3’,執(zhí)行action3()函數(shù)
{
action3();
break;
}
case ‘400’: // 按下了熱鍵‘4’,執(zhí)行action4()函數(shù)
{
action4();
break;
}
}
}
那么接下來,用戶只需要在action函數(shù)里面添加自己需要進(jìn)行的操作即可了。
-
WINDOWS
+關(guān)注
關(guān)注
4文章
3573瀏覽量
89349 -
嵌入式主板
+關(guān)注
關(guān)注
7文章
6086瀏覽量
35636
發(fā)布評論請先 登錄
相關(guān)推薦
英創(chuàng)信息技術(shù)CAN接口COM組件在C#語言領(lǐng)域應(yīng)用
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b>CAN接口COM組件在<b class='flag-5'>C#</b>語言領(lǐng)域應(yīng)用](https://file.elecfans.com/web1/M00/A8/2F/pIYBAF2KEiSAGtOXAAB2m7INGyk455.jpg)
英創(chuàng)信息技術(shù)C#使用COM組件接口進(jìn)行串口操作介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>C#</b>使用COM組件接口進(jìn)行串口操作<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/A7/DC/o4YBAF2KEb6AH_yZAABvkTU5Bvc218.jpg)
英創(chuàng)信息技術(shù)工控主板的復(fù)位管理介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b>工控主板的復(fù)位管理<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/A7/DC/o4YBAF2KEcmAOrggAAA0-GWht_M314.jpg)
英創(chuàng)信息技術(shù)如何實(shí)現(xiàn)WinCE系統(tǒng)中USB設(shè)備自動(dòng)檢測概述
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b>如何實(shí)現(xiàn)<b class='flag-5'>WinCE</b>系統(tǒng)中USB設(shè)備自動(dòng)檢測概述](https://file.elecfans.com/web1/M00/AE/0A/pIYBAF3R99qAdYruAARpDGhFSdI239.png)
英創(chuàng)信息技術(shù)WinCE工控主板的數(shù)據(jù)庫配置簡介
英創(chuàng)信息技術(shù)嵌入式系統(tǒng)設(shè)備驅(qū)動(dòng)接口的C#編程
英創(chuàng)信息技術(shù)WinCE遠(yuǎn)程桌面介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>WinCE</b>遠(yuǎn)程桌面<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/A7/DF/o4YBAF2KErOAAgl0AABLYJXRYm0326.gif)
英創(chuàng)信息技術(shù)C/C#開發(fā)基于WEC7的CAN通訊介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>C</b>/<b class='flag-5'>C#</b>開發(fā)基于WEC7的CAN通訊<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/B3/BA/pIYBAF4hFWGAcnLfAADR9ri3_Es217.png)
英創(chuàng)信息技術(shù)WinCE平臺下通過系統(tǒng)異常信息定位程序bug位置
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>WinCE</b><b class='flag-5'>平臺</b>下通過系統(tǒng)異常<b class='flag-5'>信息</b>定位程序bug位置](https://file.elecfans.com/web1/M00/B3/64/o4YBAF4hJkyAdoDrAAERmYQr9vE028.png)
英創(chuàng)信息技術(shù)主板WinCE應(yīng)用程序的FTP遠(yuǎn)程更新介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b>主板<b class='flag-5'>WinCE</b>應(yīng)用程序的FTP遠(yuǎn)程更新<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/B3/D4/pIYBAF43cJ-ALY7OAAAGn4vrrqA278.png)
英創(chuàng)信息技術(shù)WinCE工控主板開發(fā)常見問題介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>WinCE</b>工控主板開發(fā)常見問題<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/B3/82/o4YBAF446GmAKK1NAADryyAV2PQ066.png)
英創(chuàng)信息技術(shù)WinCE主板與STM32多功能模塊通訊介紹
英創(chuàng)信息技術(shù)WinCE6系統(tǒng)CEDB故障分析介紹
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>WinCE</b>6系統(tǒng)CEDB故障分析<b class='flag-5'>介紹</b>](https://file.elecfans.com/web1/M00/B3/EC/pIYBAF48y6CAAIviAADbYaTig2M926.png)
英創(chuàng)信息技術(shù)WinCE文件系統(tǒng)測試及故障分析簡介
![<b class='flag-5'>英</b><b class='flag-5'>創(chuàng)</b><b class='flag-5'>信息技術(shù)</b><b class='flag-5'>WinCE</b>文件系統(tǒng)測試及故障分析簡介](https://file.elecfans.com/web1/M00/B3/96/o4YBAF481mqASNYGAASSscaFtmc840.png)
評論