⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menubutton.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    //load the background bitmap
    Bitmap_t& BitmapToUse = (m_IsPressedDown ? m_ButtonDown : m_Button);

    if (!BitmapToUse)
    {
        hr = BitmapToUse.LoadBitmap(
            GlobalData_t::s_ModuleInstance,
            (m_IsPressedDown ? IDB_BUTTONDOWN : IDB_BUTTON)
            );
        if (FAILED(hr))
        {
            return 0;
        }
    }

    //offset the rectangle to leave space for the connector line
    ClientRect.bottom -= Layout_t::ButtonConnectorLine();

    //draw the background
    hr = paint.TileBlt(
        &BitmapToUse,
        &ClientRect,
        NULL,
        Layout_t::ButtonTileLeft(),
        Layout_t::ButtonTileTop(),
        Layout_t::ButtonTileRight(),
        Layout_t::ButtonTileBottom(),
        Colors_t::DefaultTransparentColor()
        );
    ASSERT(SUCCEEDED(hr));

    paint.SetBkMode(TRANSPARENT);

    BOOL IsSIPButton = (m_Data.IsInputButton && (! Input_IsIMEEnabled() || Input_IsInputPanelVisible()));
    
    //do we need to draw the popup arrow?
    if (
        (m_Data.IsMenu && ! m_Data.IsInputButton) 
        || (IsSIPButton && Input_IsIMEEnabled())
        )
    {
        Bitmap_t PopupArrow;
        hr = PopupArrow.Attach(
            GlobalData_t::s_GDICacheObject.LoadCachedBitmap(IDB_POPUPMENU_UP_ARROW)
            );
        ASSERT(SUCCEEDED(hr));

        ClientRect.right -= (PopupArrow.Width() + Layout_t::PopupUpArrowRightMargin());

        TransparentImage(
            paint,
            ClientRect.right,
            ClientRect.top + Layout_t::PopupUpArrowTopMargin(),
            PopupArrow.Width(),
            PopupArrow.Height(),
            PopupArrow,
            0,
            0,
            PopupArrow.Width(),
            PopupArrow.Height(),
            Colors_t::DefaultTransparentColor()
            );
    }

    if (m_Data.IsInputButton)
    {
        UINT BitmapID = IsSIPButton ?
            IDB_SIP_ICON :
            IDB_IME_BACKGROUND;

        Bitmap_t InputIcon;
        hr = InputIcon.Attach(
            GlobalData_t::s_GDICacheObject.LoadCachedBitmap(BitmapID)
            );
        ASSERT(SUCCEEDED(hr));

        //Draw the sip icon centered in the button
        TransparentImage(
            paint,
            (ClientRect.left + ClientRect.right - InputIcon.Width())/2,
            (ClientRect.top + ClientRect.bottom - InputIcon.Height())/2,
            InputIcon.Width(),
            InputIcon.Height(),
            InputIcon,
            0,
            0,
            InputIcon.Width(),
            InputIcon.Height(),
            Colors_t::DefaultTransparentColor()
            );

        if (!IsSIPButton && GetInputButtonText(TextBuffer, _countof(TextBuffer)) > 0)
        {
            paint.SetFont(Fonts_t::StandardText());
            paint.SetTextColor(
                m_IsPressedDown ?
                    Colors_t::MenuButtonDownTextColor() :
                    Colors_t::MenuButtonUpTextColor()
                );

            RECT DrawRect;
            DrawRect = ClientRect;

            DrawRect.left = Layout_t::PopupTextMargin() +
                (ClientRect.left + ClientRect.right - InputIcon.Width())/2;

            paint.DrawText(
                TextBuffer,
                -1,
                &DrawRect,
                DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX
                );
        }
    }
    else
    {
        //get the window text and draw it
        if (GetWindowText(m_hwnd, TextBuffer, _countof(TextBuffer)) > 0)
        {
            ASSERT(TextBuffer[0] != L'\0');

            paint.SetFont(Fonts_t::StandardText());
            paint.SetTextColor(
                m_IsPressedDown ?
                    Colors_t::MenuButtonDownTextColor() :
                    Colors_t::MenuButtonUpTextColor()
                );

            RECT DrawRect;
            DrawRect = ClientRect;

            // Subtract margin
            InflateRect(&DrawRect, -Layout_t::PopupTextMargin(), 0);

            paint.DrawText(
                TextBuffer,
                -1,
                &ClientRect,
                DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_END_ELLIPSIS
                );
        }
    }

    ClientRect.bottom += Layout_t::ButtonConnectorLine();

    //Draw the connector line
    POINT ConnectorLine[2];
    ConnectorLine[0].x = ClientRect.left + Layout_t::ButtonWidth()/2;
    ConnectorLine[0].y = ClientRect.bottom - Layout_t::ButtonConnectorLine();
    ConnectorLine[1].x = ConnectorLine[0].x;
    ConnectorLine[1].y = ClientRect.bottom;

    //create the pen with which to draw
    ce::auto_hpen Pen = CreatePen(
        PS_SOLID,
        -1, 
        m_IsPressedDown ?
            Colors_t::ButtonDownConnectorLineColor() :
            Colors_t::ButtonUpConnectorLineColor()
        );
    paint.SetPen(Pen);

    Polyline(
        paint,
        ConnectorLine,
        _countof(ConnectorLine)
        );

    paint.End();
    return 0;
}

/*------------------------------------------------------------------------------
    MenuButtonImpl_t::OnLButtonDown

    Handle a left button down press in our client area
------------------------------------------------------------------------------*/
LRESULT
MenuButtonImpl_t::OnLButtonDown(
    WPARAM wParam,
    LPARAM lParam
    )
{
    //ignore this is we are already 'down'
    if (m_IsPressedDown)
    {
        return 0;
    }

    m_IsPressedDown = true;

    //capture mouse movements
    SetCapture(m_hwnd);

    if (m_Data.IsMenu && IsWindowVisible(m_Data.Value.PopupMenu))
    {
        m_PopupIsVisible = true;
    }

    //force a redraw
    InvalidateRect(m_hwnd, NULL, FALSE);

    return 0;
}

/*------------------------------------------------------------------------------
    MenuButtonImpl_t::OnLostCapture

    Handle losing capture because another window has grabbed capture
    or the mouse has moved outside of our client area
------------------------------------------------------------------------------*/
LRESULT
MenuButtonImpl_t::OnLostCapture(
    void
    )
{
    if (! m_IsPressedDown)
    {
        return 0;
    }

    m_IsPressedDown  = false;
    m_PopupIsVisible = false;
    
    //trigger repaint
    InvalidateRect(m_hwnd, NULL, FALSE);

    return 0;
}

/*------------------------------------------------------------------------------
    MenuButtonImpl_t::OnLButtonUp

    Handle the left-button up event within our client area
------------------------------------------------------------------------------*/
LRESULT
MenuButtonImpl_t::OnLButtonUp(
    WPARAM wParam,
    LPARAM lParam
    )
{
    if (! m_IsPressedDown)
    {
        return 0;
    }

    m_IsPressedDown = false;
    
    ReleaseCapture();

    bool WasMenuAlreadyUp = m_PopupIsVisible;

    m_PopupIsVisible = false;
    
    //trigger repaint
    InvalidateRect(m_hwnd, NULL, FALSE);

    //we were clicked - generate a command!
    if (! m_Data.IsMenu)
    {
        if (m_Data.IsInputButton)
        {
            Input_ToggleInputPanel();
        }
        else
        {
            SendMessage(
                GetParent(m_hwnd),
                WM_COMMAND,
                MAKELONG(m_Data.Value.ControlId, BN_CLICKED),
                reinterpret_cast<LPARAM>(m_hwnd)
                );
        }
    }
    else 
    {
        PopupMenu_t Menu;
        Menu = m_Data.Value.PopupMenu;

        if (! IsWindow((HWND)Menu))
        {
            ASSERT(FALSE);
            return 0;
        }

        //if the menu was already up tell the current focused menu 
        //(the menus may be cascading) to hide all the menus
        if (WasMenuAlreadyUp)
        {
            SendMessage(GetFocus(), WM_POPUPMENU_HIDEMENUS, 0, 0);
            return 0;
        }
        
        SIZE Dimensions = {0};
        RECT WindowRect = {0};

        //get the length and width of the popup menu
        Menu.CalculateDimensions(&Dimensions);

        GetWindowRect(m_hwnd, &WindowRect);
        MapWindowRect(NULL, GetParent(m_hwnd), &WindowRect);

        SetWindowPos(
            (HWND)Menu,
            HWND_TOP,
            WindowRect.left,
            WindowRect.top - Dimensions.cy,
            Dimensions.cx,
            Dimensions.cy,
            SWP_SHOWWINDOW
            );

        SetFocus((HWND)Menu);
    }

    return 0;
}

/*------------------------------------------------------------------------------
    MenuButtonImpl_t::OnMouseMove

    Track the mouse moving while we have been pressed

    Parameters:
        xPos: current x-coordinate of the mouse
        yPos: current y-coordinate of the mouse
------------------------------------------------------------------------------*/
LRESULT
MenuButtonImpl_t::OnMouseMove(
    WPARAM wParam,
    LPARAM lParam
    )
{
    //if we aren't pressed down, ignore this
    if (! m_IsPressedDown)
    {
        return 0;
    }

    RECT ClientRect;
    GetClientRect(m_hwnd, &ClientRect);

    WORD xPos = LOWORD(lParam);
    WORD yPos = HIWORD(lParam);

    //if the mouse has moved outside of our client area,
    //release the mouse capture and mark ourself as 'not down'
    if (xPos < ClientRect.left || xPos > ClientRect.right ||
        yPos < ClientRect.top  || yPos > ClientRect.bottom
        )
    {
        ReleaseCapture();
        OnLostCapture();
    }

    return 0;
}

/*------------------------------------------------------------------------------
    MenuButtonImpl_t::OnShow

    Show / hide the button

    Parameters:
        BOOL: indicates whether the button should be visible or not
------------------------------------------------------------------------------*/
HRESULT
MenuButtonImpl_t::OnShow(
    BOOL Visible
    )
{
    if (Visible)
    {
        return ShowWindow(m_hwnd, SW_SHOW) ? S_FALSE : S_OK;
    }
    else
    {
        return ShowWindow(m_hwnd, SW_HIDE) ? S_OK : S_FALSE;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -