Bạn cần phải bọc tất cả các chức năng cần thiết được liệt kê ở http://msdn.microsoft.com/en-us/library/dd757277(VS.85).aspx
Nó không phải quá khó khăn nếu bạn chỉ sử dụng tin nhắn ngắn, mọi thứ trở nên một chút phức tạp hơn nếu bạn muốn làm SysEx hoặc phát trực tuyến.
Tất cả những gì bạn cần cho cổng nhập cơ bản là nhận ID nhập hợp lệ (InputCount -1), chuyển ID hợp lệ để Mở, Bắt đầu nhập, nhận tin nhắn qua đại biểu ... Ngừng nhập và sau đó cuối cùng đóng nó lại. Đây là một ví dụ rất thô sơ về việc làm thế nào điều này có thể đạt được - bạn sẽ cần phải kiểm tra và cẩn thận rằng Cổng không được thu thập trước khi nó đóng và cuộc gọi lại đã kết thúc hoặc bạn sẽ đóng băng hệ thống của mình!
Chúc may mắn!
namespace MIDI
{
public class InputPort
{
private NativeMethods.MidiInProc midiInProc;
private IntPtr handle;
public InputPort()
{
midiInProc = new NativeMethods.MidiInProc(MidiProc);
handle = IntPtr.Zero;
}
public static int InputCount
{
get { return NativeMethods.midiInGetNumDevs(); }
}
public bool Close()
{
bool result = NativeMethods.midiInClose(handle)
== NativeMethods.MMSYSERR_NOERROR;
handle = IntPtr.Zero;
return result;
}
public bool Open(int id)
{
return NativeMethods.midiInOpen(
out handle,
id,
midiInProc,
IntPtr.Zero,
NativeMethods.CALLBACK_FUNCTION)
== NativeMethods.MMSYSERR_NOERROR;
}
public bool Start()
{
return NativeMethods.midiInStart(handle)
== NativeMethods.MMSYSERR_NOERROR;
}
public bool Stop()
{
return NativeMethods.midiInStop(handle)
== NativeMethods.MMSYSERR_NOERROR;
}
private void MidiProc(IntPtr hMidiIn,
int wMsg,
IntPtr dwInstance,
int dwParam1,
int dwParam2)
{
// Receive messages here
}
}
internal static class NativeMethods
{
internal const int MMSYSERR_NOERROR = 0;
internal const int CALLBACK_FUNCTION = 0x00030000;
internal delegate void MidiInProc(
IntPtr hMidiIn,
int wMsg,
IntPtr dwInstance,
int dwParam1,
int dwParam2);
[DllImport("winmm.dll")]
internal static extern int midiInGetNumDevs();
[DllImport("winmm.dll")]
internal static extern int midiInClose(
IntPtr hMidiIn);
[DllImport("winmm.dll")]
internal static extern int midiInOpen(
out IntPtr lphMidiIn,
int uDeviceID,
MidiInProc dwCallback,
IntPtr dwCallbackInstance,
int dwFlags);
[DllImport("winmm.dll")]
internal static extern int midiInStart(
IntPtr hMidiIn);
[DllImport("winmm.dll")]
internal static extern int midiInStop(
IntPtr hMidiIn);
}
}
Nguồn
2010-01-02 21:00:49
Cảm ơn Mark, và vui lòng xem nhận xét của tôi gửi cho Josh. Tôi sẽ hỏi Carl sau khi hội tụ dự án của mình cho WPF. Chắc chắn tôi sẽ có rất nhiều câu hỏi sau đó. – gideonrv