epeddyco
asked on
How do I reposition and resize a dialog at runtime (programmatically)
In CView::OnInitialUpdate() of my app, I create a modeless dialog box. I want to be able to set the reposition and resize of the dialog at runtime. I am having problems with the CWnd::MoveWindow(). I cannot seem to get it to work. The dialog does not end up in what I think is the correct place. I tried the following method.
(pseudocode)
CFrameWnd->GetWindowRect(f wRect);
dialog->GetWindowRect(dwRe ct);
modify dwRect based on fwRect
dialog->MoveWindow(dwRect)
I calculate the new dialog position and size based on the CFrameWnd's position and size. How do I need to do this?
Ed
(pseudocode)
CFrameWnd->GetWindowRect(f
dialog->GetWindowRect(dwRe
modify dwRect based on fwRect
dialog->MoveWindow(dwRect)
I calculate the new dialog position and size based on the CFrameWnd's position and size. How do I need to do this?
Ed
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try changing your OnInitView function to this:
void CBasicTestView::OnInitialU pdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
// Goal:
// Create dialog and move it so that the bottom right corner
// of the dialog window rect is aligned with the bottom right
// corner of CBasicTestView Client area
// Create the dialog
theBasicDlg = new CBasicDialog;
theBasicDlg->Create(CBasic Dialog::ID D, this);
// theBasicDlg->ShowWindow(SW _SHOWNORMA L);
// theBasicDlg->CenterWindow( );
//Get the CView's screen coordinates
CRect rcViewClientRect;
GetWindowRect(rcViewClient Rect);
//Get the dialog screen coordinates
CRect rcDialogWindowRect;
theBasicDlg->GetWindowRect (rcDialogW indowRect) ;
//**NOTE: If theBasicDlg is not a POPUP window (most dialogs are)
//** but is a child window, then change the above lines to:
//**
//** GetClientRect(rcViewClient Rect);
//** GetWindowRect(rcDialogWind owRect);
//** ScreenToClient(rcDialogWin dowRect);
//**
// Calculate the new position for the dialog
CPoint NewDlgPos;
NewDlgPos.x = rcViewClientRect.right - rcDialogWindowRect.Width() ;
NewDlgPos.y = rcViewClientRect.bottom - rcDialogWindowRect.Height( );
// Move the dialog into its new position
theBasicDlg.SetWindowPos( NULL, NewDlgPos.x, NewDlgPos.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
theBasicDlg->ShowWindow(SW _SHOWNORMA L);
}
void CBasicTestView::OnInitialU
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
// Goal:
// Create dialog and move it so that the bottom right corner
// of the dialog window rect is aligned with the bottom right
// corner of CBasicTestView Client area
// Create the dialog
theBasicDlg = new CBasicDialog;
theBasicDlg->Create(CBasic
// theBasicDlg->ShowWindow(SW
// theBasicDlg->CenterWindow(
//Get the CView's screen coordinates
CRect rcViewClientRect;
GetWindowRect(rcViewClient
//Get the dialog screen coordinates
CRect rcDialogWindowRect;
theBasicDlg->GetWindowRect
//**NOTE: If theBasicDlg is not a POPUP window (most dialogs are)
//** but is a child window, then change the above lines to:
//**
//** GetClientRect(rcViewClient
//** GetWindowRect(rcDialogWind
//** ScreenToClient(rcDialogWin
//**
// Calculate the new position for the dialog
CPoint NewDlgPos;
NewDlgPos.x = rcViewClientRect.right - rcDialogWindowRect.Width()
NewDlgPos.y = rcViewClientRect.bottom - rcDialogWindowRect.Height(
// Move the dialog into its new position
theBasicDlg.SetWindowPos( NULL, NewDlgPos.x, NewDlgPos.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
theBasicDlg->ShowWindow(SW
}
ASKER
Here is my OnInitialUpdate...
--------------------------
void CBasicTestView::OnInitialU
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
// Goal:
// Create dialog and move it so that the bottom right corner
// of the dialog window rect is aligned with the bottom right
// corner of CBasicTestView Client area
// Create the dialog
theBasicDlg = new CBasicDialog;
theBasicDlg->Create(CBasic
// theBasicDlg->ShowWindow(SW
// theBasicDlg->CenterWindow(
//Get the CView's client area coordinates
RECT rcViewClientRect;
GetClientRect(&rcViewClien
//Get the dialog screen coordinates
RECT rcDialogWindowRect;
theBasicDlg->GetWindowRect
// Set the Dialogs Bottom right corner to
// CView client area bottom right
rcDialogWindowRect.bottom = rcViewClientRect.bottom;
rcDialogWindowRect.right = rcViewClientRect.right;
// Calculate ammount to move top,left corner so
// that size remains the same
int dv = rcViewClientRect.bottom - rcDialogWindowRect.bottom;
int dh = rcViewClientRect.right - rcDialogWindowRect.right;
// Set the Dialogs top left corner so
// that size remains the same
rcDialogWindowRect.top += dv;
rcDialogWindowRect.left += dh;
theBasicDlg->MoveWindow(&r
theBasicDlg->ShowWindow(SW
}
For completeness sake, here is the code for the dialog...
--------------------------
// BasicDialog.h : header file
//
//////////////////////////
// CBasicDialog dialog
class CBasicDialog : public CDialog
{
// Construction
public:
CBasicDialog(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CBasicDialog)
enum { IDD = IDD_DIALOG1 };
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBasicDial
protected:
virtual void DoDataExchange(CDataExchan
virtual BOOL PreCreateWindow(CREATESTRU
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CBasicDialog)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
--------------------------
// BasicDialog.cpp : implementation file
//
#include "stdafx.h"
#include "BasicTest.h"
#include "BasicDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////
// CBasicDialog dialog
CBasicDialog::CBasicDialog
: CDialog(CBasicDialog::IDD,
{
//{{AFX_DATA_INIT(CBasicDi
//}}AFX_DATA_INIT
}
void CBasicDialog::DoDataExchan
{
CDialog::DoDataExchange(pD
//{{AFX_DATA_MAP(CBasicDia
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBasicDi
//{{AFX_MSG_MAP(CBasicDial
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////
// CBasicDialog message handlers
BOOL CBasicDialog::PreCreateWin
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::PreCreateWindow(c
}