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 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;
}
LRESULT ColorButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
void OwnerdrawnButton::DrawGrayText(LPDRAWITEMSTRUCT dis, LPRECT pRect, LPCTSTR text, int dt_flags)
{
if (message == WM_DISPATCH_DRAWITEM) {
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
COLORREF gray = GetSysColor(COLOR_GRAYTEXT);
if (dis->itemState & ODS_DISABLED)
style |= DFCS_INACTIVE;
if (gray) {
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) {
style |= DFCS_PUSHED;
++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom;
++rect.left; ++rect.top;
++rect.right; ++rect.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);
TCHAR text[BUFFER_LEN];
GetWindowText(_hwnd, text, BUFFER_LEN);
DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, GetSysColorBrush(COLOR_BTNFACE), DI_NORMAL);
if (dis->itemState & (ODS_DISABLED|ODS_GRAYED)) {
COLORREF gray = GetSysColor(COLOR_GRAYTEXT);
TCHAR text[BUFFER_LEN];
GetWindowText(_hwnd, text, BUFFER_LEN);
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 {
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_DISABLED|ODS_GRAYED))
DrawGrayText(dis, &textRect, text, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
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) {
style |= DFCS_PUSHED;
++iconPos.x; ++iconPos.y;
++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom;
++rect.left; ++rect.top;
++rect.right; ++rect.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);
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);
DrawFocusRect(dis->hDC, &rect);
}
}
LRESULT StartmenuEntry::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
void StartmenuEntry::DrawItem(LPDRAWITEMSTRUCT dis)
{
if (message == WM_DISPATCH_DRAWITEM) {
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
style |= DFCS_INACTIVE;
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};
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 (dis->itemState & ODS_SELECTED) {
style |= DFCS_PUSHED;
++iconPos.x; ++iconPos.y;
++textRect.left; ++textRect.top;
++textRect.right; ++textRect.bottom;
}
int bk_color = COLOR_BTNFACE;
int text_color = COLOR_BTNTEXT;
int bk_color = COLOR_BTNFACE;
int text_color = COLOR_BTNTEXT;
if (dis->itemState & ODS_FOCUS) {
bk_color = COLOR_HIGHLIGHT;
text_color = COLOR_HIGHLIGHTTEXT;
}
if (dis->itemState & ODS_FOCUS) {
bk_color = COLOR_HIGHLIGHT;
text_color = COLOR_HIGHLIGHTTEXT;
}
HBRUSH bk_brush = GetSysColorBrush(bk_color);
HBRUSH bk_brush = GetSysColorBrush(bk_color);
FillRect(dis->hDC, &dis->rcItem, bk_brush);
DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, bk_brush, DI_NORMAL);
FillRect(dis->hDC, &dis->rcItem, bk_brush);
DrawIconEx(dis->hDC, iconPos.x, iconPos.y, _hicon, 16, 16, 0, bk_brush, DI_NORMAL);
TCHAR text[BUFFER_LEN];
GetWindowText(_hwnd, text, BUFFER_LEN);
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(text_color));
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER/*|DT_CENTER*/);
}
return TRUE;
} else
return super::WndProc(message, wparam, lparam);
if (dis->itemState & (ODS_DISABLED|ODS_GRAYED))
DrawGrayText(dis, &textRect, text, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
else {
BkMode mode(dis->hDC, TRANSPARENT);
TextColor lcColor(dis->hDC, GetSysColor(text_color));
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER);
}
}

View file

@ -369,54 +369,77 @@ template<typename BASE> struct OwnerDrawParent : public BASE
/**
Subclass button controls to paint colored text labels.
The owning window should use the OwnerDrawParent template to route woner draw messages to the buttons.
Subclass button controls to draw them by using WM_DISPATCH_DRAWITEM
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;
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)
: super(hwnd), _textColor(textColor) {}
protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam);
virtual void DrawItem(LPDRAWITEMSTRUCT dis);
COLORREF _textColor;
};
*/
/**
Subclass button controls to paint pictures left to the labels.
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)
: super(hwnd), _hicon(hicon), _flat(flat) {}
protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam);
virtual void DrawItem(LPDRAWITEMSTRUCT dis);
HICON _hicon;
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)
: super(hwnd), _hicon(hicon), _flat(flat) {}
StartmenuEntry(HWND hwnd, HICON hicon)
: super(hwnd), _hicon(hicon) {}
protected:
LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam);
virtual void DrawItem(LPDRAWITEMSTRUCT dis);
HICON _hicon;
bool _flat;
};