我需要在“矩形”的形式排序窗口。这意味着当我有6个窗口时,它以2x3的矩形排序,当我有5个窗口时,它以2x3排序,但没有最后一个窗口,当我有9个窗口时,它以3x3排序。但是我在坐标方面遇到了一些麻烦--子窗口超出了mdiparent窗口的界限。
我使用的算法与我在java上的mdi应用程序中使用的算法相同。
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
try{
indfr.get(counter).setLocation(i*theDesktop.getWidth()/a,j*theDesktop.getHeight()/b);
indfr.get(counter).setSize(theDesktop.getWidth()/a,theDesktop.getHeight()/b);
counter++;
}catch (IndexOutOfBoundsException exc){ break;}其中,indfr - arralist of JInternalFrames和theDesktop - JDesktopPane
c#中的算法
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
try
{
list[counter].SetDesktopLocation(i*list[counter].MdiParent.Width/a, j*list[counter].MdiParent.Height/b);
list[counter].Size = new Size(list[counter].MdiParent.Width/a, list[counter].MdiParent.Height/b);
counter++;
}
catch (IndexOutOfRangeException)
{
break;
}where list - Form[] list = this.MdiChildern;
坐标有什么问题?(P.S它不是整个算法,但它是窗口排序的主循环)

发布于 2012-05-15 07:16:25
有问题的行是try块中的行:
list[counter].SetDesktopLocation(i*list[counter].MdiParent.Width/a, j*list[counter].MdiParent.Height/b);
list[counter].Size = new Size(list[counter].MdiParent.Width/a, list[counter].MdiParent.Height/b);
counter++;您正在检查Form.Width和Form.Height,它们返回表单在屏幕上的总大小,包括所有边框。您只能将子窗口放置在父窗体的工作区内,因此需要改为查询ClientSize property。它被定义为窗体的大小,减去边框和标题栏;换句话说,窗体中可以放置子对象的区域。
将您的try块重写为以下代码:
list[counter].SetDesktopLocation(i*list[counter].MdiParent.ClientSize.Width/a, j*list[counter].MdiParent.ClientSize.Height/b);
list[counter].Size = new Size(list[counter].MdiParent.ClientSize.Width/a, list[counter].MdiParent.ClientSize.Height/b);
counter++;然后去掉那个愚蠢的空catch块。如果在抛出异常时所做的一切都是breaking,那么捕获异常就没有意义了。它将冒泡到下一个异常处理程序,如果需要的话,一直到全局异常处理程序。只捕获你特别知道如何处理的异常。你不应该得到一个IndexOutOfRangeException,如果你得到了,那是你的代码中的一个bug -你想知道它,这样你就可以修复它。这意味着不要接受异常。

但是,如果我可以在这里引导Clippy,它看起来像是你试图磁贴你的MDI子。
在这种情况下,有一种比编写一堆for循环并手动设置孩子的大小和位置更容易的方法。相反,只需调用父MDI窗体上的Form.LayoutMdi method,并指定其中一个MdiLayout enumeration values即可。在这种情况下,您可能需要MdiLayout.TileHorizontal或MdiLayout.TileVertical。
WinForms会自动精确地按照你想要的方式排列你的子窗口。
发布于 2012-05-15 07:26:02
您指定要为5个窗体“没有最后一个窗口”布局2x3,我认为这意味着您希望在最后一个窗体正常出现的地方留出一个空白空间?如果是这样的话,这对您没有什么帮助,但是如果这对您来说并不重要,那么您可以只使用.NET Windows Forms的标准部分Form.LayoutMDI方法:
LayoutMdi(MdiLayout.TileVertical);不同的是,在上面的场景中,它不会留下空白空间-为了利用空白空间,两个窗口将显示得更大。不管怎样,试一试,看看它对你有没有帮助。
https://stackoverflow.com/questions/10592190
复制相似问题