Advertisement
Tiranka1861

WINAPI CLOWN_PIANO

Oct 12th, 2023 (edited)
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #pragma comment(lib, "Winmm.lib")
  3.  
  4. typedef char MIDIKey;
  5.  
  6. #define COLOR_WHITE         0
  7. #define COLOR_BLACK         1
  8.  
  9. #define IS_BLACK(k) (0X54A & (1 << ((k) % 12)))
  10.  
  11. #define PIANO_WHITEHEIGHT   290
  12. #define PIANO_WHITEWIDHT    46
  13. #define PIANO_BLACKHEIGHT   170
  14. #define PIANO_BLACKWIDHT    30
  15.  
  16. #define MIDIKEY_FIRST       48
  17. #define MIDIKEY_LAST        71
  18.  
  19. HMIDIOUT    hMidiOut;
  20. HBRUSH      hbrWhite, hbrBlack;
  21. RECT        keyRects[128];
  22.  
  23. void CalcKeyRect(RECT *pRects, MIDIKey mkFirst, MIDIKey mkLast)
  24. {
  25.     RECT rcWhite, rcBlack;
  26.     MIDIKey key;
  27.  
  28.     //if (IS_BLACK(mkFirst))
  29.     //  mkFirst--;
  30.  
  31.     //mkFirst = IS_BLACK(mkFirst) ? mkFirst - 1 : mkFirst;
  32.  
  33.     mkFirst -= !!IS_BLACK(mkFirst);
  34.     mkLast += !!IS_BLACK(mkLast);       //Грязный код фу!
  35.  
  36.     SetRect(&rcWhite, 0, 0, PIANO_WHITEWIDHT, PIANO_WHITEHEIGHT);
  37.     SetRect(&rcBlack, -PIANO_BLACKWIDHT / 2, 0, PIANO_BLACKWIDHT / 2, PIANO_BLACKHEIGHT);
  38.     for (key = mkFirst; key <= mkLast; key++)
  39.     {
  40.         if (IS_BLACK(key))
  41.         {
  42.             CopyRect(&pRects[key], &rcBlack);
  43.         }
  44.         else
  45.         {
  46.             CopyRect(&pRects[key], &rcWhite);
  47.             OffsetRect(&rcWhite, PIANO_WHITEWIDHT, 0);
  48.             OffsetRect(&rcBlack, PIANO_WHITEWIDHT, 0);
  49.         }
  50.  
  51.        
  52.     }
  53. }
  54.  
  55.  
  56. void DrawPiano(HDC hdc, MIDIKey mkFirst, MIDIKey mkLast)
  57. {
  58.     char color;
  59.     MIDIKey key;
  60.  
  61.     mkFirst -= !!IS_BLACK(mkFirst);
  62.     mkLast += !!IS_BLACK(mkLast);
  63.  
  64.     SaveDC(hdc);
  65.  
  66.     for(color = COLOR_WHITE; color <= COLOR_BLACK; color++)
  67.     {
  68.         if (COLOR_BLACK == color)
  69.             SelectObject(hdc, hbrBlack);
  70.         else
  71.             SelectObject(hdc, hbrWhite);
  72.         for (key = mkFirst; key <= mkLast; ++key)
  73.             if (!IS_BLACK(key) ^ color)
  74.                 Rectangle(hdc, keyRects[key].left, keyRects[key].top, keyRects[key].right, keyRects[key].bottom);
  75.     }
  76.     RestoreDC(hdc, -1);
  77. }
  78.  
  79.  
  80. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  81. {
  82.     PAINTSTRUCT ps;
  83.     switch (uMsg)
  84.     {
  85.     case WM_PAINT:
  86.         BeginPaint(hWnd, &ps);
  87.         DrawPiano(ps.hdc, MIDIKEY_FIRST, MIDIKEY_LAST);
  88.         EndPaint(hWnd, &ps);
  89.         break;
  90.     case WM_KEYDOWN:
  91.         midiOutShortMsg(hMidiOut, 0x7F4090); //7f - Сила нажатия, 40 - номер ноты, 90 -  
  92.         break;
  93.     case WM_KEYUP:
  94.         midiOutShortMsg(hMidiOut, 0x7F4080); //7f - Сила нажатия, 40 - номер ноты, 90 -  
  95.         break;
  96.  
  97.     case WM_DESTROY:
  98.         PostQuitMessage(0);
  99.         break;
  100.     }
  101.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  102. }
  103.  
  104. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
  105. {
  106.     WNDCLASSEX wcex;
  107.     MSG msg;
  108.  
  109.     memset(&wcex, 0, sizeof(wcex)); //Сразу все поля инициализирует 0
  110.     wcex.cbSize = sizeof(WNDCLASSEX);
  111.     wcex.lpfnWndProc = WindowProc;
  112.     wcex.hInstance = hInstance;
  113.     wcex.hCursor = LoadCursor(0, IDC_CROSS);
  114.     wcex.hbrBackground = (HBRUSH)(COLOR_HIGHLIGHT + 1);
  115.     wcex.lpszClassName = L"MyCoolWindowClass";
  116.     RegisterClassEx(&wcex);
  117.  
  118.     CalcKeyRect(keyRects, MIDIKEY_FIRST, MIDIKEY_LAST);
  119.    
  120.     midiOutOpen(&hMidiOut, MIDI_MAPPER, 0, 0, CALLBACK_NULL);
  121.     //Для работы был добавлен winmm.lib (ппроект->свойства->компоновщик->ввод->winmm.lib;
  122.  
  123.     hbrBlack = CreateSolidBrush(0x000000);
  124.     hbrWhite = CreateSolidBrush(0xFFFFFF);
  125.  
  126.     midiOutShortMsg(hMidiOut, 0x7F4090); //7f - Сила нажатия, 40 - номер ноты, 90 -  
  127.     midiOutShortMsg(hMidiOut, 0x7F4390);
  128.     midiOutShortMsg(hMidiOut, 0x7F4790);
  129.  
  130.     CreateWindowEx(0, wcex.lpszClassName, L"ClownPiano!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 300, 0, 0, hInstance, NULL);
  131.  
  132.     while (GetMessage(&msg, 0, 0, 0))
  133.     {
  134.         DispatchMessage(&msg);
  135.     }
  136.     DeleteObject(hbrBlack);
  137.     DeleteObject(hbrWhite);
  138.     DeleteObject(hMidiOut);
  139.     midiOutClose(hMidiOut);
  140.     return msg.wParam;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement