Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #pragma comment(lib, "Winmm.lib")
- typedef char MIDIKey;
- #define COLOR_WHITE 0
- #define COLOR_BLACK 1
- #define IS_BLACK(k) (0X54A & (1 << ((k) % 12)))
- #define PIANO_WHITEHEIGHT 290
- #define PIANO_WHITEWIDHT 46
- #define PIANO_BLACKHEIGHT 170
- #define PIANO_BLACKWIDHT 30
- #define MIDIKEY_FIRST 48
- #define MIDIKEY_LAST 71
- HMIDIOUT hMidiOut;
- HBRUSH hbrWhite, hbrBlack;
- RECT keyRects[128];
- void CalcKeyRect(RECT *pRects, MIDIKey mkFirst, MIDIKey mkLast)
- {
- RECT rcWhite, rcBlack;
- MIDIKey key;
- //if (IS_BLACK(mkFirst))
- // mkFirst--;
- //mkFirst = IS_BLACK(mkFirst) ? mkFirst - 1 : mkFirst;
- mkFirst -= !!IS_BLACK(mkFirst);
- mkLast += !!IS_BLACK(mkLast); //Грязный код фу!
- SetRect(&rcWhite, 0, 0, PIANO_WHITEWIDHT, PIANO_WHITEHEIGHT);
- SetRect(&rcBlack, -PIANO_BLACKWIDHT / 2, 0, PIANO_BLACKWIDHT / 2, PIANO_BLACKHEIGHT);
- for (key = mkFirst; key <= mkLast; key++)
- {
- if (IS_BLACK(key))
- {
- CopyRect(&pRects[key], &rcBlack);
- }
- else
- {
- CopyRect(&pRects[key], &rcWhite);
- OffsetRect(&rcWhite, PIANO_WHITEWIDHT, 0);
- OffsetRect(&rcBlack, PIANO_WHITEWIDHT, 0);
- }
- }
- }
- void DrawPiano(HDC hdc, MIDIKey mkFirst, MIDIKey mkLast)
- {
- char color;
- MIDIKey key;
- mkFirst -= !!IS_BLACK(mkFirst);
- mkLast += !!IS_BLACK(mkLast);
- SaveDC(hdc);
- for(color = COLOR_WHITE; color <= COLOR_BLACK; color++)
- {
- if (COLOR_BLACK == color)
- SelectObject(hdc, hbrBlack);
- else
- SelectObject(hdc, hbrWhite);
- for (key = mkFirst; key <= mkLast; ++key)
- if (!IS_BLACK(key) ^ color)
- Rectangle(hdc, keyRects[key].left, keyRects[key].top, keyRects[key].right, keyRects[key].bottom);
- }
- RestoreDC(hdc, -1);
- }
- LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- switch (uMsg)
- {
- case WM_PAINT:
- BeginPaint(hWnd, &ps);
- DrawPiano(ps.hdc, MIDIKEY_FIRST, MIDIKEY_LAST);
- EndPaint(hWnd, &ps);
- break;
- case WM_KEYDOWN:
- midiOutShortMsg(hMidiOut, 0x7F4090); //7f - Сила нажатия, 40 - номер ноты, 90 -
- break;
- case WM_KEYUP:
- midiOutShortMsg(hMidiOut, 0x7F4080); //7f - Сила нажатия, 40 - номер ноты, 90 -
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
- {
- WNDCLASSEX wcex;
- MSG msg;
- memset(&wcex, 0, sizeof(wcex)); //Сразу все поля инициализирует 0
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.lpfnWndProc = WindowProc;
- wcex.hInstance = hInstance;
- wcex.hCursor = LoadCursor(0, IDC_CROSS);
- wcex.hbrBackground = (HBRUSH)(COLOR_HIGHLIGHT + 1);
- wcex.lpszClassName = L"MyCoolWindowClass";
- RegisterClassEx(&wcex);
- CalcKeyRect(keyRects, MIDIKEY_FIRST, MIDIKEY_LAST);
- midiOutOpen(&hMidiOut, MIDI_MAPPER, 0, 0, CALLBACK_NULL);
- //Для работы был добавлен winmm.lib (ппроект->свойства->компоновщик->ввод->winmm.lib;
- hbrBlack = CreateSolidBrush(0x000000);
- hbrWhite = CreateSolidBrush(0xFFFFFF);
- midiOutShortMsg(hMidiOut, 0x7F4090); //7f - Сила нажатия, 40 - номер ноты, 90 -
- midiOutShortMsg(hMidiOut, 0x7F4390);
- midiOutShortMsg(hMidiOut, 0x7F4790);
- CreateWindowEx(0, wcex.lpszClassName, L"ClownPiano!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 300, 0, 0, hInstance, NULL);
- while (GetMessage(&msg, 0, 0, 0))
- {
- DispatchMessage(&msg);
- }
- DeleteObject(hbrBlack);
- DeleteObject(hbrWhite);
- DeleteObject(hMidiOut);
- midiOutClose(hMidiOut);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement