约束窗口wpf

发布于 2024-10-16 20:17:00 字数 360 浏览 4 评论 0原文

我有一个窗口需要限制在另一个窗口内。为此, 我挂接到顶层窗口上的 SizeChanged 事件......在这种情况下,我需要调整第二个窗口,以便仅当两个窗口之间存在交叉点时(即,如果较小的窗口得到),它才与最近的边缘对齐在较大窗口的边界之外。 我做了很多数学计算来得到这个......但我仍然没有接近解决方案!

我在做这件事时遇到了困难,因为它涉及很多混乱的代码,我想知道你们中是否有人有更简单的解决方案?

基本上我正在处理 2 个矩形,我需要确保当较大矩形的大小发生变化时...如果两者之间存在交集,则较小的矩形应将自身与较大矩形的边缘对齐,以便较小的矩形矩形在更大的矩形内。

可能是 C# 形式的简单数学问题?

有什么建议欢迎欢迎谢谢!

I have a window that needs to be constrained within another window. In order to do this,
I hook into the SizeChanged event on the top level window....and in that event I need to adjust the second window so that it is aligned to the nearest edge only if there is an intersection between the two i.e if the smaller window gets outside the boundary of the bigger window.
I do a lot of math calculation to get this...and im still not near to the solution!

Im having trouble doing this because it involves a lot of messy code I was wondering if any of you guys had an easier solution to this?

Basically im dealing with 2 rectangles and I need to ensure that when the size of the bigger rectangle changes...if there is an intersection between the two, then the smaller rectangle should align itself to the edge of the bigger rectangle so that the smaller rectangle is within the bigger rectangle.

Could be a simple math problem in C# forms?

Any suggestions are welcome thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-10-23 20:17:00

对于这两个窗口,您需要获取系统坐标中窗口位置的 x 和 y 坐标。

如何在 wpf 中执行此操作可以在此处找到 http://blogs.msdn.com/b/llobo/archive/2006/05/02/code-for-getting-screen-relative-position-in-wpf.aspx

接下来,您需要让两个窗口对彼此的 sizechanged 事件做出反应,以便当另一个窗口的大小发生更改时,一个窗口会收到通知。

那么下面的数学就可以完成这项工作:
(假设窗口 1 当前位于窗口 2 的边界内,并且窗口 2 大小发生变化,并且您希望实际调整窗口大小而不是在可能的情况下移动它)

//PSEUDOCODE
//Case1 (left bound changes)
if(window2.x > window1.x)
{
    window1.x = window2.x;
}
//Case2 (top bound changes)
if(window2.y > window1.y)
{
    window1.y = window2.y;
}
//Case3 (right bound changes)
if(window2.x + window2.width < window1.x + window1.width)
{
    window1.width = window2.x + window2.width - window1.x;
}
//Case4 (bottom bound changes)
if(window2.y + window2.height < window1.y + window1.height)
{
    window1.height = window2.y + window2.height - window1.y;
}        

For both windows you need to get the x and the y coordinates of the location of the window in systemcoordinates.

How to do this in wpf can be found here http://blogs.msdn.com/b/llobo/archive/2006/05/02/code-for-getting-screen-relative-position-in-wpf.aspx

Next you need to have the two windows react on each others sizechanged events so that the one window gets notified when the others size has changed.

Then the following math will do the job:
(assuming window 1 is currently in the bounds of window 2 and window 2 size changes and you want to actually resize the window instead of moving it when possible)

//PSEUDOCODE
//Case1 (left bound changes)
if(window2.x > window1.x)
{
    window1.x = window2.x;
}
//Case2 (top bound changes)
if(window2.y > window1.y)
{
    window1.y = window2.y;
}
//Case3 (right bound changes)
if(window2.x + window2.width < window1.x + window1.width)
{
    window1.width = window2.x + window2.width - window1.x;
}
//Case4 (bottom bound changes)
if(window2.y + window2.height < window1.y + window1.height)
{
    window1.height = window2.y + window2.height - window1.y;
}        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文