#include // winmm.lib をリンクする #pragma comment(lib,"winmm") #define MIDIMSG(status,channel,data1,data2) ( (DWORD)((status<<4) | channel | (data1<<8) | (data2<<16)) ) LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { static HMIDIOUT hMidiOut; static BYTE note=0x3C,velocity=0x40; static BYTE program=0; HDC hdc; PAINTSTRUCT ps; char str[64]; switch(uMsg){ case WM_CREATE: midiOutOpen(&hMidiOut,MIDIMAPPER,NULL,0,CALLBACK_NULL); return 0; case WM_LBUTTONDOWN: //鍵盤を押す midiOutShortMsg(hMidiOut,MIDIMSG(0x9,0x0,note,velocity)); return 0; case WM_RBUTTONDOWN: //鍵盤を離す midiOutShortMsg(hMidiOut,MIDIMSG(0x9,0x0,note,0)); return 0; case WM_KEYDOWN: switch(wParam){ case 'A': //音色変更 if(program>0) program--; midiOutShortMsg(hMidiOut,MIDIMSG(0xC,0,program,0)); break; case 'F': //音色変更 if(program<0x7F) program++; midiOutShortMsg(hMidiOut,MIDIMSG(0xC,0,program,0)); break; case VK_LEFT: //音階変更 if(note>0) note--; break; case VK_RIGHT: //音階変更 if(note<0x7F) note++; break; case VK_DOWN: //打鍵強度変更 if(velocity>0) velocity--; break; case VK_UP: //打鍵強度変更 if(velocity<0x7F) velocity++; break; } InvalidateRect(hWnd,NULL,TRUE); return 0; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); wsprintf(str,"音色 = 0x%02X , 音階 = 0x%02X , 打鍵強度 = 0x%02X",program,note,velocity); TextOut(hdc,0,0,str,lstrlen(str)); EndPaint(hWnd,&ps); return 0; case WM_DESTROY: midiOutReset(hMidiOut); midiOutClose(hMidiOut); PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,uMsg,wParam,lParam); } int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR lpCmdLine,int nCmdShow) { WNDCLASS wc; MSG msg; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = __FILE__; if(!RegisterClass(&wc)) return 0; HWND hWnd=CreateWindow( __FILE__,"MIDIを鳴らす", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); if(hWnd==NULL) return 0; BOOL bRet; while((bRet=GetMessage(&msg,NULL,0,0))!=0){ if(bRet==-1) break; DispatchMessage(&msg); } return (int)msg.wParam; }