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:
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);
}
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: