[英]Place a normal ProgressBar to the exact location of a ToolStripProgressBar
在C#或VBNET中如何在运行时将正常的ProgressBar控件(或第三方ProgressBar)放置到statusstrip中ToolStripProgressBar的确切位置?
我试过这个,但正常的进度条移动到左上角,我的statusstrip在底部...:
ProgressBar1.Size = ToolStripProgressBar1.ProgressBar.Size
ProgressBar1.Location = ToolStripProgressBar1.ProgressBar.Location
而其他事情也发生了同样的事情:
ProgressBar1.Size = ToolStripProgressBar1.ProgressBar.Bounds.Size
ProgressBar1.Location = ToolStripProgressBar1.ProgressBar.Bounds.Location
有了这个,正常的进度条移动到statusstrip的底角但是大小/位置不准确,我可以看到正常进度条的ToolStripProgressBar1:
ProgressBar1.Size = ToolStripProgressBar1.ProgressBar.Bounds.Size
ProgressBar1.Location = ToolStripProgressBar1.ProgressBar.Parent.Bounds.Location
我不知道你为什么要放置一个控件来覆盖现有的ToolStripProgressBar
,但幸运的是,实现这样的事情很简单。 您可以直接访问托管ProgressBar
通过ProgressBar
属性,使用PointToScreen
正确的方法来获取屏幕坐标的位置ProgressBar
,然后再转换到位置的形式协调版本,并使用你的另一进度。 请注意,您在外面使用的ProgressBar应该将Parent
设置为您的表单:
public Form1(){
InitializeComponent();
//handle the Shown event of your form to ensure
//your toolStripProgressBar1 has been rendered correctly with correct location
Shown += (s,e) => {
//suppose you have a progressbar called progressBar1
progressBar1.Location = PointToClient(toolStripProgressBar1.ProgressBar
.PointToScreen(Point.Empty));
//do this to cover the whole existing toolStripProgressBar1 exactly
progressBar1.Size = toolStripProgressBar1.ProgressBar.Size;
//call this to ensure your progressBar1 lies on top of all other controls of
//your form (of course your statusStrip should be a control of your form)
progressBar1.BringToFront();
};
//We should also handle the SizeChanged event of the form
//because when resizing, the location of the toolStripProgressBar (relatively
// to the form) will change
SizeChanged += (s, e) => {
progressBar1.Location = PointToClient(toolStripProgressBar1.ProgressBar
.PointToScreen(Point.Empty));
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.