简体   繁体   English

设置窗口位置

[英]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? 设置Left会自动触发要刷新的事件,还是等待OnRender事件?

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. 我知道Windows API有SetWindowPos ,但我宁愿远离非托管代码。 Would using this function be faster? 使用此功能会更快吗?

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. 这是因为LeftTop依赖项属性将PropertyChangedCallback实例设置为OnPositioningChanged方法。 This method calls InvalidateArrange . 此方法调用InvalidateArrange From the link: 从链接:

Frequent calls to InvalidateArrange or in particular to UpdateLayout have significant performance consequences. 频繁调用InvalidateArrange或特别是对UpdateLayout的调用会产生重大的性能影响。

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). 无论你是使用非托管路线还是坚持使用当前的方法,我都认为必须在某个时刻进行相同的渲染调用(很高兴能够对此进行更正)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM