OwnerdrawnButton

svn path=/trunk/; revision=5670
This commit is contained in:
Martin Fuchs 2003-08-19 09:36:25 +00:00
parent a355ea3e20
commit 0fd6225404
2 changed files with 184 additions and 207 deletions

View file

@ -467,6 +467,16 @@ Button::Button(HWND parent, LPCTSTR text, int left, int top, int width, int heig
} }
LRESULT OwnerdrawnButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
{
if (message == WM_DISPATCH_DRAWITEM) {
DrawItem((LPDRAWITEMSTRUCT)lparam);
return TRUE;
} else
return super::WndProc(message, wparam, lparam);
}
static RECT s_MyDrawText_Rect = {0, 0, 0, 0}; static RECT s_MyDrawText_Rect = {0, 0, 0, 0};
static BOOL CALLBACK MyDrawText(HDC hdc, LPARAM data, int cnt) static BOOL CALLBACK MyDrawText(HDC hdc, LPARAM data, int cnt)
@ -475,223 +485,167 @@ static BOOL CALLBACK MyDrawText(HDC hdc, LPARAM data, int cnt)
return TRUE; return TRUE;
} }
void OwnerdrawnButton::DrawGrayText(LPDRAWITEMSTRUCT dis, LPRECT pRect, LPCTSTR text, int dt_flags)
LRESULT ColorButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
{ {
if (message == WM_DISPATCH_DRAWITEM) { COLORREF gray = GetSysColor(COLOR_GRAYTEXT);
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED) if (gray) {
style |= DFCS_INACTIVE; TextColor lcColor(dis->hDC, GetSysColor(COLOR_BTNHIGHLIGHT));
RECT shadowRect = {pRect->left+1, pRect->top+1, pRect->right+1, pRect->bottom+1};
DrawText(dis->hDC, text, -1, &shadowRect, dt_flags);
RECT textRect = {dis->rcItem.left+2, dis->rcItem.top+2, dis->rcItem.right-4, dis->rcItem.bottom-4}; BkMode mode(dis->hDC, TRANSPARENT);
SetTextColor(dis->hDC, gray);
DrawText(dis->hDC, text, -1, pRect, dt_flags);
} else {
int old_r = pRect->right;
int old_b = pRect->bottom;
DrawText(dis->hDC, text, -1, pRect, dt_flags|DT_CALCRECT);
int x = pRect->left + (old_r-pRect->right)/2;
int y = pRect->top + (old_b-pRect->bottom)/2;
int w = pRect->right-pRect->left;
int h = pRect->bottom-pRect->top;
s_MyDrawText_Rect.right = w;
s_MyDrawText_Rect.bottom = h;
GrayString(dis->hDC, GetSysColorBrush(COLOR_GRAYTEXT), MyDrawText, (LPARAM)text, -1, x, y, w, h);
}
}
/* not yet used
void ColorButton::DrawItem(LPDRAWITEMSTRUCT dis)
{
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
style |= DFCS_INACTIVE;
RECT textRect = {dis->rcItem.left+2, dis->rcItem.top+2, dis->rcItem.right-4, dis->rcItem.bottom-4};
if (dis->itemState & ODS_SELECTED) {
style |= DFCS_PUSHED;
++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom;
}
DrawFrameControl(dis->hDC, &dis->rcItem, DFC_BUTTON, style);
TCHAR text[BUFFER_LEN];
GetWindowText(_hwnd, text, BUFFER_LEN);
if (dis->itemState & (ODS_DISABLED|ODS_GRAYED))
DrawGrayText(dis, &textRect, text, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
else {
TextColor lcColor(dis->hDC, _textColor);
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
if (dis->itemState & ODS_FOCUS) {
RECT rect = {
dis->rcItem.left+3, dis->rcItem.top+3,
dis->rcItem.right-dis->rcItem.left-4, dis->rcItem.bottom-dis->rcItem.top-4
};
if (dis->itemState & ODS_SELECTED) { if (dis->itemState & ODS_SELECTED) {
style |= DFCS_PUSHED; ++rect.left; ++rect.top;
++textRect.left; ++textRect.top; ++rect.right; ++rect.bottom;
++textRect.right; ++textRect.bottom;
} }
DrawFocusRect(dis->hDC, &rect);
}
}
*/
void PictureButton::DrawItem(LPDRAWITEMSTRUCT dis)
{
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
style |= DFCS_INACTIVE;
POINT iconPos = {dis->rcItem.left+2, (dis->rcItem.top+dis->rcItem.bottom-16)/2};
RECT textRect = {dis->rcItem.left+16+4, dis->rcItem.top+2, dis->rcItem.right-4, dis->rcItem.bottom-4};
if (dis->itemState & ODS_SELECTED) {
style |= DFCS_PUSHED;
++iconPos.x; ++iconPos.y;
++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom;
}
if (_flat) {
if (GetWindowStyle(_hwnd) & BS_FLAT) // Only with BS_FLAT set, there will be drawn a frame without highlight.
DrawEdge(dis->hDC, &dis->rcItem, EDGE_RAISED, BF_RECT|BF_FLAT);
} else
DrawFrameControl(dis->hDC, &dis->rcItem, DFC_BUTTON, style); DrawFrameControl(dis->hDC, &dis->rcItem, DFC_BUTTON, style);
TCHAR text[BUFFER_LEN]; DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, GetSysColorBrush(COLOR_BTNFACE), DI_NORMAL);
GetWindowText(_hwnd, text, BUFFER_LEN);
if (dis->itemState & (ODS_DISABLED|ODS_GRAYED)) { TCHAR text[BUFFER_LEN];
COLORREF gray = GetSysColor(COLOR_GRAYTEXT); GetWindowText(_hwnd, text, BUFFER_LEN);
if (gray) { if (dis->itemState & (ODS_DISABLED|ODS_GRAYED))
{ DrawGrayText(dis, &textRect, text, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
TextColor lcColor(dis->hDC, GetSysColor(COLOR_BTNHIGHLIGHT)); else {
RECT shadowRect = {textRect.left+1, textRect.top+1, textRect.right+1, textRect.bottom+1}; //BkMode mode(dis->hDC, TRANSPARENT);
DrawText(dis->hDC, text, -1, &shadowRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); TextColor lcColor(dis->hDC, GetSysColor(COLOR_BTNTEXT));
} DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
}
BkMode mode(dis->hDC, TRANSPARENT);
TextColor lcColor(dis->hDC, gray);
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
} else {
int old_r = textRect.right;
int old_b = textRect.bottom;
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER|DT_CALCRECT);
int x = textRect.left + (old_r-textRect.right)/2;
int y = textRect.top + (old_b-textRect.bottom)/2;
int w = textRect.right-textRect.left;
int h = textRect.bottom-textRect.top;
s_MyDrawText_Rect.right = w;
s_MyDrawText_Rect.bottom = h;
GrayString(dis->hDC, GetSysColorBrush(COLOR_GRAYTEXT), MyDrawText, (LPARAM)text, -1, x, y, w, h);
}
} else {
TextColor lcColor(dis->hDC, _textColor);
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
if (dis->itemState & ODS_FOCUS) {
RECT rect = {
dis->rcItem.left+3, dis->rcItem.top+3,
dis->rcItem.right-dis->rcItem.left-4, dis->rcItem.bottom-dis->rcItem.top-4
};
if (dis->itemState & ODS_SELECTED) {
++rect.left; ++rect.top;
++rect.right; ++rect.bottom;
}
DrawFocusRect(dis->hDC, &rect);
}
return TRUE;
} else
return super::WndProc(message, wparam, lparam);
}
LRESULT PictureButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
{
if (message == WM_DISPATCH_DRAWITEM) {
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
style |= DFCS_INACTIVE;
POINT iconPos = {dis->rcItem.left+2, (dis->rcItem.top+dis->rcItem.bottom-16)/2};
RECT textRect = {dis->rcItem.left+16+4, dis->rcItem.top+2, dis->rcItem.right-4, dis->rcItem.bottom-4};
if (dis->itemState & ODS_FOCUS) {
RECT rect = {
dis->rcItem.left+3, dis->rcItem.top+3,
dis->rcItem.right-dis->rcItem.left-4, dis->rcItem.bottom-dis->rcItem.top-4
};
if (dis->itemState & ODS_SELECTED) { if (dis->itemState & ODS_SELECTED) {
style |= DFCS_PUSHED; ++rect.left; ++rect.top;
++iconPos.x; ++iconPos.y; ++rect.right; ++rect.bottom;
++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom;
} }
DrawFocusRect(dis->hDC, &rect);
if (_flat) { }
if (GetWindowStyle(_hwnd) & BS_FLAT) // Only with BS_FLAT set, there will be drawn a frame without highlight.
DrawEdge(dis->hDC, &dis->rcItem, EDGE_RAISED, BF_RECT|BF_FLAT);
} else
DrawFrameControl(dis->hDC, &dis->rcItem, DFC_BUTTON, style);
DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, GetSysColorBrush(COLOR_BTNFACE), DI_NORMAL);
TCHAR text[BUFFER_LEN];
GetWindowText(_hwnd, text, BUFFER_LEN);
if (dis->itemState & (ODS_DISABLED|ODS_GRAYED)) {
COLORREF gray = GetSysColor(COLOR_GRAYTEXT);
if (gray) {
{
TextColor lcColor(dis->hDC, GetSysColor(COLOR_BTNHIGHLIGHT));
RECT shadowRect = {textRect.left+1, textRect.top+1, textRect.right+1, textRect.bottom+1};
DrawText(dis->hDC, text, -1, &shadowRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
}
BkMode mode(dis->hDC, TRANSPARENT);
TextColor lcColor(dis->hDC, gray);
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
} else {
int old_r = textRect.right;
int old_b = textRect.bottom;
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/|DT_CALCRECT);
int x = textRect.left + (old_r-textRect.right)/2;
int y = textRect.top + (old_b-textRect.bottom)/2;
int w = textRect.right-textRect.left;
int h = textRect.bottom-textRect.top;
s_MyDrawText_Rect.right = w;
s_MyDrawText_Rect.bottom = h;
GrayString(dis->hDC, GetSysColorBrush(COLOR_GRAYTEXT), MyDrawText, (LPARAM)text, -1, x, y, w, h);
}
} else {
//BkMode mode(dis->hDC, TRANSPARENT);
TextColor lcColor(dis->hDC, GetSysColor(COLOR_BTNTEXT));
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
}
if (dis->itemState & ODS_FOCUS) {
RECT rect = {
dis->rcItem.left+3, dis->rcItem.top+3,
dis->rcItem.right-dis->rcItem.left-4, dis->rcItem.bottom-dis->rcItem.top-4
};
if (dis->itemState & ODS_SELECTED) {
++rect.left; ++rect.top;
++rect.right; ++rect.bottom;
}
DrawFocusRect(dis->hDC, &rect);
}
return TRUE;
} else
return super::WndProc(message, wparam, lparam);
} }
LRESULT StartmenuEntry::WndProc(UINT message, WPARAM wparam, LPARAM lparam) void StartmenuEntry::DrawItem(LPDRAWITEMSTRUCT dis)
{ {
if (message == WM_DISPATCH_DRAWITEM) { UINT style = DFCS_BUTTONPUSH;
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED) if (dis->itemState & ODS_DISABLED)
style |= DFCS_INACTIVE; style |= DFCS_INACTIVE;
POINT iconPos = {dis->rcItem.left+2, (dis->rcItem.top+dis->rcItem.bottom-16)/2}; POINT iconPos = {dis->rcItem.left+2, (dis->rcItem.top+dis->rcItem.bottom-16)/2};
RECT textRect = {dis->rcItem.left+16+4, dis->rcItem.top+2, dis->rcItem.right-4, dis->rcItem.bottom-4}; RECT textRect = {dis->rcItem.left+16+4, dis->rcItem.top+2, dis->rcItem.right-4, dis->rcItem.bottom-4};
if (dis->itemState & ODS_SELECTED) { if (dis->itemState & ODS_SELECTED) {
style |= DFCS_PUSHED; style |= DFCS_PUSHED;
++iconPos.x; ++iconPos.y; ++iconPos.x; ++iconPos.y;
++textRect.left; ++textRect.top; ++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom; ++textRect.right; ++textRect.bottom;
} }
int bk_color = COLOR_BTNFACE; int bk_color = COLOR_BTNFACE;
int text_color = COLOR_BTNTEXT; int text_color = COLOR_BTNTEXT;
if (dis->itemState & ODS_FOCUS) { if (dis->itemState & ODS_FOCUS) {
bk_color = COLOR_HIGHLIGHT; bk_color = COLOR_HIGHLIGHT;
text_color = COLOR_HIGHLIGHTTEXT; text_color = COLOR_HIGHLIGHTTEXT;
} }
HBRUSH bk_brush = GetSysColorBrush(bk_color); HBRUSH bk_brush = GetSysColorBrush(bk_color);
FillRect(dis->hDC, &dis->rcItem, bk_brush); FillRect(dis->hDC, &dis->rcItem, bk_brush);
DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, bk_brush, DI_NORMAL); DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, bk_brush, DI_NORMAL);
TCHAR text[BUFFER_LEN]; TCHAR text[BUFFER_LEN];
GetWindowText(_hwnd, text, BUFFER_LEN); GetWindowText(_hwnd, text, BUFFER_LEN);
if (dis->itemState & (ODS_DISABLED|ODS_GRAYED)) { if (dis->itemState & (ODS_DISABLED|ODS_GRAYED))
COLORREF gray = GetSysColor(COLOR_GRAYTEXT); DrawGrayText(dis, &textRect, text, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
else {
if (gray) { BkMode mode(dis->hDC, TRANSPARENT);
{ TextColor lcColor(dis->hDC, GetSysColor(text_color));
TextColor lcColor(dis->hDC, GetSysColor(COLOR_BTNHIGHLIGHT)); DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER);
RECT shadowRect = {textRect.left+1, textRect.top+1, textRect.right+1, textRect.bottom+1}; }
DrawText(dis->hDC, text, -1, &shadowRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
}
BkMode mode(dis->hDC, TRANSPARENT);
TextColor lcColor(dis->hDC, gray);
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
} else {
int old_r = textRect.right;
int old_b = textRect.bottom;
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/|DT_CALCRECT);
int x = textRect.left + (old_r-textRect.right)/2;
int y = textRect.top + (old_b-textRect.bottom)/2;
int w = textRect.right-textRect.left;
int h = textRect.bottom-textRect.top;
s_MyDrawText_Rect.right = w;
s_MyDrawText_Rect.bottom = h;
GrayString(dis->hDC, GetSysColorBrush(COLOR_GRAYTEXT), MyDrawText, (LPARAM)text, -1, x, y, w, h);
}
} else {
BkMode mode(dis->hDC, TRANSPARENT);
TextColor lcColor(dis->hDC, GetSysColor(text_color));
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
}
return TRUE;
} else
return super::WndProc(message, wparam, lparam);
} }

View file

@ -369,54 +369,77 @@ template<typename BASE> struct OwnerDrawParent : public BASE
/** /**
Subclass button controls to paint colored text labels. Subclass button controls to draw them by using WM_DISPATCH_DRAWITEM
The owning window should use the OwnerDrawParent template to route woner draw messages to the buttons. The owning window should use the OwnerDrawParent template to route owner draw messages to the buttons.
*/ */
struct ColorButton : public SubclassedWindow struct OwnerdrawnButton : public SubclassedWindow
{ {
typedef SubclassedWindow super; typedef SubclassedWindow super;
OwnerdrawnButton(HWND hwnd)
: super(hwnd) {}
protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam);
void DrawGrayText(LPDRAWITEMSTRUCT dis, LPRECT pRect, LPCTSTR text, int dt_flags);
virtual void DrawItem(LPDRAWITEMSTRUCT dis) = 0;
};
/**
Subclass button controls to paint colored text labels.
The owning window should use the OwnerDrawParent template to route owner draw messages to the buttons.
*/
/* not yet used
struct ColorButton : public OwnerdrawnButton
{
typedef OwnerdrawnButton super;
ColorButton(HWND hwnd, COLORREF textColor) ColorButton(HWND hwnd, COLORREF textColor)
: super(hwnd), _textColor(textColor) {} : super(hwnd), _textColor(textColor) {}
protected: protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam); virtual void DrawItem(LPDRAWITEMSTRUCT dis);
COLORREF _textColor; COLORREF _textColor;
}; };
*/
/** /**
Subclass button controls to paint pictures left to the labels. Subclass button controls to paint pictures left to the labels.
The buttons should have set the style bit BS_OWNERDRAW. The buttons should have set the style bit BS_OWNERDRAW.
The owning window should use the OwnerDrawParent template to route woner draw messages to the buttons. The owning window should use the OwnerDrawParent template to route owner draw messages to the buttons.
*/ */
struct PictureButton : public SubclassedWindow struct PictureButton : public OwnerdrawnButton
{ {
typedef SubclassedWindow super; typedef OwnerdrawnButton super;
PictureButton(HWND hwnd, HICON hicon, bool flat=false) PictureButton(HWND hwnd, HICON hicon, bool flat=false)
: super(hwnd), _hicon(hicon), _flat(flat) {} : super(hwnd), _hicon(hicon), _flat(flat) {}
protected: protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam); virtual void DrawItem(LPDRAWITEMSTRUCT dis);
HICON _hicon; HICON _hicon;
bool _flat; bool _flat;
}; };
/// start menu button /**
struct StartmenuEntry : public SubclassedWindow implement start menu button as owner drawn buitton controls
*/
struct StartmenuEntry : public OwnerdrawnButton
{ {
typedef SubclassedWindow super; typedef OwnerdrawnButton super;
StartmenuEntry(HWND hwnd, HICON hicon, bool flat=false) StartmenuEntry(HWND hwnd, HICON hicon)
: super(hwnd), _hicon(hicon), _flat(flat) {} : super(hwnd), _hicon(hicon) {}
protected: protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam); virtual void DrawItem(LPDRAWITEMSTRUCT dis);
HICON _hicon; HICON _hicon;
bool _flat;
}; };