如何使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]

縮小圖:
在這裏插入圖片描述

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