0
votes

I want to control the location of a modal dialog. Sorry for the long post, but this is everything that I think could be needed to help with the issue.

I have a dialog window that opens a list when a button is clicked. The Parent is CXEdit and it calls OnFunction(void) which contains the code here:

void CXEdit::OnFunction(void)
{
int     k, n1, n2 ;
LPUDF   u ;
char    name[100],
        *p ;
CFuncList   dlg(xtype) ;
int     j1, j2, j3 ;
CString Fname ;

//  How do I set the modal dialog's position?
dlg.SetWindowPos(NULL, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

//originally this was the only code
k = dlg.DoModal();
if (k == -1)
    return ;
    ...

When I step through the code, everything works properly, except that on a two-monitor system, the dialog appears on the far right:

enter image description here

(I'm using VirtualBox configured to simulate two screens).

If I try to enable the dlg.SetWindowPos() line as shown, I get an ASSERT message and debugging shows that I don't have a handle for the dlg (From WINOCC.CPP):

BOOL CWnd::SetWindowPos(const CWnd* pWndInsertAfter, int x, int y, int cx,
int cy, UINT nFlags)
{
ASSERT(::IsWindow(m_hWnd));

if (m_pCtrlSite == NULL)
    return ::SetWindowPos(m_hWnd, pWndInsertAfter->GetSafeHwnd(),
        x, y, cx, cy, nFlags);
else
    return m_pCtrlSite->SetWindowPos(pWndInsertAfter, x, y, cx, cy, nFlags);
}

the debugger shows m_hWnd = 0x00000000

Once I step into DoModal(), I'm in MFC code and can't make any changes.

The Class CFuncList is defined off of CDialog:

class CFuncList : public CDialog
{
// Construction
public:
CFuncList(int type, CWnd* pParent = NULL);  // standard constructor

// Dialog Data
//{{AFX_DATA(CFuncList)
enum            { IDD = IDD_SELECT_FUNCTION };
int     paste ;
    // NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// Implementation
public:
int     xtype ;
private:
CRrDoc      *pDoc;  // the ubiquitous document pointer
CListCtrl   *flist ;
int         *plist ;
int         findex ;
int         fselect ;
int         Sort ;
int         LCount ;
void        ActiveFocusCommon(void);
void        EnableButtons(void) ;
int         BuildFunkList(void) ;
void        DisplayList(void) ;
void        SortList() ;
void        ReSort(int n) ;
void        SetSelected() ;
protected:
virtual void    DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void    OnDestroy();
// Generated message map functions
//{{AFX_MSG(CFuncList)
virtual BOOL    OnInitDialog        ();
virtual void    OnOK                ();
virtual void    OnCancel            ();
LRESULT         OnCommandHelp       (WPARAM wParam, LPARAM lParam);
afx_msg void    OnHelp              ();
afx_msg void    OnItemClick         (NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void    OnItemDoubleClick   (NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void    OnColumnClick       (NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
1
Thanks! I hadn't been able to find the CPP file that contained the actual code to define this, but your answer sent me on the right path. Now it's just a matter of calculating the appropriate x / y coordinates.Christian A Strasser

1 Answers

1
votes

You can use SetWindowPos in the dialog's OnInitDialog method. This method is executed after the dialog has an HWND but before it becomes visible.