Changing the location of a CFileDialog.
-
Is it possible to change the startup location of a CFileDialog? I am working on a legacy application that makes use of multiple monitors, and I need to be able to position the location of the file load/save dialogs. I derived my own class based on CFileDialog and added what I thought was the appropriate SetWindowsPos call in the OnInitDialog method, but the things just will not move. If I change the windows size instead of the position, it correctly truncates the dialog, so I'm fairly sure I have the right window, yet no matter what values I pass in for the position, the location remains the same. Below is the important code from a test program.
BOOL CMyFileDialog::OnInitDialog()
{
CFileDialog::OnInitDialog();CWnd \*pW = GetParent(); // Get a pointer to the parent window. RECT Rect; pW->GetWindowRect(&Rect); //Get rect of parent window pW->SetWindowPos( NULL, 10, 10, Rect.right - Rect.left, Rect.bottom - Rect.top, 0); return TRUE;
}
If it makes any difference this is using VS2005.
-
Is it possible to change the startup location of a CFileDialog? I am working on a legacy application that makes use of multiple monitors, and I need to be able to position the location of the file load/save dialogs. I derived my own class based on CFileDialog and added what I thought was the appropriate SetWindowsPos call in the OnInitDialog method, but the things just will not move. If I change the windows size instead of the position, it correctly truncates the dialog, so I'm fairly sure I have the right window, yet no matter what values I pass in for the position, the location remains the same. Below is the important code from a test program.
BOOL CMyFileDialog::OnInitDialog()
{
CFileDialog::OnInitDialog();CWnd \*pW = GetParent(); // Get a pointer to the parent window. RECT Rect; pW->GetWindowRect(&Rect); //Get rect of parent window pW->SetWindowPos( NULL, 10, 10, Rect.right - Rect.left, Rect.bottom - Rect.top, 0); return TRUE;
}
If it makes any difference this is using VS2005.
-
Is it possible to change the startup location of a CFileDialog? I am working on a legacy application that makes use of multiple monitors, and I need to be able to position the location of the file load/save dialogs. I derived my own class based on CFileDialog and added what I thought was the appropriate SetWindowsPos call in the OnInitDialog method, but the things just will not move. If I change the windows size instead of the position, it correctly truncates the dialog, so I'm fairly sure I have the right window, yet no matter what values I pass in for the position, the location remains the same. Below is the important code from a test program.
BOOL CMyFileDialog::OnInitDialog()
{
CFileDialog::OnInitDialog();CWnd \*pW = GetParent(); // Get a pointer to the parent window. RECT Rect; pW->GetWindowRect(&Rect); //Get rect of parent window pW->SetWindowPos( NULL, 10, 10, Rect.right - Rect.left, Rect.bottom - Rect.top, 0); return TRUE;
}
If it makes any difference this is using VS2005.
Your code is setting the postion of your parent window, not your dialog! In reality it is doing nothing since the origin and size remain the same just moving the parent window to point 10, 10 of the screen. [edit]Missed the new origin[/edit]
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Your code is setting the postion of your parent window, not your dialog! In reality it is doing nothing since the origin and size remain the same just moving the parent window to point 10, 10 of the screen. [edit]Missed the new origin[/edit]
Just say 'NO' to evaluated arguments for diadic functions! Ash
The documentation I've seen seems to indicate that you have to actually get the parent of the CFileDialog. Since I can manipulate the size of the dialog this way, it would seem that I have the right window. Further experimenting with my test program shows that I can in fact move the location of the dialog a limited amount. It appear, however, that something prevents you from moving the dialog so that any of it is off of the screen. Unfortunately, I need to move it to a second monitor which seems to be a problem.
-
The documentation I've seen seems to indicate that you have to actually get the parent of the CFileDialog. Since I can manipulate the size of the dialog this way, it would seem that I have the right window. Further experimenting with my test program shows that I can in fact move the location of the dialog a limited amount. It appear, however, that something prevents you from moving the dialog so that any of it is off of the screen. Unfortunately, I need to move it to a second monitor which seems to be a problem.
rentzk wrote:
The documentation I've seen seems to indicate that you have to actually get the parent of the CFileDialog.
Here[^] is the documentation for
SetWindowPos()
. The problem with your code is that you are calling this function on the parent window rather than the dialog, and thus your dialog does not move. If you make the correct call then you should be able to move your dialog anywhere within your monitors.GetWindowRect(&Rect); //Get rect of dialog window
// move the dialog to offset 10,10 of the screen
SetWindowPos(pW, 10, 10, Rect.right - Rect.left, Rect.bottom - Rect.top, 0);I have a feeling that you need to use some special values of x to move it from the main monitor, but cannot recall the exact formula; try searching MSDN or Google.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
rentzk wrote:
The documentation I've seen seems to indicate that you have to actually get the parent of the CFileDialog.
Here[^] is the documentation for
SetWindowPos()
. The problem with your code is that you are calling this function on the parent window rather than the dialog, and thus your dialog does not move. If you make the correct call then you should be able to move your dialog anywhere within your monitors.GetWindowRect(&Rect); //Get rect of dialog window
// move the dialog to offset 10,10 of the screen
SetWindowPos(pW, 10, 10, Rect.right - Rect.left, Rect.bottom - Rect.top, 0);I have a feeling that you need to use some special values of x to move it from the main monitor, but cannot recall the exact formula; try searching MSDN or Google.
Just say 'NO' to evaluated arguments for diadic functions! Ash
I don't think it's the documentation for SetWindowPos that is relevant here, but instead the various articles describing how to modify CFileDialog. These articles mention the need to get the parent in order to access the various buttons on the dialog and change it's size. It would appear that this dialog is the child of some other window, but I haven't pulled out win spy yet to see what it is. When I try to get the window rect directly, the returned value indicates a box of zero size, while the parent call returns legitimate values. Likewise, CallingSetWindowPos with on the parent actually moves the window (not off of the screen, unfortunately), but also adjusts it's size.
-
I don't think it's the documentation for SetWindowPos that is relevant here, but instead the various articles describing how to modify CFileDialog. These articles mention the need to get the parent in order to access the various buttons on the dialog and change it's size. It would appear that this dialog is the child of some other window, but I haven't pulled out win spy yet to see what it is. When I try to get the window rect directly, the returned value indicates a box of zero size, while the parent call returns legitimate values. Likewise, CallingSetWindowPos with on the parent actually moves the window (not off of the screen, unfortunately), but also adjusts it's size.
rentzk wrote:
These articles mention the need to get the parent in order to access the various buttons on the dialog and change it's size.
That does not sound correct, all dialog controls are children of the dialog window not its parent. Perhaps you would be better to create your own dialog which is derived from
CFileDialog
; see here[^] for further information.rentzk wrote:
When I try to get the window rect directly, the returned value indicates a box of zero size, while the parent call returns legitimate values.
Unfortunately I cannot reproduce this eactly as I do not have MFC on my system. However when I tried a similar test via a Win32 application I was able to get the size and position of my dialog, and move it to a different point on the screen independent of its parent. That would suggest to me that the same is possible with an MFC class dialog.
rentzk wrote:
Likewise, CallingSetWindowPos with on the parent actually moves the window (not off of the screen, unfortunately), but also adjusts it's size.
And so it should,
SetWindowPos
should work for any window.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
rentzk wrote:
These articles mention the need to get the parent in order to access the various buttons on the dialog and change it's size.
That does not sound correct, all dialog controls are children of the dialog window not its parent. Perhaps you would be better to create your own dialog which is derived from
CFileDialog
; see here[^] for further information.rentzk wrote:
When I try to get the window rect directly, the returned value indicates a box of zero size, while the parent call returns legitimate values.
Unfortunately I cannot reproduce this eactly as I do not have MFC on my system. However when I tried a similar test via a Win32 application I was able to get the size and position of my dialog, and move it to a different point on the screen independent of its parent. That would suggest to me that the same is possible with an MFC class dialog.
rentzk wrote:
Likewise, CallingSetWindowPos with on the parent actually moves the window (not off of the screen, unfortunately), but also adjusts it's size.
And so it should,
SetWindowPos
should work for any window.Just say 'NO' to evaluated arguments for diadic functions! Ash
It would appear that CFileDialog contains a function called OnInitdone. The base class for this function contains nothing more than a CenterWindow call, which has been taking all of my work and throwing it away. Overriding this function and not centering the dialog has everything working correctly, with the dialog being placed on whatever monitor I wish.