【问题标题】:Place a normal ProgressBar to the exact location of a ToolStripProgressBar将普通 ProgressBar 放置到 ToolStripProgressBar 的确切位置
【发布时间】:2013-12-14 18:06:06
【问题描述】:

在 C# 或 VBNET 中,如何在运行时将普通 ProgressBar 控件(或第 3 方 ProgressBar)放置到状态条内 ToolStripProgressBar 的确切位置?

我试过了,但是正常的进度条移动到左上角,我的状态条在底部...:

ProgressBar1.Size = ToolStripProgressBar1.ProgressBar.Size
ProgressBar1.Location = ToolStripProgressBar1.ProgressBar.Location

其他情况也会发生同样的事情:

ProgressBar1.Size = ToolStripProgressBar1.ProgressBar.Bounds.Size
ProgressBar1.Location = ToolStripProgressBar1.ProgressBar.Bounds.Location

这样,正常进度条移动到状态条所在的底角,但大小/位置不准确,我可以看到正常进度条后面的 ToolStripProgressBar1:

ProgressBar1.Size = ToolStripProgressBar1.ProgressBar.Bounds.Size
ProgressBar1.Location = ToolStripProgressBar1.ProgressBar.Parent.Bounds.Location

【问题讨论】:

  • 为什么必须在运行时?在状态栏中留出一个大空间(使用固定大小的标签),在该空间上画一个米,将锚点设置为左下角。它不会是状态栏或内部布局面板的“成员”,但应该通过移动和调整大小保持原位。它必须在任何动态状态栏的左侧

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


【解决方案1】:

我不知道你为什么要放置一个控件来覆盖现有的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));
  };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    相关资源
    最近更新 更多