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
Solved

How do I keep track of a windows location

Posted on 2000-03-02
7
198 Views
Last Modified: 2013-12-26
I am trying to store the location of a window in my preferences using a Rect. Anyone have any sample code of saving a windows location and then placing it in the same spot when you open the window.
0
Comment
Question by:gail_p
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:Alex Curylo
ID: 2578463
Well, immediately before you delete the window you want to save, get its dimensions in global coordinates. In PowerPlant for an LWindow object it'd be something like

mPlaylistWindow->GetGlobalBounds(mPrefs.playlistRect);

(or for a regular WindowPtr, cast it to a WindowPeek and store the port.portRect of the WindowRecord that points to)

where mPrefs is a structure which includes the Rect "playlistRect" and gets saved and loaded however you save and load your preferences, sounds like you're doing that already.

Once you've loaded your preferences, the main trick is to make sure that the bounds saved make sense on the current screen, whose dimensions might have changed in the meantime. Here's what I use in PowerPlant programs, after creating the window but before showing it, to insist that the saved position leave at least 40 pixels showing in each axis on the current screen:

Rect saved, intersection;
Rect screen = (**(GetGrayRgn())).rgnBBox;
saved = mPrefs.playlistRect;
if (!::EmptyRect(&saved))
            if (::SectRect(&saved, &screen, &intersection))
                  if ((intersection.right - intersection.left) > 40)
                        if ((intersection.bottom - intersection.top) > 40)
                              mPlaylistWindow->DoSetBounds(saved);

(if you're not using PowerPlant, you would use MoveWindow() and SizeWindow() on your WindowPtr, deriving the location/size from the saved Rect)

....and that's about all the advice 50 points is worth I think :)

0
 

Author Comment

by:gail_p
ID: 2578590
I had tired the following code and it works fine when I have only one window open but if I have more than one window open the window reopens at 0,0 - under the menu. Any Suggestions. I increased the points by 50 but I only had 200. Thats why I was stingy. ;}

if( gMovedSplashWindow ) {
            myPrefs.prefChanged = true;
            SetPort(&gSplashDialogPtr);
            theWindowPeek = (DocumentPeek)gSplashDialogPtr;

      /*      myPrefs.splashWindRect.top = theWindowPeek->docwindow.port.portRect.top;
            myPrefs.splashWindRect.bottom = theWindowPeek->docwindow.port.portRect.bottom;
            myPrefs.splashWindRect.left = theWindowPeek->docwindow.port.portRect.left;
            myPrefs.splashWindRect.right = theWindowPeek->docwindow.port.portRect.right;
            LocalToGlobal( &topLeft(myPrefs.splashWindRect));
            LocalToGlobal( &botRight(myPrefs.splashWindRect));*/
            
            gWindowPoint.v = theWindowPeek->docwindow.port.portRect.top;
            gWindowPoint.h = theWindowPeek->docwindow.port.portRect.left;
            LocalToGlobal(&gWindowPoint);      
            
            myPrefs.splashWindRect.top = gWindowPoint.v;
            myPrefs.splashWindRect.left = gWindowPoint.h;
            
            gWindowPoint.v = theWindowPeek->docwindow.port.portRect.bottom;
            gWindowPoint.h = theWindowPeek->docwindow.port.portRect.right;
            LocalToGlobal(&gWindowPoint);      
            
            myPrefs.splashWindRect.bottom  = gWindowPoint.v;
            myPrefs.splashWindRect.right = gWindowPoint.h;
      }
0
 
LVL 4

Expert Comment

by:Alex Curylo
ID: 2580840
This is a little verbose; a simple

myPrefs.splashWindRect = theWindowPeek->docwindow.port.portRect;
LocalToGlobal( &topLeft(myPrefs.splashWindRect));
LocalToGlobal( &botRight(myPrefs.splashWindRect));

really ought to work just fine.

First thing I'd do is check the values in splashWindRect, to make sure that 'docwindow' really is valid when you're saving it. Be nice to see your DocumentPeek definition and the code you're using to set the window position too.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 4

Expert Comment

by:Alex Curylo
ID: 2580843
This is a little verbose; a simple

myPrefs.splashWindRect = theWindowPeek->docwindow.port.portRect;
LocalToGlobal( &topLeft(myPrefs.splashWindRect));
LocalToGlobal( &botRight(myPrefs.splashWindRect));

really ought to work just fine.

First thing I'd do is check the values in splashWindRect, to make sure that 'docwindow' really is valid when you're saving it. Be nice to see your DocumentPeek definition and the code you're using to set the window position too.
0
 

Author Comment

by:gail_p
ID: 2582182
I have tried your code but I am still having a problem. I checked to see if the DialogPeek's address is the same as the DialogPtr and it is. Then I checked the contents of the portRect and it is:

top 0
left 0
bottom 140
right 201

this seems to be correct (it is the window size), when I assign this:

myPrefs.splashWindRect = theDialogPeek->window.port.portRect;

everything works but the LocalToGlobal does not change the contents of myPrefs.splashWindRect.

My window opening code looks like this:

gSplashDialogPtr = GetNewDialog( kSplashDialog, nil, (DialogPtr)-1);
      if( gSplashDialogPtr != nil) {
      
            SetPort(gSplashDialogPtr);
            TextFont(3);
          TextSize(9);
          
          MoveWindow(gSplashDialogPtr, myPrefs.splashWindRect.left, myPrefs.splashWindRect.top, true );

            Draw_Splash();      
      }  

I have replaced the values in the MoveWindow and this works. I am confused as to why this doesn't work. It ends up opening the window at 0,0
0
 
LVL 4

Accepted Solution

by:
Alex Curylo earned 50 total points
ID: 2582204
Hmmmm. You didn't get rid of the SetPort() call, did you? Not having the right port set is pretty much the only thing that can go wrong with LocalToGlobal().

I am assuming here that your definition of whatever DocumentPeek points to starts off with the field "WindowRecord docwindow", is that correct?

Oh. Hang on. Duh. I need some sleep :)

That line

SetPort(&gSplashDialogPtr);

Make that

SetPort(gSplashDialogPtr);

and it will suddenly work!

(You pass SetPort a GrafPtr/WindowPtr/DialogPtr ... not the ADDRESS of one of those like you are here.)
0
 

Author Comment

by:gail_p
ID: 2582994
Thanks for all your help, after stepping through the code I found that a function I use to check for changes was disposing of the window I was trying to get the location of if there was more than one window open.

Thanks Again for all your help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
if loop in java 3 152
sum67 challenge 35 107
EvenOdd challenge 10 125
How to convert MFC::CString to UTF8 wchar_t* 10 277
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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