template< class Func >
void draw_with_brush_and_pen(CDC& dc, const COLORREF& col, const COLORREF& col2, const int line_width, Func draw_func)
{
CBrush myBrush(col);
CBrush* oldBrush = dc.SelectObject(&myBrush);
CPen myPen(PS_SOLID, line_width, col2);
CPen* oldPen = dc.SelectObject(&myPen);
draw_func(dc);
dc.SelectObject(oldBrush);
dc.SelectObject(oldPen);
return;
}
void enmergin(CRect& rect, const int mergin)
{
rect.top -= mergin;
rect.bottom += mergin;
rect.left -= mergin;
rect.right += mergin;
return;
}
template< class Func >
void draw_with_font(CDC& dc, const char* face, Func draw_func)
{
HFONT hFont = CreateFont(
36,
0,
0,
0,
FW_REGULAR,
FALSE,
FALSE,
FALSE,
SHIFTJIS_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
FIXED_PITCH | FF_MODERN,
face);
HFONT hFontOld = (HFONT)SelectObject(dc, hFont);
draw_func(dc);
SelectObject(dc, hFontOld);
DeleteObject(hFont);
return;
}
void MyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
Graphics grp(dc);
dc.SetBkMode(TRANSPARENT);
CRect rt;
rt = lpDrawItemStruct->rcItem;
{
CRgn rgn;
rgn.CreateRoundRectRgn(0, 0, rt.Width(), rt.Height(), 18, 18);
SetWindowRgn(rgn, TRUE);
}
UINT state = lpDrawItemStruct->itemState;
if ((state & ODS_SELECTED))
{
auto rt2 = rt;
enmergin(rt2, -4);
auto func = [&rt2](CDC& dc) { dc.RoundRect(rt2, CPoint(20, 20)); };
draw_with_brush_and_pen(dc, RGB(88, 128, 88), RGB(0,255,0), 3, func);
dc.SetTextColor(RGB(0, 255, 20));
}
else if ((state & ODS_FOCUS))
{
auto rt2 = rt;
enmergin(rt2, -4);
auto func = [&rt2](CDC& dc) { dc.RoundRect(rt2, CPoint(20, 20)); };
draw_with_brush_and_pen(dc, RGB(180, 190, 180), RGB(0, 188, 0), 3, func);
dc.SetTextColor(RGB(0, 122, 20));
}
else
{
auto rt2 = rt;
enmergin(rt2, -3);
auto func = [&rt2](CDC& dc) { dc.RoundRect(rt2, CPoint(20, 20)); };
if (_yes_hover)
{
draw_with_brush_and_pen(dc, RGB(255, 180, 250), RGB(180, 23, 255), 1, func);
dc.SetTextColor(RGB(255, 20, 220));
}
else
{
draw_with_brush_and_pen(dc, RGB(180, 180, 180), RGB(0, 23, 255), 1, func);
dc.SetTextColor(RGB(0, 20, 120));
}
}
CString strTemp;
GetWindowText(strTemp);
if ((state & ODS_FOCUS))
{
enmergin(rt, -12);
dc.DrawFocusRect(rt);
}
else
{
}
{
auto func = [&rt, &strTemp](CDC& dc) {
dc.DrawText(strTemp, rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
};
draw_with_font(dc, "Meiryo UI", func);
}
dc.Detach();
return;
}
BOOL MyButton::OnEraseBkgnd(CDC* pDC)
{
TODO
return true;
}
void MyButton::OnNcMouseMove(UINT nHitTest, CPoint point)
{
TODO
_yes_hover = true;
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hWnd;
tme.dwHoverTime = HOVER_DEFAULT;
::TrackMouseEvent(&tme);
CButton::OnNcMouseMove(nHitTest, point);
}
void MyButton::OnNcMouseLeave()
{
TODO
_yes_hover = false;
CButton::OnNcMouseLeave();
}
void MyButton::OnMouseHover(UINT nFlags, CPoint point)
{
TODO
_yes_hover = true;
CButton::OnMouseHover(nFlags, point);
this->Invalidate();
}
void MyButton::OnMouseLeave()
{
TODO
_yes_hover = false;
Invalidate();
CButton::OnMouseLeave();
}
void MyButton::OnMouseMove(UINT nFlags, CPoint point)
{
TODO
static bool done = false;
if (!_yes_hover)
{
_yes_hover= true;
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hWnd;
tme.dwHoverTime = 5;
::TrackMouseEvent(&tme);
}
CButton::OnMouseMove(nFlags, point);
}