引用
using System.IO;
using System.Reflection;
調用語法
WinCE
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
WIn32
Directory.GetCurrentDirectory();
神風地球喵 發表在 痞客邦 留言(0) 人氣(351)
引用與宣告部分
using System.Runtime.InteropServices; //命名空間引用 DllImport
-----------------------
// XP 上驗證可行
[DllImport("winmm.dll")]
public static extern long waveOutSetVolume(long deviceID, long Volume);
-------------------------
// 使用Wince 時, 需引用 codedll.dll
[DllImport("coredll.dll", EntryPoint = "waveOutSetVolume")]
extern static Int32 waveOutSetVolume(IntPtr hwo, uint dwVolume); //設定音量值
[DllImport("coredll.dll", EntryPoint = "waveOutGetVolume")]
private static extern uint waveOutGetVolume(IntPtr device, ref int volume); //取得音量值
調用方式
//設定音量值
waveOutSetVolume(IntPtr.Zero, 0xFFFF);
//取得音量值
int pdwVolume2;
waveOutGetVolume(IntPtr.Zero, ref pdwVolume2);
MessageBox.Show(pdwVolume2.ToString());
//
waveOutSetVolume(0, 0x0000);
deviceID : Handle to an open waveform-audio output device. This parameter can also be a device identifier. 為0表示預設裝置
Volume : New volume setting. The low-order word contains the left-channel volume setting, and the high-order word contains the right-channel setting. A value of 0xFFFF represents full volume, and a value of 0x0000 is silence.
MSDN:
http://msdn.microsoft.com/en-us/library/ms713762(VS.85).aspx
備註;
//限制音量的取值範圍
if (Value < 0) Value = 0;
if (Value > 0xffff) Value = 0xffff;
System.UInt32 left = (System.UInt32)Value; //左聲道音量
System.UInt32 right = (System.UInt32)Value;//右聲道音量
waveOutSetVolume(0, left << 16 | right); //邏輯左移後合併
1.DeviceID 可以選擇聲音裝置
2.引用codedll.dll可以支援WinCE
神風地球喵 發表在 痞客邦 留言(0) 人氣(2,100)
API定義
using System.Runtime.InteropServices; //引用 DllImport
//
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
const uint WM_APPCOMMAND = 0x319;
const uint APPCOMMAND_VOLUME_UP = 0x0a;
const uint APPCOMMAND_VOLUME_DOWN = 0x09;
const uint APPCOMMAND_VOLUME_MUTE = 0x08;
增加音量
SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
減少音量
SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
靜音與恢復
SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);
備註
1.不支援WinCE
神風地球喵 發表在 痞客邦 留言(0) 人氣(591)
一定要加入引用:
using System.Runtime.InteropServices;
DllImport的用法:
DllImport("MyDllImport.dll")]
private static extern int mySum(int a,int b);
----------------------
一個例子:
Beep() 是在 kernel32.lib 中定義的,Beep具有以下原型:
BOOL Beep(DWORD dwFreq, DWORD dwDuration);
用C#編寫後的結果:
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration);
神風地球喵 發表在 痞客邦 留言(1) 人氣(1,173)