调用System.Management.Automation.Host.Rectangle报bottom不能大于等于top错误
问题原因及修复方法
核心原因
PowerShell主机界面使用左上角为原点(0,0)的坐标系,垂直方向向下y值递增,因此System.Management.Automation.Host.Rectangle要求合法矩形必须满足top < bottom:
top对应矩形最顶部一行的y坐标,取值更小bottom对应矩形最底部一行的y坐标,取值更大
你当前代码里给top赋了更大的数值,bottom赋了更小的数值,完全违反了校验规则,所以会触发报错。
报错提示的混淆点
你看到的报错"bottom" cannot be greater than or equal to "top"属于表述容易误解的情况,实际内部校验逻辑就是不允许top >= bottom,和你的参数情况完全匹配,并不是报错和你的参数相反。
修复代码
调整top和bottom的赋值即可正常运行:
Console.WriteLine("setting rectangles: " + sizex +", " +sizey); // 调整为top取小值,bottom取大值 int screenTop = 1; int screenBottom = sizey; int statusTop = 0; int statusBottom = 1; Console.WriteLine ("screen top bottom; "+screenTop + " < " + screenBottom ); Console.WriteLine ("status top bottom; "+statusTop + " < " + statusBottom ); // Rectangle(int left, int top, int right, int bottom); System.Management.Automation.Host.Rectangle screenWindowRect = new Rectangle(0,screenTop,sizex,screenBottom); System.Management.Automation.Host.Rectangle statusLineRect = new Rectangle(0,statusTop,sizex,statusBottom); Console.WriteLine("rectangles set");
额外说明
官方文档标注的参数顺序没有错误,这个问题既不是代码bug也不是文档错误,属于对界面坐标系使用规则的理解偏差导致的问题。
内容的提问来源于stack exchange,提问作者silicontrip
相关产品推荐
相关产品推荐

