WPF小试牛刀

  想法很简单, 做一个程序, 可以同时启动若干个程序的...

  用C++做, 实现功能应该挺快, 但界面显示, 实在不敢保证, Qt还没用熟, 想来用C#要比用熟Qt快一些, 于是决定用WPF做

  昨晚折腾了几个小时, 逐渐摸熟WPF的基本流程, 发现跟Java很像啊, 以后在windows上得学学C#, 果然如小吴所说, 是windows上面的程序制作利器

 

  哈哈, 上面的一个ico, 是来自BYHH的ID为yanhuang的某网友, 当初在网上说说要做这个, 他说做一个挺好, 嗯, 昨晚本来想三国杀一下的, 想想, 做点事吧

  先上效果图, 其实很挫, 很多可以完善

  嗯, 然后上源码, 程序灰常简单, 都不值一提了

Info.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QQ_Byhh
{
    class Info
    {
        public string path;
        public string Path
        {
            get { return path; }
            set { path = value; }
        }

    }
}
MainWindow.xaml文件

<Window x:Class="QQ_Byhh.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="QQ_Byhh" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <ListView Name="ProgramList" MinWidth="280" ItemsSource="{Binding Path=mPath}">
            <ListView.View>
                <GridView x:Name="ProgramGrid">
                    <GridViewColumn Header="ProgramPath"  Width="400" DisplayMemberBinding="{Binding Path=Path}"></GridViewColumn>
                    <GridViewColumn Header="Operate" Width="99">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Delete"  Width="75" Height="23" Click="Button_Click" CommandParameter="{Binding Path=Path}"/>                              
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        
        <Button Grid.Row="1" HorizontalAlignment="Right" Name="Add" Margin="0,0,350,0" Click="Add_Click">Add Program</Button>
        <Button Grid.Row="1" HorizontalAlignment="Right" Name="Start" Margin="0,0,62,0" Click="Start_Click">Start Program</Button>
    </Grid>
</Window>
MainWindow.xaml.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Diagnostics;

namespace QQ_Byhh
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Info> mPath = new List<Info>();
        //List<string> mName = new List<string>();

        public MainWindow()
        {
            InitializeComponent();
            this.DataBinding();
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            bool AddFlag = true;
            Microsoft.Win32.OpenFileDialog oFile = new Microsoft.Win32.OpenFileDialog();
            oFile.InitialDirectory = @"c:\";
            oFile.RestoreDirectory = true;
            oFile.Filter = "文本文件(*.exe)|*.exe";
            oFile.ShowDialog();
            if (oFile.FileName != "")
            {
                for (int i = 0; i < mPath.Count; i++)
                {
                    if (mPath[i].path == oFile.FileName)
                    {
                        AddFlag = false;
                    }
                }
                if (AddFlag)
                {
                    Info myPath = new Info()
                    {
                        path = oFile.FileName
                    };
                    mPath.Add(myPath);
                }
            }
            this.ProgramList.Items.Refresh();
        }

        private void DataBinding()
        {
            this.ProgramList.ItemsSource = mPath;//为ListView绑定数据源  
        }

        private void DeleteProgram(string sP)
        {
           
            if (mPath.Count > 0)
            {
                for (int i = 0; i < mPath.Count; i++)
                {
                    if (mPath[i].path == sP)
                    {
                        mPath.Remove(mPath[i]);
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            string sP = Convert.ToString(b.CommandParameter);
            this.DeleteProgram(sP);
            this.ProgramList.Items.Refresh();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < mPath.Count; i++)
            {
                Process p = new Process();
                p.StartInfo.FileName = mPath[i].path;
                p.Start();
            }
            
        }  
    }
}

  C#在windows上果然很强大啊, 学了半天就能做个像模像样的, 以后得多加学习一下

posted @ 2012-10-06 15:56  Moondark  阅读(572)  评论(1编辑  收藏  举报