Kirk Backus
Kirk Backus

Reputation: 4866

Setting The Window Position

Some Background

I am writing an application that moves a multitude of windows on the screen which demands a real-time constraint. I normally set the window positions using the following format:

this.Left = position.x;
this.Top = position.y;

The Question

What I would like to know is if this is the fastest, or most efficient way to do this. Does setting the Left automatically fire off an event to refresh, or does it wait for an OnRender event?

Secondly, why isn't there a function to simply set the position altogether? Or is this even necessary? I am aware that the Windows API has SetWindowPos, but I would prefer to stay away from unmanaged code. Would using this function be faster?

Upvotes: 2

Views: 142

Answers (1)

keyboardP
keyboardP

Reputation: 69372

I'm not sure if it's the most efficient but calling it multiple times may have performance issues. The reason for this is because the Left and Top dependency properties have a PropertyChangedCallback instance set to the OnPositioningChanged method. This method calls InvalidateArrange. From the link:

Frequent calls to InvalidateArrange or in particular to UpdateLayout have significant performance consequences.

I know it's an obvious answer but the best way would be to benchmark both methods and see which one works for you. Regardless of whether you go the unmanaged route or stick with your current method, I imagine the same rendering calls will have to be made at some point (happy to be corrected on that).

Upvotes: 3

Related Questions