Public Types | Public Member Functions | Protected Member Functions | Protected Attributes

FlatButton Struct Reference

Subclass button controls to paint colored text labels. More...

#include <window.h>

Inheritance diagram for FlatButton:
Inheritance graph
[legend]
Collaboration diagram for FlatButton:
Collaboration graph
[legend]

List of all members.

Public Types

typedef OwnerdrawnButton super

Public Member Functions

 FlatButton (HWND hwnd)
 FlatButton (HWND owner, int id)

Protected Member Functions

LRESULT WndProc (UINT nmsg, WPARAM wparam, LPARAM lparam)
virtual void DrawItem (LPDRAWITEMSTRUCT dis)

Protected Attributes

COLORREF _textColor
COLORREF _activeColor
bool _active

Detailed Description

Subclass button controls to paint colored text labels.

The owning window should use the OwnerDrawParent template to route owner draw messages to the buttons.

Definition at line 691 of file window.h.


Member Typedef Documentation

Reimplemented from OwnerdrawnButton.

Definition at line 693 of file window.h.


Constructor & Destructor Documentation

FlatButton::FlatButton ( HWND  hwnd ) [inline]

Definition at line 695 of file window.h.

     : super(hwnd), _active(false) {}
FlatButton::FlatButton ( HWND  owner,
int  id 
) [inline]

Definition at line 698 of file window.h.

     : super(GetDlgItem(owner, IDOK)), _active(false) {}

Member Function Documentation

void FlatButton::DrawItem ( LPDRAWITEMSTRUCT  dis ) [protected, virtual]

Implements OwnerdrawnButton.

Definition at line 1045 of file window.cpp.

References _active, _activeColor, WindowHandle::_hwnd, _textColor, and BUFFER_LEN.

{
    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;
    }

    FillRect(dis->hDC, &dis->rcItem, GetSysColorBrush(COLOR_BTNFACE));

     // highlight the button?
    if (_active)
        DrawEdge(dis->hDC, &dis->rcItem, EDGE_ETCHED, BF_RECT);
    else if (GetWindowStyle(_hwnd) & BS_FLAT)   // Only with BS_FLAT there will be drawn a frame to show highlighting.
        DrawEdge(dis->hDC, &dis->rcItem, EDGE_RAISED, BF_RECT|BF_FLAT);

    TCHAR txt[BUFFER_LEN];
    int txt_len = GetWindowText(_hwnd, txt, 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, txt, txt_len, &shadowRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
            }

            BkMode mode(dis->hDC, TRANSPARENT);
            TextColor lcColor(dis->hDC, gray);
            DrawText(dis->hDC, txt, txt_len, &textRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
        } else {
            int old_r = textRect.right;
            int old_b = textRect.bottom;
            DrawText(dis->hDC, txt, txt_len, &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)txt, txt_len, x, y, w, h);
        }
    } else {
        TextColor lcColor(dis->hDC, _active? _activeColor: _textColor);
        DrawText(dis->hDC, txt, txt_len, &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);
    }
}
LRESULT FlatButton::WndProc ( UINT  nmsg,
WPARAM  wparam,
LPARAM  lparam 
) [protected, virtual]

Reimplemented from OwnerdrawnButton.

Definition at line 1114 of file window.cpp.

References _active, WindowHandle::_hwnd, HWND, and OwnerdrawnButton::WndProc().

{
    switch(nmsg) {
      case WM_MOUSEMOVE: {
        bool active = false;

        if (IsWindowEnabled(_hwnd)) {
            DWORD pid_foreground;
            HWND hwnd_foreground = GetForegroundWindow();   //@@ may be better look for WM_ACTIVATEAPP ?
            GetWindowThreadProcessId(hwnd_foreground, &pid_foreground);

            if (GetCurrentProcessId() == pid_foreground) {
                POINT pt = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
                ClientRect clntRect(_hwnd);

                 // highlight the button?
                if (pt.x>=clntRect.left && pt.x<clntRect.right && pt.y>=clntRect.top && pt.y<clntRect.bottom)
                    active = true;
            }
        }

        if (active != _active) {
            _active = active;

            if (active) {
                TRACKMOUSEEVENT tme = {sizeof(tme), /*TME_HOVER|*/TME_LEAVE, _hwnd/*, HOVER_DEFAULT*/};
                _TrackMouseEvent(&tme);
            }

            InvalidateRect(_hwnd, NULL, TRUE);
        }

        return 0;}

      case WM_LBUTTONUP: {
        POINT pt = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
        ClientRect clntRect(_hwnd);

         // no more in the active rectangle?
        if (pt.x<clntRect.left || pt.x>=clntRect.right || pt.y<clntRect.top || pt.y>=clntRect.bottom)
            goto cancel_press;

        goto def;}

      case WM_CANCELMODE:
      cancel_press: {
        TRACKMOUSEEVENT tme = {sizeof(tme), /*TME_HOVER|*/TME_LEAVE|TME_CANCEL, _hwnd/*, HOVER_DEFAULT*/};
        _TrackMouseEvent(&tme);
        _active = false;
        ReleaseCapture();}
        // fall through

      case WM_MOUSELEAVE:
        if (_active) {
            _active = false;

            InvalidateRect(_hwnd, NULL, TRUE);
        }

        return 0;

      default: def:
        return super::WndProc(nmsg, wparam, lparam);
    }
}

Here is the call graph for this function:


Member Data Documentation

bool FlatButton::_active [protected]

Definition at line 707 of file window.h.

Referenced by DrawItem(), and WndProc().

COLORREF FlatButton::_activeColor [protected]

Definition at line 706 of file window.h.

Referenced by DrawItem().

COLORREF FlatButton::_textColor [protected]

Definition at line 705 of file window.h.

Referenced by DrawItem().


The documentation for this struct was generated from the following files:

ROS Explorer Source Code Documentation
generated on 02.01.2013 by doxygen

ROS Explorer Homepage