Page 1 of 1

Saving window position so it starts in same place again.

Posted: Wed Mar 03, 2004 5:04 am
by RJP Computing
Hi,
This is possibly a very question. I am writing the posions of my program to an ini file before it closes so that I can read them back in agian. I am tring to make sure the window reappears on the screen in the exact location that it shut down in. I am close but it would apear when I use GetWindowRect_ it returns only the window portion not the frame around it. Is there an API call that will give me the exact location of the whole window including the frame and caption.

Thanks

Posted: Wed Mar 03, 2004 5:10 am
by Paul
Using WindowX() and WindowY() doesn't work for you?

Posted: Wed Mar 03, 2004 3:32 pm
by RJP Computing
I give myself a big DUH. I told you it might be really stupid question. :oops:
Thanks Paul.

Posted: Thu Mar 04, 2004 8:22 am
by PB
> I told you it might be really stupid question.

Actually, you didn't -- you left out that word. ;)

Posted: Mon Mar 22, 2004 9:26 pm
by Joey
This is prob too late but I wrote 2 examples of ways of doing it, I guess it could be of use to some people

First using procedures

Code: Select all

Procedure GetXpos()
  appdir$=Space(255)                    
  GetCurrentDirectory_(255,@appdir$)    
    If Right(appdir$,1)<>"\"              
      appdir$+"\" 
    EndIf
    
    If FileSize(""+appdir$+"\Config.ini")=-1 
      Xpos = 100                                 
    EndIf                                                                             

    If FileSize(""+appdir$+"\Config.ini")<>-1 
      OpenPreferences("Config.ini")   
      Xpos = ReadPreferenceLong("Xaxis",0)
      ClosePreferences()
    EndIf
    
    If Xpos < 0         
      Xpos = 100           
    EndIf               
  ProcedureReturn Xpos 
EndProcedure

Procedure GetYpos()
  appdir$=Space(255)                    
  GetCurrentDirectory_(255,@appdir$)    
    If Right(appdir$,1)<>"\"              
      appdir$+"\" 
    EndIf
    
    If FileSize(""+appdir$+"\Config.ini")=-1 
      Ypos = 100                                 
    EndIf                                                                             

    If FileSize(""+appdir$+"\Config.ini")<>-1 
      OpenPreferences("Config.ini")   
      Ypos = ReadPreferenceLong("Yaxis",0)
      ClosePreferences()
    EndIf
    
    If Ypos < 0         
      Ypos = 100           
    EndIf               
  ProcedureReturn Ypos 
EndProcedure

Procedure WriteConfig()
  
    CreatePreferences("Config.ini")
      WritePreferenceLong("Xaxis",WindowX())
      WritePreferenceLong("Yaxis",WindowY())
    ClosePreferences()
EndProcedure 
  
  appdir$=Space(255)                    
  GetCurrentDirectory_(255,@appdir$)

Ypos = GetYpos()
Xpos = GetXpos()

OpenWindow(#1,Xpos,Ypos,195,200,#PB_Window_MinimizeGadget,"TEST - By Joey")

       
       
  Repeat    
        EventID = WaitWindowEvent()        
          
  Until EventID = #PB_Event_CloseWindow 

Writeconfig()



And secondly without using procedures which I have annotated

Code: Select all


appdir$=Space(255)                    
GetCurrentDirectory_(255,@appdir$)    ;Getting directory run from
If Right(appdir$,1)<>"\"              
appdir$+"\" 
EndIf


If FileSize(""+appdir$+"\Config.ini")=-1 
Xpos = 100                                 ;Checking if a config file already exists
Ypos = 100                                 ;If it does not it is setting default co-ordinates
EndIf

If FileSize(""+appdir$+"\Config.ini")<>-1 ;If config file does exist reading longs to give co-ordinates
OpenPreferences("Config.ini")   ;for window to be created at
Xpos = ReadPreferenceLong("Xaxis",xaxis)
Ypos = ReadPreferenceLong("Yaxis",yaxis)


If Xpos < 0         ;This bit is to combat the fact that if app is closed when minimized it will
Xpos = 100          ;give negative co-ordinates, these 2 parts make sure it appears all be it in 
EndIf               ;the default location

If Ypos < 0
Ypos = 100
EndIf

EndIf

OpenWindow(#1,Xpos,Ypos,195,180,#PB_Window_MinimizeGadget,"TEST - By Joey")

Repeat    
EventID = WaitWindowEvent()        
Until EventID = #PB_Event_CloseWindow 


xaxis =WindowX()                        ;This bit records where the window was closed
yaxis =WindowY()
CreatePreferences("Config.ini")
WritePreferenceLong("Xaxis",xaxis)
WritePreferenceLong("Yaxis",yaxis)
ClosePreferences() 





By the way how could I get 2 returns from a procedure and assign each one a different variable. If it's not pos then no probs

Posted: Mon Mar 22, 2004 10:41 pm
by dagcrack
Joey, sharing is always appreciated, thanks ;) I bet its going to be very useful for many ppl arround.

Posted: Mon Mar 22, 2004 11:15 pm
by Paul
ReadPreference commands allow you to set a default value if one is not found so you do not have to do all the file checking. Also, only CreatePreferences needs a check.

Here is a more simple form of your code...

Code: Select all

OpenPreferences("Config.ini")
  Xpos=ReadPreferenceLong("Xaxis",100) 
  Ypos=ReadPreferenceLong("Yaxis",100)   
ClosePreferences()

If Xpos<0:Xpos=100:EndIf
If Ypos<0:Ypos=100:EndIf


If OpenWindow(1,Xpos,Ypos,195,180,#PB_Window_MinimizeGadget,"TEST") 
  Repeat    
    EventID=WaitWindowEvent()        
  Until EventID=#PB_Event_CloseWindow 

 
  If CreatePreferences("Config.ini") 
    WritePreferenceLong("Xaxis",WindowX()) 
    WritePreferenceLong("Yaxis",WindowY()) 
    ClosePreferences() 
  EndIf
EndIf

Posted: Tue Mar 23, 2004 8:39 pm
by Joey
Cheers, I dunno why but your method takes longer to run/compile :?

Besides sorry for mistakes like that I've only been coding for a few weeks.