Saving window position so it starts in same place again.

Just starting out? Need help? Post your questions and find answers here.
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Saving window position so it starts in same place again.

Post 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
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Using WindowX() and WindowY() doesn't work for you?
Image Image
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

I give myself a big DUH. I told you it might be really stupid question. :oops:
Thanks Paul.
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I told you it might be really stupid question.

Actually, you didn't -- you left out that word. ;)
Joey
New User
New User
Posts: 7
Joined: Thu Mar 18, 2004 10:09 pm

Post 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
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Joey, sharing is always appreciated, thanks ;) I bet its going to be very useful for many ppl arround.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post 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
Last edited by Paul on Tue Mar 23, 2004 11:00 pm, edited 1 time in total.
Image Image
Joey
New User
New User
Posts: 7
Joined: Thu Mar 18, 2004 10:09 pm

Post 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.
Post Reply