Setting the initial position of a dialog-based application
Problem
When a dialog-based application is started, the main window (modal dialog) is positioned into center of screen. Problem: How can I position this window, where I want. Of course many will try to cal SetWindowPos ( MoveWindow, ...) , into OnInitDialog. But they will have a surprise, anyway the dialog will appear too in center of screen. Another solution, will be to set up a timer which will reposition the window. This is a dirty solution, so I'm offering a more elegant solution in this article.
Solution
Add a message handler for WM_WINDOWPOSCHANGING
void CAppDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
if (lpwndpos->cx = -1)
if (lpwndpos->cy = -1) // is init time
{
lpwndpos->x = 0; // change here if you want to move left or right the window.
lpwndpos->y = 0; // change here if you want to move top or bottom the window.
}
CDialog::OnWindowPosChanging(lpwndpos);
}
Explanation
After CreateDialogIndirect, the MFC call CenterWindow. The last line of this function call SetWindowPos with position of window, and with -1, -1, as width and height of window. So the WM_WINDOWPOSCHANGING is obviously