Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF中的动态进度条_C#_Wpf_Backgroundworker - Fatal编程技术网

C# WPF中的动态进度条

C# WPF中的动态进度条,c#,wpf,backgroundworker,C#,Wpf,Backgroundworker,编辑文章: 我需要在WPF中使用progressbar。 ProgressBar.Maximum应该是动态的,由DataGrid中的行数填充。 我想在函数CreationBT()之后增加progressbar值 做后台工作是做我想做的事情的好方法吗 这是我的密码: 命名空间虚拟ouin { 公共静态类DataGridExtensions { 公共静态T GetVisualChild(可视父级),其中T:VisualChild { T child=默认值(T); int numVisuals=Vi

编辑文章:

我需要在WPF中使用progressbar。 ProgressBar.Maximum应该是动态的,由DataGrid中的行数填充。 我想在函数CreationBT()之后增加progressbar值

做后台工作是做我想做的事情的好方法吗

这是我的密码:

命名空间虚拟ouin {

公共静态类DataGridExtensions
{
公共静态T GetVisualChild(可视父级),其中T:VisualChild
{
T child=默认值(T);
int numVisuals=VisualTreeHelper.GetChildrenCount(父级);
对于(int i=0;ipublic static class DataGridExtensions
{
    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
    public static DataGridRow GetSelectedRow(this DataGrid grid)
    {
        return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
    }
    public static DataGridRow GetRow(this DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }
    public static DataGridCell GetCell(this DataGrid grid, int row, int column)
    {
        DataGridRow rowContainer = grid.GetRow(row);
        return grid.GetCell(rowContainer, column);
    }
}


public static class SendKeys
{
    /// <summary>
    ///   Sends the specified key.
    /// </summary>
    /// <param name="key">The key.</param>
    public static void Send(Key key)
    {
        if (Keyboard.PrimaryDevice != null)
        {
            if (Keyboard.PrimaryDevice.ActiveSource != null)
            {
                var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Down) { RoutedEvent = Keyboard.KeyDownEvent };
                InputManager.Current.ProcessInput(e1);
            }
        }
    }
}


public partial class MainWindow : Window
{
    BackgroundWorker worker = new BackgroundWorker();

    public MainWindow()
    {
        InitializeComponent();
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool SetCursorPos(int x, int y);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;



    public static void LeftMouseClick(int xpos, int ypos)
    {
        SetCursorPos(xpos, ypos);
        mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
    }

    public struct Ligne
    {
        public string Rubrique { set; get; }
        public string Equipement { set; get; }
        public string Etat { set; get; }
        public string Organe { set; get; }
        public string Etat_Org { set; get; }
        public string Destinataire { set; get; }
        public string Intervenant { set; get; }
        public string Commentaire { set; get; }
        public string Date_Inter { set; get; }
        public string Heure { set; get; }
        public string Diag { set; get; }
        public string Remede { set; get; }
        public string Temps { set; get; }
        public string Obs { set; get; }
    }

    private void remplir_data()
    {

        OpenFileDialog fichier = new OpenFileDialog();
        if (fichier.ShowDialog() == true) // Test result.
        {
            string file = fichier.FileName;

            if (file.Contains(".txt"))
            {
                StreamReader sr = new StreamReader(file);
                while (!sr.EndOfStream)
                {
                    string maLigne = sr.ReadLine();
                    DGV.Items.Add(new Ligne
                    {
                        Rubrique = maLigne.Split(';')[0],
                        Equipement = maLigne.Split(';')[1],
                        Etat = maLigne.Split(';')[2],
                        Organe = maLigne.Split(';')[3],
                        Etat_Org = maLigne.Split(';')[4],
                        Destinataire = maLigne.Split(';')[5],
                        Intervenant = maLigne.Split(';')[6],
                        Commentaire = maLigne.Split(';')[7],
                        Date_Inter = maLigne.Split(';')[8],
                        Heure = maLigne.Split(';')[9],
                        Diag = maLigne.Split(';')[10],
                        Remede = maLigne.Split(';')[11],
                        Temps = maLigne.Split(';')[12],
                        Obs = maLigne.Split(';')[13],
                    });
                }
                sr.Close();
            }
            else MessageBox.Show("Selectionnez un fichier compatible");
        }

    }

    private void BT_REMPLIRDATA_Click(object sender, EventArgs e)
    {
        this.DGV.Items.Clear();
        remplir_data();  
    }

    private void BT_QUITTER_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    public void Creation_BT(int i)
    {
        int x = 150;
        int y = 236;

        DataGridCell dataGridCell = null;

        LeftMouseClick(x, y);
        dataGridCell = this.DGV.GetCell(i, 0);
        TextBlock cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 25);
        dataGridCell = DGV.GetCell(i, 1);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 45);
        dataGridCell = DGV.GetCell(i, 2);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 65);
        dataGridCell = DGV.GetCell(i, 3);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 85);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 4);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 175);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 5);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 200);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 6);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 255);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 7);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        System.Threading.Thread.Sleep(300);
        LeftMouseClick(x, y - 50);

        LeftMouseClick(x, y);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 8);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 100);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 8);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x + 60, y + 100);
        System.Threading.Thread.Sleep(300);
        dataGridCell = DGV.GetCell(i, 9);
        cell = dataGridCell.Content as TextBlock;
        InputSimulator.SimulateTextEntry(cell.Text);

        System.Threading.Thread.Sleep(300);
        LeftMouseClick(100, 580);
        System.Threading.Thread.Sleep(500);

        LeftMouseClick(x + 50, y - 100);
        System.Threading.Thread.Sleep(300);
        LeftMouseClick(x, y - 100);
        System.Threading.Thread.Sleep(500);
        LeftMouseClick(x, y - 100);
        InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
        InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_C);

        System.Threading.Thread.Sleep(300);

        InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
        InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_C);
    }

    private void Creation_ACTIVITE(int i, string BT)
    {
        int x = 150;
        int y = 236;
        DataGridCell dataGridCell = null;

        LeftMouseClick(x + 250, y - 150);
        System.Threading.Thread.Sleep(300);

        LeftMouseClick(x, y - 20);
        InputSimulator.SimulateTextEntry(BT);

        LeftMouseClick(x, y + 100);
        dataGridCell = DGV.GetCell(i, 10);
        TextBlock cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 120);
        dataGridCell = DGV.GetCell(i, 11);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 190);
        dataGridCell = DGV.GetCell(i, 8);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x + 70, y + 190);
        dataGridCell = DGV.GetCell(i, 9);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x + 270, y + 190);
        dataGridCell = DGV.GetCell(i, 12);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x, y + 240);
        dataGridCell = DGV.GetCell(i, 13);
        cell = dataGridCell.Content as TextBlock;
        System.Threading.Thread.Sleep(300);
        InputSimulator.SimulateTextEntry(cell.Text);

        LeftMouseClick(x - 100, y + 300);

        System.Threading.Thread.Sleep(500);
        LeftMouseClick(x + 150, y + 300);

        System.Threading.Thread.Sleep(500);
        LeftMouseClick(x + 330, y + 370);

    }

    public void BT_REMPLIRBT_Click(object sender, RoutedEventArgs e)
    {

        int nbligne = DGV.Items.Count - 1;
        PGB.Maximum = nbligne;
        for (int i = 0; i <= nbligne; i++)
        {
            string BT = null;
            Creation_BT(i);
            // Need here to increment my progress bar
            System.Threading.Thread.Sleep(1000);                

            IDataObject ClipData = Clipboard.GetDataObject();

            if (ClipData.GetDataPresent(DataFormats.Text))
            {
                BT = Clipboard.GetData(DataFormats.Text).ToString();
                File.AppendAllText(@"D:\test.txt", BT + "\r\n");
            }

            Creation_ACTIVITE(i, BT);
            // Need here to increment my progress bar

            System.Threading.Thread.Sleep(1000);
        }
        // Need here to complete my progress bar 
    }
PGB.Maximum = max;
// In Windows Forms:
Invoke(new Action(() => { PGB.Maximum = max; }));

// In WPF:
Dispatcher.Invoke(() => PGB.Maximum = max)