# How to save window position on frame move?

**URL:** <https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226>\
**Category:** wxRuby\
**Created:** [April 20, 2010, 6:26am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226 "2010-04-20T06:26:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 20, 2010, 6:26am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/1 "2010-04-20T06:26:08Z")

</div>

Dear All,

I want my app to save it’s window position  
so next time I run it, the window will place at the last position  
But I cannot use close event to save position  
since it is a tray application

I plan to use on move window  
but cannot seem to find any documentation about it

Any suggestion?

Regards  
Hendra

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 20, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/2 "2010-04-20T17:29:26Z")

</div>

It’s easy to save the position of your window. And technically, you can  
use  
the onClose event, even with it being a Tray App.

The method in which this works, is that you would need to return false  
from  
your window to prevent the window from being destroyed.

An example of this:

class MyWin \< Wx::Frame  
def initialize()  
super(nil,-1,“Sample Hide, not close”)  
evt\_close(self,:on\_close)  
end

def on\_close(event)  
@pos = self.position  
self.hide  
return false  
end  
end

HTH,

Mario

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 21, 2010, 4:31am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/3 "2010-04-21T04:31:00Z")

</div>

I already know how to save the position  
but the reason for me not to use on\_close event is  
because I am making a simple and personal desktop calendar  
with todo and event reminder  
and I am not likely to close it before I shutdown my computer  
therefore I need another event to save it

what comes in my mind is an event when a window is moved  
but I cannot seem to find it  
any other idea?

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 21, 2010, 8:19am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/4 "2010-04-21T08:19:52Z")

</div>

Hi Hendra

On 21/04/2010 02:59, penguinroad wrote:

> I already know how to save the position  
> but the reason for me not to use on\_close event is  
> because I am making a simple and personal desktop calendar  
> with todo and event reminder  
> and I am not likely to close it before I shutdown my computer  
> therefore I need another event to save it

Maybe Wx::ActivateEvent -  
[http://wxruby.rubyforge.org/doc/activateevent.html](http://wxruby.rubyforge.org/doc/activateevent.html)

You could use the evt\_activate\_app to detect when the whole application  
loses focus, and serialise the window state then. I have not tried this  
with tray-based apps, but there is a sample in etc/ that you could have  
a look at and try easily.

best  
alex

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 22, 2010, 4:02am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/5 "2010-04-22T04:02:01Z")

</div>

On Wed, Apr 21, 2010 at 1:01 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > therefore I need another event to save it

Thank you for the pointer  
I found Wx::MoveEvent  
and it works exactly as I need  
Thank you

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 22, 2010, 8:23am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/6 "2010-04-22T08:23:40Z")

</div>

Well, thats exacly what I do  
write 2 lines to a file to save x and y position of my window  
I don’t know it it occurs frequently  
but somehow it do just fine

thanks

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 22, 2010, 8:13am UTC](https://www.ruby-forum.com/t/how-to-save-window-position-on-frame-move/187226/7 "2010-04-22T08:13:07Z")

</div>

On 22/04/2010 03:01, hendra kusuma wrote:

> Thank you for the pointer  
> I found Wx::MoveEvent  
> and it works exactly as I need

Good, I’m glad it works. I was cautious about suggesting MoveEvent  
because it occurs frequently when a Frame is, say, being dragged about  
by its title bar. So it’s not desirable to do lengthy operations (e.g.  
write to a File) each time it occurs.

best  
alex
