Solved

How do I reposition and resize a dialog at runtime (programmatically)

Posted on 1997-05-02
3
1,978 Views
Last Modified: 2013-11-20
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(fwRect);
dialog->GetWindowRect(dwRect);
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
0
Comment
Question by:epeddyco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Melange earned 50 total points
ID: 1301292
Is your dialog a child of your frame window or a popup window? If it is a pop-up window (even if you specified a parent window), then make sure you use screen coordinates in the call to MoveWindow. If it is truly child window, then the coordinates will be in client coordinates relative to the dialog's parent.

If you're still having a problem, then send me your source code.
0
 

Author Comment

by:epeddyco
ID: 1301293
I am still having problems with this.  Any more suggestions?

Here is my OnInitialUpdate...
------------------------------------------------------------------
void CBasicTestView::OnInitialUpdate()
{
      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(CBasicDialog::IDD, this);
//      theBasicDlg->ShowWindow(SW_SHOWNORMAL);
//      theBasicDlg->CenterWindow();

      //Get the CView's client area coordinates
      RECT rcViewClientRect;
      GetClientRect(&rcViewClientRect);

      //Get the dialog screen coordinates
      RECT rcDialogWindowRect;
    theBasicDlg->GetWindowRect(&rcDialogWindowRect);

      // 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(&rcDialogWindowRect);
      theBasicDlg->ShowWindow(SW_SHOWNORMAL);
}

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(CBasicDialog)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
      //}}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(CWnd* pParent /*=NULL*/)
      : CDialog(CBasicDialog::IDD, pParent)
{
      //{{AFX_DATA_INIT(CBasicDialog)
      //}}AFX_DATA_INIT
}


void CBasicDialog::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CBasicDialog)
      //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBasicDialog, CDialog)
      //{{AFX_MSG_MAP(CBasicDialog)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBasicDialog message handlers


BOOL CBasicDialog::PreCreateWindow(CREATESTRUCT& cs)
{
      // TODO: Add your specialized code here and/or call the base class
      
      return CDialog::PreCreateWindow(cs);
}

0
 
LVL 3

Expert Comment

by:Melange
ID: 1301294
Try changing your OnInitView function to this:

void CBasicTestView::OnInitialUpdate()
{
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(CBasicDialog::IDD, this);
// theBasicDlg->ShowWindow(SW_SHOWNORMAL);
// theBasicDlg->CenterWindow();

//Get the CView's screen coordinates
CRect rcViewClientRect;
GetWindowRect(rcViewClientRect);

//Get the dialog screen coordinates
CRect rcDialogWindowRect;
theBasicDlg->GetWindowRect(rcDialogWindowRect);

//**NOTE:  If theBasicDlg is not a POPUP window (most dialogs are)
//**       but is a child window, then change the above lines to:
//**
//**       GetClientRect(rcViewClientRect);
//**       GetWindowRect(rcDialogWindowRect);
//**       ScreenToClient(rcDialogWindowRect);
//**

// 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_SHOWNORMAL);
}

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 151
withoutTen challenge 14 140
How to convert MFC::CString to UTF8 wchar_t* 10 459
Message not shown 5 104
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question