From 3f2da7cb6385e01ab5eb52deff24d1bd180a4450 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 10 Sep 2026 11:08:42 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9C=E3=82=BF=E3=83=B3=E3=81=AE=E7=8A=B6?= =?UTF-8?q?=E6=85=8B=E3=81=8C=E5=A4=89=E3=82=8F=E3=82=89=E3=81=AA=E3=81=8B?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E3=81=AE=E3=82=92=E7=9B=B4=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FlatButton.cpp | 70 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/src/FlatButton.cpp b/src/FlatButton.cpp index f6320f9..1d7c5b8 100644 --- a/src/FlatButton.cpp +++ b/src/FlatButton.cpp @@ -1,5 +1,7 @@ #include "FlatButton.h" +#include + #include #include "HostContext.h" @@ -8,6 +10,18 @@ namespace pe { namespace { +const wchar_t* kProp = L"ProxyEditFlatButton"; +const UINT_PTR kSubclassId = 0x50450004; + +struct State { + bool primary = false; + bool hot = false; +}; + +State* StateOf(HWND button) { + return (State*)GetPropW(button, kProp); +} + int Scaled(HWND window, int value) { UINT dpi = GetDpiForWindow(window); if (dpi == 0) dpi = 96; @@ -26,6 +40,36 @@ void FrameRectangle(HDC dc, const RECT& area, COLORREF color) { DeleteObject(brush); } +LRESULT CALLBACK ButtonProc(HWND window, UINT message, WPARAM first, LPARAM second, UINT_PTR, + DWORD_PTR) { + State* state = StateOf(window); + switch (message) { + case WM_MOUSEMOVE: + if (state && !state->hot) { + state->hot = true; + TRACKMOUSEEVENT track{sizeof(track), TME_LEAVE, window, 0}; + TrackMouseEvent(&track); + InvalidateRect(window, nullptr, FALSE); + } + break; + case WM_MOUSELEAVE: + if (state && state->hot) { + state->hot = false; + InvalidateRect(window, nullptr, FALSE); + } + break; + case WM_NCDESTROY: { + State* dying = (State*)RemovePropW(window, kProp); + RemoveWindowSubclass(window, ButtonProc, kSubclassId); + delete dying; + break; + } + default: + break; + } + return DefSubclassProc(window, message, first, second); +} + } int FlatButtonWidth(HWND parent, HFONT font, const wchar_t* text) { @@ -40,11 +84,15 @@ int FlatButtonWidth(HWND parent, HFONT font, const wchar_t* text) { } HWND MakeFlatButton(HWND parent, const wchar_t* text, int id, HFONT font, bool defaulted) { - DWORD style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_OWNERDRAW; - if (defaulted) style |= BS_DEFPUSHBUTTON; - HWND button = CreateWindowExW(0, L"BUTTON", text, style, 0, 0, 10, 10, parent, - (HMENU)(INT_PTR)id, ModuleInstance(), nullptr); - if (button && font) SendMessageW(button, WM_SETFONT, (WPARAM)font, TRUE); + HWND button = CreateWindowExW(0, L"BUTTON", text, + WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_OWNERDRAW, 0, 0, 10, 10, + parent, (HMENU)(INT_PTR)id, ModuleInstance(), nullptr); + if (!button) return nullptr; + if (font) SendMessageW(button, WM_SETFONT, (WPARAM)font, TRUE); + State* state = new State(); + state->primary = defaulted; + SetPropW(button, kProp, (HANDLE)state); + SetWindowSubclass(button, ButtonProc, kSubclassId, 0); return button; } @@ -52,14 +100,14 @@ void DrawFlatButton(const DRAWITEMSTRUCT* item) { if (!item || item->CtlType != ODT_BUTTON) return; HDC dc = item->hDC; const RECT area = item->rcItem; + const State* state = StateOf(item->hwndItem); const bool pressed = (item->itemState & ODS_SELECTED) != 0; - const bool hot = (item->itemState & ODS_HOTLIGHT) != 0; const bool disabled = (item->itemState & ODS_DISABLED) != 0; - const LONG_PTR style = GetWindowLongPtrW(item->hwndItem, GWL_STYLE); - const bool primary = (style & BS_DEFPUSHBUTTON) == BS_DEFPUSHBUTTON; - const bool accent = primary || (item->itemState & (ODS_DEFAULT | ODS_FOCUS)) != 0; + const bool focused = (item->itemState & ODS_FOCUS) != 0; + const bool hot = state && state->hot; + const bool primary = state && state->primary; - COLORREF body = accent && !disabled ? RGB(0xE8, 0xF2, 0xFC) : GetSysColor(COLOR_BTNFACE); + COLORREF body = primary && !disabled ? RGB(0xE8, 0xF2, 0xFC) : GetSysColor(COLOR_BTNFACE); if (disabled) { body = RGB(0xF5, 0xF5, 0xF5); } else if (pressed) { @@ -74,7 +122,7 @@ void DrawFlatButton(const DRAWITEMSTRUCT* item) { border = RGB(0xD0, 0xD0, 0xD0); } else if (pressed) { border = RGB(0x00, 0x5A, 0x9E); - } else if (hot || accent) { + } else if (hot || primary || focused) { border = RGB(0x00, 0x78, 0xD4); } FrameRectangle(dc, area, border);