storing a windows postion in the registry...
-
I've tried to store the windows location/size in the registry, and it appears to save the right numbers, but when I read them back in and call MoveWindow it seems to change the numbers. Is move window the right function to call for this, or am I saving the wrong postion using by calling GetWindowRect? I'm pretty sure that its not any of the code that is wrong, but rather that I'm not saving the right numbers. thanks tyler.
-
I've tried to store the windows location/size in the registry, and it appears to save the right numbers, but when I read them back in and call MoveWindow it seems to change the numbers. Is move window the right function to call for this, or am I saving the wrong postion using by calling GetWindowRect? I'm pretty sure that its not any of the code that is wrong, but rather that I'm not saving the right numbers. thanks tyler.
There are a few possible issues. 1/ Where are you setting the position 2/ what sort of co-ordinates are you saving ( relative to window or relative to dialog ) ? ClientToScreen will convert your co-ordinates to ones relative to the whole window 3/ Have you checked the values going in and coming out to make sure you're reading them correctly ? 4/ Are you passing them in correctly ( MoveWindow takes a width and height, rather than x2, y2. )
-
I've tried to store the windows location/size in the registry, and it appears to save the right numbers, but when I read them back in and call MoveWindow it seems to change the numbers. Is move window the right function to call for this, or am I saving the wrong postion using by calling GetWindowRect? I'm pretty sure that its not any of the code that is wrong, but rather that I'm not saving the right numbers. thanks tyler.
You may also try with
Get/SetWindowPlacement()
. If you save and restore all its fields you can also restore minimized/maximized state. Cheers Paolo. -
I've tried to store the windows location/size in the registry, and it appears to save the right numbers, but when I read them back in and call MoveWindow it seems to change the numbers. Is move window the right function to call for this, or am I saving the wrong postion using by calling GetWindowRect? I'm pretty sure that its not any of the code that is wrong, but rather that I'm not saving the right numbers. thanks tyler.
I suggest that you should read the topic about Persistent Frames in the book Inside Visual C++ from Kruglinsky from Microsoft Press.
-
I've tried to store the windows location/size in the registry, and it appears to save the right numbers, but when I read them back in and call MoveWindow it seems to change the numbers. Is move window the right function to call for this, or am I saving the wrong postion using by calling GetWindowRect? I'm pretty sure that its not any of the code that is wrong, but rather that I'm not saving the right numbers. thanks tyler.
to save the Window position :
WINDOWPLACEMENT pwp; BOOL ret; ret = GetWindowPlacement(&pwp); CString strBuffer; strBuffer.Format("%i %i %i %i %i %i %i %i %i %i", pwp.flags, pwp.showCmd, pwp.ptMinPosition.x , pwp.ptMinPosition.y , pwp.ptMaxPosition.x , pwp.ptMaxPosition.y , pwp.rcNormalPosition.left , pwp.rcNormalPosition.top , pwp.rcNormalPosition.right , pwp.rcNormalPosition.bottom); AfxGetApp()->WriteProfileString("Settings","windowPos",strBuffer);
to restore the window position :
WINDOWPLACEMENT pwp; CString strBuffer = AfxGetApp ()->GetProfileString("Settings","windowPos"); int cRead = \_stscanf(strBuffer,"%i %i %i %i %i %i %i %i %i %i", &pwp.flags,&pwp.showCmd, &pwp.ptMinPosition.x , &pwp.ptMinPosition.y , &pwp.ptMaxPosition.x , &pwp.ptMaxPosition.y , &pwp.rcNormalPosition.left , &pwp.rcNormalPosition.top , &pwp.rcNormalPosition.right , &pwp.rcNormalPosition.bottom); if (cRead == 10) SetWindowPlacement(&pwp);
-
There are a few possible issues. 1/ Where are you setting the position 2/ what sort of co-ordinates are you saving ( relative to window or relative to dialog ) ? ClientToScreen will convert your co-ordinates to ones relative to the whole window 3/ Have you checked the values going in and coming out to make sure you're reading them correctly ? 4/ Are you passing them in correctly ( MoveWindow takes a width and height, rather than x2, y2. )
ah yeah, MoveWindow takes width and height and not x2, y2. I wish I remembered that. I did this once in MFC though the same way with MoveWindow using GetWindowRect but when looking back through the code I missed the rect.Width(), and rect.Height() instead of just a RECT structure passing the bottom and right. thanks for the help.
-
to save the Window position :
WINDOWPLACEMENT pwp; BOOL ret; ret = GetWindowPlacement(&pwp); CString strBuffer; strBuffer.Format("%i %i %i %i %i %i %i %i %i %i", pwp.flags, pwp.showCmd, pwp.ptMinPosition.x , pwp.ptMinPosition.y , pwp.ptMaxPosition.x , pwp.ptMaxPosition.y , pwp.rcNormalPosition.left , pwp.rcNormalPosition.top , pwp.rcNormalPosition.right , pwp.rcNormalPosition.bottom); AfxGetApp()->WriteProfileString("Settings","windowPos",strBuffer);
to restore the window position :
WINDOWPLACEMENT pwp; CString strBuffer = AfxGetApp ()->GetProfileString("Settings","windowPos"); int cRead = \_stscanf(strBuffer,"%i %i %i %i %i %i %i %i %i %i", &pwp.flags,&pwp.showCmd, &pwp.ptMinPosition.x , &pwp.ptMinPosition.y , &pwp.ptMaxPosition.x , &pwp.ptMaxPosition.y , &pwp.rcNormalPosition.left , &pwp.rcNormalPosition.top , &pwp.rcNormalPosition.right , &pwp.rcNormalPosition.bottom); if (cRead == 10) SetWindowPlacement(&pwp);
thanks a lot, this is much better than what I was doing previously.
-
to save the Window position :
WINDOWPLACEMENT pwp; BOOL ret; ret = GetWindowPlacement(&pwp); CString strBuffer; strBuffer.Format("%i %i %i %i %i %i %i %i %i %i", pwp.flags, pwp.showCmd, pwp.ptMinPosition.x , pwp.ptMinPosition.y , pwp.ptMaxPosition.x , pwp.ptMaxPosition.y , pwp.rcNormalPosition.left , pwp.rcNormalPosition.top , pwp.rcNormalPosition.right , pwp.rcNormalPosition.bottom); AfxGetApp()->WriteProfileString("Settings","windowPos",strBuffer);
to restore the window position :
WINDOWPLACEMENT pwp; CString strBuffer = AfxGetApp ()->GetProfileString("Settings","windowPos"); int cRead = \_stscanf(strBuffer,"%i %i %i %i %i %i %i %i %i %i", &pwp.flags,&pwp.showCmd, &pwp.ptMinPosition.x , &pwp.ptMinPosition.y , &pwp.ptMaxPosition.x , &pwp.ptMaxPosition.y , &pwp.rcNormalPosition.left , &pwp.rcNormalPosition.top , &pwp.rcNormalPosition.right , &pwp.rcNormalPosition.bottom); if (cRead == 10) SetWindowPlacement(&pwp);
Just one thing that you forgot, when you restore the window's position you need to set pwp.length = sizeof(pwp) for anybody else that sees this. Tyler.