Skip to content

Instantly share code, notes, and snippets.

@cfillion
Last active March 18, 2019 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfillion/0d364ba7a8ab90e71c688175ec0026f1 to your computer and use it in GitHub Desktop.
Save cfillion/0d364ba7a8ab90e71c688175ec0026f1 to your computer and use it in GitHub Desktop.
A REAPER extension that draws a red square on the arrange view.
#include <memory>
#ifdef _WIN32
# include <windows.h>
#else
# include <swell/swell.h>
# include <wdltypes.h> // GWLP_WNDPROC
#endif
#define REAPERAPI_IMPLEMENT
#include "reaper_plugin_functions.h"
class WindowProcHook {
public:
WindowProcHook(HWND window, WNDPROC proc)
: m_window(window)
{
m_next = reinterpret_cast<WNDPROC>(SetWindowLongPtr(m_window,
GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(proc)));
}
LRESULT next(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) const
{
return m_next(window, msg, wParam, lParam);
}
~WindowProcHook()
{
SetWindowLongPtr(m_window, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(m_next));
}
private:
HWND m_window;
WNDPROC m_next;
};
class Brush {
public:
Brush(COLORREF color) : m_brush(CreateSolidBrush(color)) {}
~Brush() { DeleteObject(m_brush); }
operator HBRUSH() const { return m_brush; }
private:
HBRUSH m_brush;
};
static std::unique_ptr<WindowProcHook> g_mainHook;
static void paint(HWND window)
{
static RECT rect{42, 42, 84, 84};
static Brush brush(RGB(255, 0, 0));
HDC hdc = GetDC(window);
FillRect(hdc, &rect, brush);
ReleaseDC(window, hdc);
}
static LRESULT CALLBACK paintProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = g_mainHook->next(window, msg, wParam, lParam);
switch(msg) {
case WM_PAINT:
paint(window);
break;
}
return ret;
}
extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec)
{
if(!rec) {
g_mainHook.release();
return 0;
}
if(rec->caller_version != REAPER_PLUGIN_VERSION)
return 0;
GetMainHwnd = reinterpret_cast<decltype(GetMainHwnd)>(rec->GetFunc("GetMainHwnd"));
if(!GetMainHwnd)
return 0;
HWND arrange = GetDlgItem(GetMainHwnd(), 1000);
g_mainHook = std::make_unique<WindowProcHook>(arrange, &paintProc);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment