博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf 进度条 下拉
阅读量:6956 次
发布时间:2019-06-27

本文共 5622 字,大约阅读时间需要 18 分钟。

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="322" Width="525">
    <Grid Height="218" Margin="0,3" HorizontalAlignment="Left" Width="517">
        <Grid.RowDefinitions>
            <RowDefinition Height="105*"/>
            <RowDefinition Height="113*"/>
        </Grid.RowDefinitions>
        <Rectangle Height="28" HorizontalAlignment="Left" Margin="64,24,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="0" />
        <Label Content="进度:" Height="28" HorizontalAlignment="Left" Margin="12,26,0,0" Name="label1" VerticalAlignment="Top" />
        <Button Content="开 始" Height="23" HorizontalAlignment="Left" Margin="206,123,0,-33" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" Grid.Row="1" />
        <Label Content="0%" Margin="71,24,0,53" Name="label2" HorizontalAlignment="Left" Width="46" />
        <TextBox TextWrapping ="Wrap" HorizontalAlignment="Left" Margin="12,57,0,10" Name="textBox1" Width="421"  Grid.RowSpan="2" TextChanged="textBox1_TextChanged" />
    </Grid>
</Window>

 

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Data;
using System.Data.SqlClient;
namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    //public partial class MainWindow : Window
    //{
    //    public MainWindow()
    //    {
    //        InitializeComponent();
    //    }
    //    private void button1_Click(object sender, RoutedEventArgs e)
    //    {
    //    }
    //    private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    //    {
    //    }
    //}
 
    public partial class MainWindow : Window
    {
      public  static double percent = 0;
      public static   int counts = 0;
      public static  int cc = 0;
      public static List<Menu> list = null;
      public static List<Menu> queryProgress()
      {
          SqlConnection conn = new SqlConnection();
          conn.ConnectionString = "Data Source=.;Initial Catalog=CompanyGZ;Integrated Security=True";
          SqlCommand cmd = new SqlCommand();
          string sql = "SELECT * FROM Menu;";
          cmd.Connection = conn;
          cmd.CommandText = sql;
          conn.Open();
          SqlDataAdapter adp = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          adp.Fill(ds);
          DataTable dt = ds.Tables[0];
          list = new List<Menu>();
          list = DataTable2List(dt);
          counts = int.Parse(ds.Tables[0].Rows.Count.ToString());
          return list;
      }
      private static List<Menu> DataTable2List(DataTable dt)
      {
          if (dt.Rows.Count > 0)
          {
              List<Menu> list = new List<Menu>();
              Menu model = null;
              foreach (DataRow dr in dt.Rows)
              {
                  model = new Menu();
                  LoadEntityData(model, dr);
                  list.Add(model);
              }
              return list;
          }
          return null;
      }
      public static void LoadEntityData(Menu model, DataRow dr)
      {
          if (dr.Table.Columns.Contains("mId") && !dr.IsNull("mId"))
          {
              model.Mid = int.Parse(dr["mId"].ToString());
          }
          model.MName = dr["mName"].ToString();
          model.MIsDel = Convert.ToBoolean(dr["mIsDel"]);
          model.MAddtime = Convert.ToDateTime(dr["mAddtime"]);
      }
        private delegate int BrushProcessHandle();
        public MainWindow()
        {
            InitializeComponent();
        }
        Thickness tmpT;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            queryProgress();
            this.rectangle1.Width = 0;
            i = 1;
            if (tmpT.Left == 0 && tmpT.Top == 0 && tmpT.Right == 0 && tmpT.Bottom == 0)
            {
                tmpT = this.label2.Margin;
            }
            else
            {
                this.label2.Margin = tmpT;
            }
            this.textBox1.Text = "do working...";
            // 注释此句将百分比将会一直在进度条头部显示
            this.label2.Margin = new Thickness(this.label2.Margin.Left - 52, this.label2.Margin.Top, this.label2.Margin.Right, this.label2.Margin.Bottom);
            // 取消注释字体颜色为白色
            //this.label2.Foreground = Brushes.White;
            // 取消注释字体为斜体
            //this.label2.FontStyle = FontStyles.Italic;
            BrushProcessBar();
           
        }
        private DispatcherTimer dt;
        private int i = 1;
        private void BrushProcessBar()
        {
            dt = new DispatcherTimer();
            dt.Interval = new TimeSpan(10000);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
        }
        protected void dt_Tick(object sender, EventArgs e)
        {
            // 纯色进度条
            //SolidColorBrush scb = new SolidColorBrush();
            //scb.Color = Color.FromRgb(63, 134, 231);
            // 渐变进度条
            LinearGradientBrush scb = new LinearGradientBrush(Color.FromRgb(63, 169, 233), Color.FromRgb(16, 111, 197), 0);
            this.rectangle1.Width = i++;
            this.label2.Margin = new Thickness(this.label2.Margin.Left + 1, this.label2.Margin.Top, this.label2.Margin.Right, this.label2.Margin.Bottom);
            this.rectangle1.Fill = scb;
            //if (i == 50 || i == 120 || i == 410)
            //{
            //    Thread.Sleep(200);
            //}
            //this.label2.Content = Decimal.Round(((decimal)i) / 400 * 100, 2) + "%";//i == 400
            if (percent == 100)
            {
                dt.Stop();
              //  this.label2.Visibility =Visibility.Hidden;
                this.textBox1.Text += "\r\nCompleted";
            }
            else
            {
               
                if (cc <= counts)
                {
                   cc=cc+1;
                }
                textBox1.Text = "do working...";
                for (int j = 0; j < 6; j++)
                {
                    if (cc + j >= counts)
                    {
                      
                        percent = 100;
                        this.label2.Content = ("" + Convert.ToInt16(percent) + "%");
                        return ;
                    // Decimal.Round(((cc)) / counts * 100, 2) + "%";//i == 400
                    }
                    textBox1.Text += "\n"+list[cc+j].Mid + ":" + list[cc+j].MName + ":" + list[cc+j].MIsDel + list[cc+j].MAddtime+"\n";
                    percent =Convert.ToDouble( cc )/ counts*100;
                    this.label2.Content =(""+Convert.ToInt16( percent)+ "%");//i == 400
                  
                }
           //this.textBox1.Text+=  
                if (percent == 100) return;
            }
     
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
      
    }
}

 

转载于:https://www.cnblogs.com/kexb/p/3699267.html

你可能感兴趣的文章
sublime text笔记
查看>>
MySQL中,修改表的某一字段的部分值
查看>>
Java TreeMap 源码解析
查看>>
在Ubuntu 12.04 上编译调试 OpenJDK8
查看>>
WPF一段代码搞定所有的点击事件
查看>>
Mac上同步git遇到N多坎坷
查看>>
kettle-自定义常量数据->删除
查看>>
第十二周项目3-用递归方法求解(二)
查看>>
php中base64_decode与base64_encode加密解密函数
查看>>
127. Word Ladder
查看>>
Node.js权威指南 (8) - 创建HTTP与HTTPS服务器及客户端
查看>>
CSharp设计模式读书笔记(6):建造者模式(学习难度:★★★★☆,使用频率:★★☆☆☆)...
查看>>
Docker_1 安装Docker-CE
查看>>
shell 变量赋值与替换
查看>>
Codeforces Round #469 (Div. 2)C. Zebras
查看>>
haproxy教程
查看>>
保存列表中的托管元数据,TaxonomyField 字段类型
查看>>
ado模版不会自动生成
查看>>
Qt for Android (5.7)
查看>>
当出现“在当前位置发现已经存在为"RESUME.XLW"的文件。您希望将该文件替换掉吗?” 解决方案...
查看>>