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,11 +485,39 @@ 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)
{
COLORREF gray = GetSysColor(COLOR_GRAYTEXT);
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);
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)
{
if (message == WM_DISPATCH_DRAWITEM) {
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
@ -498,32 +536,9 @@ LRESULT ColorButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
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 {
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);
}
@ -539,17 +554,12 @@ LRESULT ColorButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
}
DrawFocusRect(dis->hDC, &rect);
}
return TRUE;
} else
return super::WndProc(message, wparam, lparam);
}
*/
LRESULT PictureButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
void PictureButton::DrawItem(LPDRAWITEMSTRUCT dis)
{
if (message == WM_DISPATCH_DRAWITEM) {
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
@ -576,32 +586,9 @@ LRESULT PictureButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
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 {
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*/);
@ -618,17 +605,11 @@ LRESULT PictureButton::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
}
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) {
LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lparam;
UINT style = DFCS_BUTTONPUSH;
if (dis->itemState & ODS_DISABLED)
@ -660,38 +641,11 @@ LRESULT StartmenuEntry::WndProc(UINT message, WPARAM wparam, LPARAM lparam)
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 {
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/*|DT_CENTER*/);
DrawText(dis->hDC, text, -1, &textRect, DT_SINGLELINE|DT_VCENTER);
}
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.
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;
};