admin管理员组

文章数量:1125462

Code:

void CPublicTalksDatabaseDlg::AdjustFindDialogPosition(const CRect& rectControl)
{
    CRect rectDlg;
    m_pFindDlg->GetWindowRect(rectDlg);

    // Calculate the new Y position to move the dialog
    int newY = rectControl.bottom + GetSystemMetrics(SM_CYHSCROLL); // Use vertical scrollbar width

    // Move the dialog to the new Y position, keeping the same X position
    rectDlg.MoveToY(newY);

    m_pFindDlg->MoveWindow(&rectDlg);
}

It is not working correctly. The rectControl represents the child control on the main window in screen coordinates. I now want to move the m_pFindDlg (if needed) so that atleast part of the control will be visible. Right now it is not working correctly.

It ends up here:

But I thought it might up up here:

Code:

void CPublicTalksDatabaseDlg::AdjustFindDialogPosition(const CRect& rectControl)
{
    CRect rectDlg;
    m_pFindDlg->GetWindowRect(rectDlg);

    // Calculate the new Y position to move the dialog
    int newY = rectControl.bottom + GetSystemMetrics(SM_CYHSCROLL); // Use vertical scrollbar width

    // Move the dialog to the new Y position, keeping the same X position
    rectDlg.MoveToY(newY);

    m_pFindDlg->MoveWindow(&rectDlg);
}

It is not working correctly. The rectControl represents the child control on the main window in screen coordinates. I now want to move the m_pFindDlg (if needed) so that atleast part of the control will be visible. Right now it is not working correctly.

It ends up here:

But I thought it might up up here:

Share Improve this question edited 2 days ago Andrew Truckle asked 2 days ago Andrew TruckleAndrew Truckle 19k17 gold badges82 silver badges214 bronze badges 2
  • Which is the child control on the main window? The list box starting with "001 -- How Well Do You Know God?"? – Jabberwocky Commented 2 days ago
  • @Jabberwocky In that instance the child is the selected item of the list box you refer to, yes. – Andrew Truckle Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

Ironically, something as simple as this seems better:

void CPublicTalksDatabaseDlg::AdjustFindDialogPosition(CRect &rectControl)
{
    CRect rectDlg;
    m_pFindDlg->GetWindowRect(rectDlg);

    m_pFindDlg->SetWindowPos(nullptr, rectControl.CenterPoint().x, rectControl.CenterPoint().y, 0, 0, SWP_NOSIZE | SWP_NOZORDER |
        SWP_SHOWWINDOW);
}


Update

I stumbled on the solution (not explanation) quite by chance by making the function static:

    // Adjusts the position of the find dialog relative to a specified control or a list box item
    static void AdjustFindDialogPosition(CMyFindDialog* pFindDialog, CWnd * pFocusControl, const int itemIndex = -1) {
            if (pFindDialog && pFocusControl) {
            CRect rectControl{};

            if (itemIndex != -1) {
                // Get rectangle of the list box item corresponding to itemIndex
                CListBox* pListBox = dynamic_cast<CListBox*>(pFocusControl);
                if (pListBox) {
                    pListBox->GetItemRect(itemIndex, &rectControl);
                    pListBox->ClientToScreen(&rectControl);
                }
            }
            else {
                // Get window rectangle of the specified control
                pFocusControl->GetWindowRect(rectControl);
            }

            CRect rectDlg;
            pFindDialog->GetWindowRect(rectDlg);

            // Set the find dialog position to the center of the control's rectangle
            const auto centerPoint = rectControl.CenterPoint();
            pFindDialog->SetWindowPos(nullptr,
                centerPoint.x,
                centerPoint.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
        }
    }

Now:

本文标签: visual cAdjusting popup window position is working incorrectlyStack Overflow