首页 > 代码库 > wpf 进度条 下拉
wpf 进度条 下拉
<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)
{
}
}
}