如何使progressbar放在屏幕中间(backgroundworker搭配使用)

backgroundworker组件是winform提供的很好用的进度条信息组件,启用线程工作,需要使用progressbar。现有一个项目需要主界面工作的时候使用progressbar组件,在窗体中间显示进度条。通俗易懂的方法如下:
原文链接:https://blog.csdn.net/weixin_42599077/article/details/106012614

窗体的load事件设置组件位置

组件的位置:组件左上角和主窗体组件左上角的位置
窗体加载的时候需要隐藏进度条和标签,使用的时候可以显示

        private void mainform_Load(object sender, EventArgs e)
        {
            int progressBar1_x = (int)(0.5 * (this.Width - progressBar1.Width));
            int progressBar1_y = (int)(0.5*(this.Height - progressBar1.Height));
            int label1_x = (int)(0.5 * (this.Width - progressBar1.Width));
            int label1_y = (int)(0.485 * (this.Height - progressBar1.Height - label1.Height));
            progressBar1.Location = new Point(progressBar1_x,progressBar1_y);
            label1.Location = new Point(label1_x,label1_y);
            //progressBar1.Visible = false;
            //label1.Visible = false;
        }

注册Resize事件

这个事件用于方法缩小主窗体时,progressbar仍然处在屏幕中间

		private void InitializeComponent()
  		{
			this.Resize += new System.EventHandler(this.progressbarResize);
		}

        private void progressbarResize(object sender, EventArgs e)
        {
            int progressBar1_x = (int)(0.5 * (this.Width - progressBar1.Width));
            int progressBar1_y = (int)(0.5 * (this.Height - progressBar1.Height));
            int label1_x = (int)(0.5 * (this.Width - progressBar1.Width));
            int label1_y = (int)(0.485 * (this.Height - progressBar1.Height - label1.Height));
            progressBar1.Location = new Point(progressBar1_x, progressBar1_y);
            label1.Location = new Point(label1_x, label1_y);
        }

效果图

全屏图:
![Alt]

缩小图:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章