将正常的ProgressBar放置到ToolStripProgressBar的确切位置

时间:2013-12-14 18:06:06

标签: c# .net vb.net winforms progress-bar

在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

1 个答案:

答案 0 :(得分:2)

我不知道你为什么要放置一个控件来覆盖现有的ToolStripProgressBar,但幸运的是,实现这样的事情很简单。您只需通过ProgressBar属性访问托管的ProgressBar,正确使用PointToScreen方法获取ProgressBar的屏幕坐标位置,然后将该位置转换为表格坐标版本并将其用于您的另一个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));
  };
}