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

📄 messagebox.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    m_Owner = pParameters->Owner;
    m_Instance = pParameters->Instance;
    m_IconId = pParameters->IconId;

    if (0 == HIWORD(pParameters->pTitle))
    {
        SetWindowTextW(
            m_hwnd,
            CommonUtilities_t::LoadString(m_Instance, LOWORD(pParameters->pTitle))
            );
    }
    else
    {
        SetWindowTextW(m_hwnd, pParameters->pTitle);
    }

    if (0 == HIWORD(pParameters->pText))
    {
        m_Text.assign(
            CommonUtilities_t::LoadString(m_Instance, LOWORD(pParameters->pText))
            );
    }
    else if (pParameters->pText)
    {
        m_Text.assign(pParameters->pText);
    }

    MessageBoxImpl_t::ResizeMessageBox(
        m_hwnd,
        pParameters->pText
        );

    //set buttons
    MenuBar_t MenuBar;
    MenuBar = GetDlgItem(m_hwnd, IDC_MENUBAR);

    //message box was reized, so first reset cached position of menu button buttons
    MenuBar.ResetPositions();

    if (!pParameters->MenuId)
    {
        MenuBar.SetMenu(GlobalData_t::s_ModuleInstance, IDMB_DEFAULT_MESSAGE_BOX);
    }
    else
    {
        //Load custom buttons
        MenuBar.SetMenu(m_Instance, pParameters->MenuId);
    }

    return FALSE;
}

/*------------------------------------------------------------------------------
    MessageBoxImpl_t::OnPaint

    Handle paint requests for this message box
------------------------------------------------------------------------------*/
LRESULT
MessageBoxImpl_t::OnPaint(
    HDC hdc
    )
{
    WCHAR TextBuffer[MAX_PATH] = L"";
    PaintHelper_t paint;

    //start the painting operation
    if (FAILED(paint.Attach(hdc)) &&
        FAILED(paint.Begin(m_hwnd)))
    {
        return 0;
    }

    RECT ClientRect;
    GetClientRect(m_hwnd, &ClientRect);

    PaintHelper_t paintBuffer;

    //paint directly into device context if it was passed in
    //otherwise do buffered paint to avoid flicker
    PaintHelper_t& paintToUse = (
        hdc ||
        FAILED(paintBuffer.CreateCanvas(
            m_hwnd,
            ClientRect.right,
            ClientRect.bottom
            ))
        ) ? paint : paintBuffer;

    PHDrawBackground(paintToUse, &ClientRect);

    ce::auto_hpen Pen;
    ce::auto_hbrush Brush;

    //Draw the border rectangle and background over the screen
    Pen = CreatePen(PS_SOLID, -1, Colors_t::MessageBoxBorderColor());
    paintToUse.SetPen(Pen);

    Brush = CreateSolidBrush(Colors_t::MessageBoxBackgroundColor());
    paintToUse.SetBrush(Brush);

    Rectangle(
        paintToUse,
        ClientRect.left,
        ClientRect.top,
        ClientRect.right,
        ClientRect.bottom
        );

    //Draw the main text/sub text divider line
    paintToUse.SetPen(NULL);
    Pen = CreatePen(PS_SOLID, -1, Colors_t::MessageBoxDividerLineColor());
    paintToUse.SetPen(Pen);

    POINT DividerLine[2];
    DividerLine[0].x = ClientRect.left + 1;
    DividerLine[0].y = ClientRect.top +
        Layout_t::MessageBoxTextMargin() +
        Layout_t::MessageBoxTitleHeight() +
        Layout_t::MessageBoxTitleToDividerMargin();
    DividerLine[1].x = ClientRect.right - 1;
    DividerLine[1].y = DividerLine[0].y;

    Polyline(
        paintToUse,
        DividerLine,
        _countof(DividerLine)
        );

    //Draw the icon
    if (!m_Icon)
    {
        m_Icon.LoadBitmap(m_Instance, m_IconId);
        ASSERT((HBITMAP)m_Icon);

        //we can easily support multiple icon sizes, by loading the icon early
        //and using the bitmap metrics when message box size is computed
        ASSERT(
            (m_Icon.Width() == Layout_t::MessageBoxIconWidth()) &&
            (m_Icon.Height() == Layout_t::MessageBoxIconHeight())
            );
    }

    POINT IconPosition;
    IconPosition.x = ClientRect.left + Layout_t::MessageBoxTextMargin();
    IconPosition.y = ClientRect.top +
        Layout_t::MessageBoxTextMargin() +
        Layout_t::MessageBoxTitleHeight() +
        Layout_t::MessageBoxTitleToIconMargin();

    TransparentImage(
        paintToUse,
        IconPosition.x,
        IconPosition.y,
        Layout_t::MessageBoxIconWidth(),
        Layout_t::MessageBoxIconHeight(),
        (HBITMAP)m_Icon,
        0,
        0,
        Layout_t::MessageBoxIconWidth(),
        Layout_t::MessageBoxIconHeight(),
        Colors_t::DefaultTransparentColor()
        );

    paintToUse.SetBkMode(TRANSPARENT);
    RECT TextArea = ClientRect;

    InflateRect(&TextArea, -Layout_t::MessageBoxTextMargin(), 0);

    //Draw the Title
    if (GetWindowText(m_hwnd, TextBuffer, _countof(TextBuffer)) > 0)
    {
    #ifdef AUTOMATION
        WCHAR* pDelimiter = wcschr(TextBuffer, STRING_DELIMITER);
        if (pDelimiter)
        {
            *pDelimiter = 0;
        }
    #endif
        paintToUse.SetFont(Fonts_t::LargeText());
        paintToUse.SetTextColor(Colors_t::MessageBoxTitleColor());

        TextArea.top = ClientRect.top + Layout_t::MessageBoxTextMargin();

        paintToUse.DrawText(
            TextBuffer,
            -1,
            &TextArea,
            (DT_TOP | DT_LEFT | DT_NOPREFIX | DT_SINGLELINE)
            );
    }

    //Draw the Text
    paintToUse.SetFont(Fonts_t::StandardText());
    paintToUse.SetTextColor(Colors_t::MessageBoxTextColor());

    TextArea.left += Layout_t::MessageBoxIconWidth() +
        Layout_t::MessageBoxTextMargin();
    TextArea.top = ClientRect.top +
        Layout_t::MessageBoxTextMargin() +
        Layout_t::MessageBoxTitleHeight() +
        Layout_t::MessageBoxTitleToTextMargin();

    paintToUse.DrawText(
        m_Text,
        m_Text.length(),
        &TextArea,
        (DT_LEFT | DT_NOPREFIX | DT_WORDBREAK)
        );

    if (paintToUse == paintBuffer)
    {
        ASSERT(EqualRect(&ClientRect, &paintBuffer.Rect()));

        //AnimateMessageBox
        if (m_FirstTime)
        {
            m_FirstTime = false;

            HBITMAP bitmap = paintBuffer.SetBitmap(NULL);
            AnimateMessageBox(bitmap);
            paintBuffer.SetBitmap(bitmap);
        }

        paint = paintBuffer;
        paintBuffer.End();
    }

    paint.End();
    return 0;
}

void
MessageBoxImpl_t::ResizeMessageBox(
    HWND MessageBox,
    const WCHAR* pText
    )
{
    RECT WindowRect;
    RECT DesiredRect = {0};
    HWND Control;
    int Delta;
    HRESULT hr;

    //Message box metrics:
    //TopToBottom:  ||TextMargin| TitleHeight |TitleToTextMargin| Text Height | ButtonHeight||
    //LeftToRight:  ||TextMargin| IconWidth |TextMargin| Text Width |TextMargin||
    SIZE NonTextArea;

    NonTextArea.cx = 3*Layout_t::MessageBoxTextMargin() +
        Layout_t::MessageBoxIconWidth();

    //extra space needed (ordered from top to bottom)
    NonTextArea.cy = Layout_t::MessageBoxTextMargin() +
        Layout_t::MessageBoxTitleHeight() +
        Layout_t::MessageBoxTitleToTextMargin() +
        Layout_t::ButtonHeight();

    GetWindowRect(MessageBox, &WindowRect);
    ASSERT(
        (RECTWIDTH(WindowRect) > NonTextArea.cx) &&
        (RECTHEIGHT(WindowRect) > NonTextArea.cy)
        );

    //Calculate text height, given the text width
    DesiredRect.right = RECTWIDTH(WindowRect) - NonTextArea.cx;

    hr = PaintHelper_t::CalculateTextDimensions(
        pText,
        -1,
        Fonts_t::StandardText(),
        &DesiredRect,
        (DT_LEFT | DT_NOPREFIX | DT_WORDBREAK)
        );
    ASSERT(DesiredRect.top == 0);

    //check if text fits within the default message box height
    if (FAILED(hr) ||
        (RECTHEIGHT(DesiredRect) < (Layout_t::MessageBoxHeight() - NonTextArea.cy)))
    {
        DesiredRect.bottom = Layout_t::MessageBoxHeight();
    }
    else
    {
        //Add
        DesiredRect.bottom += NonTextArea.cy;
    }

    Delta = RECTHEIGHT(WindowRect) - RECTHEIGHT(DesiredRect);
    SetWindowPos(
        (HWND)MessageBox,
        NULL,
        WindowRect.left,
        WindowRect.top + Delta,
        RECTWIDTH(WindowRect),
        RECTHEIGHT(DesiredRect),
        SWP_NOZORDER | SWP_NOACTIVATE
        );

    //shift children
    Control = GetWindow(MessageBox, GW_CHILD);
    while (Control)
    {
        RECT ControlWindowRect;

        GetWindowRect(Control, &ControlWindowRect);
        MapWindowRect(NULL, MessageBox, &ControlWindowRect);

        SetWindowPos(
            Control,
            NULL,
            ControlWindowRect.left,
            ControlWindowRect.top - Delta ,
            0,
            0,
            SWP_NOZORDER | SWP_NOSIZE
            );

        Control = GetWindow(Control, GW_HWNDNEXT);
    }

    return;
};


⌨️ 快捷键说明

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