我试图通过PowerShell在窗体上绘制Windows徽标。以下代码将在表单上仅放置一个点。它出什么问题了?
$labels = @(0)*5
for ($i=0;$i -lt 4;$i++)
{ $labels[$i] = new-object system.window.forms.label
$labels[$i].location = new-object system.drawing.point($i+10,5)
$labels[$i].text = $i.tostring()
$main_form.controls.add($labels[$i])
}
}
$main_form.showdialog()
输出只是表格上的一个点。将文本值更改为“ a”只会打印一个a。
您的x和y坐标的Controls.Add
取整方式不正确-x
然后采用y
,但是您将所有控件放置在同一y
坐标上,并且每个控件的x偏移为1像素(您可能$i * 10
不是用表示的$i + 10
),所以它们都互相重叠。
还有很多错别字-例如system.window.forms
-window
而不是windows
,new-object system.drawing.point($i+10,5)
甚至都不起作用(这会产生Method invocation failed because [System.Object[]] does not contain a method named 'op_Addition'.
错误)。值得花一些时间在提交之前测试发布的代码,甚至要从问题中剪切并粘贴它以确保其实际运行,因为您更有可能获得某人的回应!
无论如何,以下对我有用:
Add-Type -AssemblyName "System.Windows.Forms";
Add-Type -AssemblyName "System.Drawing";
$main_form = new-object System.Windows.Forms.Form;
$labels = @();
for( $i=0; $i -lt 5; $i++ )
{
$label = new-object System.Windows.Forms.Label;
$label.BackColor = "Orange";
$label.Location = new-object System.Drawing.Point(10, ($i * 25));
$label.Text = $i.ToString();
$labels += $label;
$main_form.Controls.Add($label);
}
$main_form.ShowDialog();
显示如下形式:
随时根据您的需求进行调整。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句