用C#实现第一个拥有好看界面的EXE应用程序

技术致谢:
[1]: https://handyorg.github.io/handycontrol/

  1. 使用HandyControl建立一个基础工程
    在这里插入图片描述
  2. vs2019 下方点击XAML标签:
    在这里插入图片描述
  3. 定位光标到 <Grid> </Grid>之间:
  4. 插入代码:
<hc:TabControl x:Name="MainWindowsTabControl_Handle"
                IsAnimationEnabled="True" 
                ShowCloseButton="False" 
                IsDraggable="False" 
                IsTabFillEnabled="True" 
                SelectionChanged="TabControl_SelectionChanged">
            <hc:TabItem x:Name="Tab01Page" Header="Tab01" FontFamily="Consolas" FontStyle="Italic" FontWeight="Bold">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>

                    <ContentControl x:Name ="ShowTab1TitleToUser_ContentControl" 
                                    Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="3"
                                    Content="--" 
                                    FontFamily="Microsoft YaHei" 
                                    FontStyle="Italic" 
                                    FontWeight="Bold" 
                                    FontSize="48" 
                                    HorizontalAlignment="Center" 
                                    VerticalAlignment="Center"
                                    />
                    
             

                </Grid>
            </hc:TabItem>
            <hc:TabItem x:Name="Tab02Page" Header="Tab02" FontFamily="Consolas" FontStyle="Italic" FontWeight="Bold">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>


                    <TextBox x:Name="Tab2SearchFilesText" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="4"
                             Height="30"
                             Style="{StaticResource TextBoxExtend}" 
                             hc:InfoElement.Title="Find What:" 
                             hc:InfoElement.TitlePlacement="Left"
                             VerticalAlignment="Center"
                             Cursor="IBeam"
                             Visibility="Visible" FontFamily="Consolas" FontStyle="Italic" FontWeight="Bold"/>

                    <Button x:Name="Tab2SearchFilesClickButton" hc:BorderElement.CornerRadius="5"
                            Grid.Column="5" Grid.Row="1"  Grid.ColumnSpan="1"
                            Height="30"
                            Width="150"
                            Content="Search"
                            Click="UsedEverythingApisButtonSearch_Click"
                            Visibility="Visible" FontFamily="Consolas" FontStyle="Italic" FontWeight="Bold"/>

                    <ListView x:Name="Tab2EverythingApisSearchingResultListView" 
                                  ItemsSource="{Binding DataList}"
                                  Grid.Column="1" Grid.Row="2"  Grid.ColumnSpan="5" Grid.RowSpan="12"
                                  PreviewMouseLeftButtonDown="ListViewItem_PreviewMouseLeftButtonDown"
                                  PreviewMouseDoubleClick="ListViewItem_PreviewMouseDoubleClick"
                                  Visibility="Visible">
                        <ListView.View>
                            <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
                                <GridViewColumn DisplayMemberBinding="{Binding Path=FileName}"  Header="FileName" Width="350"/>
                                <GridViewColumn DisplayMemberBinding="{Binding Path=FullPath}"  Header="FullPath" Width="500"/>
                            </GridView>
                        </ListView.View>
                    </ListView>

                    
                </Grid>
            </hc:TabItem>
        </hc:TabControl>

        <hc:ScrollViewer
                VerticalScrollBarVisibility="Auto" HorizontalAlignment="Right" IsHitTestVisible="False">
            <StackPanel
                    hc:Growl.Token="SuccessMsg"
                    hc:Growl.GrowlParent="True" VerticalAlignment="Bottom" Margin="0,10,10,10" />
        </hc:ScrollViewer>

        <hc:ScrollViewer
                VerticalScrollBarVisibility="Auto" HorizontalAlignment="Right" IsHitTestVisible="False">
            <StackPanel
                    hc:Growl.Token="InfoMsg"
                    hc:Growl.GrowlParent="True" VerticalAlignment="Bottom" Margin="0,10,10,10" />
        </hc:ScrollViewer>

        <hc:ScrollViewer
                VerticalScrollBarVisibility="Auto" HorizontalAlignment="Right" IsHitTestVisible="False">
            <StackPanel
                    hc:Growl.Token="WarningMsg"
                    hc:Growl.GrowlParent="True" VerticalAlignment="Bottom" Margin="0,10,10,10" />
        </hc:ScrollViewer>

        <hc:ScrollViewer
                VerticalScrollBarVisibility="Auto" HorizontalAlignment="Right" IsHitTestVisible="False">
            <StackPanel
                    hc:Growl.Token="ErrorMsg"
                    hc:Growl.GrowlParent="True" VerticalAlignment="Bottom" Margin="0,10,10,10" />
        </hc:ScrollViewer>
  1. 点击进入到*.xaml.cs文件:
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Runtime.InteropServices;
using HandyControl.Controls;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;

namespace SystemManager
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            e.Handled = true;
            if (e.OriginalSource != MainWindowsTabControl_Handle)
            {
                return;
            }

            HandyControl.Controls.TabItem ti = MainWindowsTabControl_Handle.SelectedItem as HandyControl.Controls.TabItem;
            string table_item_name = ti.Header.ToString();

            if (e.Source is HandyControl.Controls.TabControl)
            {
                if (table_item_name.CompareTo("Tab01") == 0)
                {
                    Growl.Info("Tab01", "InfoMsg");
                }
                else if (table_item_name.CompareTo("Tab02") == 0)
                {
                    Growl.Info("Tab02", "InfoMsg");
                }
            }
        }




        #region 引用 EVERYTHING DLL
        const int EVERYTHING_OK = 0;
        const int EVERYTHING_ERROR_MEMORY = 1;
        const int EVERYTHING_ERROR_IPC = 2;
        const int EVERYTHING_ERROR_REGISTERCLASSEX = 3;
        const int EVERYTHING_ERROR_CREATEWINDOW = 4;
        const int EVERYTHING_ERROR_CREATETHREAD = 5;
        const int EVERYTHING_ERROR_INVALIDINDEX = 6;
        const int EVERYTHING_ERROR_INVALIDCALL = 7;

        const int EVERYTHING_REQUEST_FILE_NAME = 0x00000001;
        const int EVERYTHING_REQUEST_PATH = 0x00000002;
        const int EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004;
        const int EVERYTHING_REQUEST_EXTENSION = 0x00000008;
        const int EVERYTHING_REQUEST_SIZE = 0x00000010;
        const int EVERYTHING_REQUEST_DATE_CREATED = 0x00000020;
        const int EVERYTHING_REQUEST_DATE_MODIFIED = 0x00000040;
        const int EVERYTHING_REQUEST_DATE_ACCESSED = 0x00000080;
        const int EVERYTHING_REQUEST_ATTRIBUTES = 0x00000100;
        const int EVERYTHING_REQUEST_FILE_LIST_FILE_NAME = 0x00000200;
        const int EVERYTHING_REQUEST_RUN_COUNT = 0x00000400;
        const int EVERYTHING_REQUEST_DATE_RUN = 0x00000800;
        const int EVERYTHING_REQUEST_DATE_RECENTLY_CHANGED = 0x00001000;
        const int EVERYTHING_REQUEST_HIGHLIGHTED_FILE_NAME = 0x00002000;
        const int EVERYTHING_REQUEST_HIGHLIGHTED_PATH = 0x00004000;
        const int EVERYTHING_REQUEST_HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000;

        const int EVERYTHING_SORT_NAME_ASCENDING = 1;
        const int EVERYTHING_SORT_NAME_DESCENDING = 2;
        const int EVERYTHING_SORT_PATH_ASCENDING = 3;
        const int EVERYTHING_SORT_PATH_DESCENDING = 4;
        const int EVERYTHING_SORT_SIZE_ASCENDING = 5;
        const int EVERYTHING_SORT_SIZE_DESCENDING = 6;
        const int EVERYTHING_SORT_EXTENSION_ASCENDING = 7;
        const int EVERYTHING_SORT_EXTENSION_DESCENDING = 8;
        const int EVERYTHING_SORT_TYPE_NAME_ASCENDING = 9;
        const int EVERYTHING_SORT_TYPE_NAME_DESCENDING = 10;
        const int EVERYTHING_SORT_DATE_CREATED_ASCENDING = 11;
        const int EVERYTHING_SORT_DATE_CREATED_DESCENDING = 12;
        const int EVERYTHING_SORT_DATE_MODIFIED_ASCENDING = 13;
        const int EVERYTHING_SORT_DATE_MODIFIED_DESCENDING = 14;
        const int EVERYTHING_SORT_ATTRIBUTES_ASCENDING = 15;
        const int EVERYTHING_SORT_ATTRIBUTES_DESCENDING = 16;
        const int EVERYTHING_SORT_FILE_LIST_FILENAME_ASCENDING = 17;
        const int EVERYTHING_SORT_FILE_LIST_FILENAME_DESCENDING = 18;
        const int EVERYTHING_SORT_RUN_COUNT_ASCENDING = 19;
        const int EVERYTHING_SORT_RUN_COUNT_DESCENDING = 20;
        const int EVERYTHING_SORT_DATE_RECENTLY_CHANGED_ASCENDING = 21;
        const int EVERYTHING_SORT_DATE_RECENTLY_CHANGED_DESCENDING = 22;
        const int EVERYTHING_SORT_DATE_ACCESSED_ASCENDING = 23;
        const int EVERYTHING_SORT_DATE_ACCESSED_DESCENDING = 24;
        const int EVERYTHING_SORT_DATE_RUN_ASCENDING = 25;
        const int EVERYTHING_SORT_DATE_RUN_DESCENDING = 26;

        const int EVERYTHING_TARGET_MACHINE_X86 = 1;
        const int EVERYTHING_TARGET_MACHINE_X64 = 2;
        const int EVERYTHING_TARGET_MACHINE_ARM = 3;

        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern UInt32 Everything_SetSearchW(string lpSearchString);
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetMatchPath(bool bEnable);
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetMatchCase(bool bEnable);
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetMatchWholeWord(bool bEnable);
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetRegex(bool bEnable);
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetMax(UInt32 dwMax);
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetOffset(UInt32 dwOffset);

        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetMatchPath();
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetMatchCase();
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetMatchWholeWord();
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetRegex();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetMax();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetOffset();
        [DllImport("Everything64.dll")]
        public static extern IntPtr Everything_GetSearchW();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetLastError();

        [DllImport("Everything64.dll")]
        public static extern bool Everything_QueryW(bool bWait);

        [DllImport("Everything64.dll")]
        public static extern void Everything_SortResultsByPath();

        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetNumFileResults();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetNumFolderResults();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetNumResults();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetTotFileResults();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetTotFolderResults();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetTotResults();
        [DllImport("Everything64.dll")]
        public static extern bool Everything_IsVolumeResult(UInt32 nIndex);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_IsFolderResult(UInt32 nIndex);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_IsFileResult(UInt32 nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern void Everything_GetResultFullPathName(UInt32 nIndex, StringBuilder lpString, UInt32 nMaxCount);
        [DllImport("Everything64.dll")]
        public static extern void Everything_Reset();

        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultFileName(UInt32 nIndex);

        // Everything 1.4
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetSort(UInt32 dwSortType);
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetSort();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetResultListSort();
        [DllImport("Everything64.dll")]
        public static extern void Everything_SetRequestFlags(UInt32 dwRequestFlags);
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetRequestFlags();
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetResultListRequestFlags();
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultExtension(UInt32 nIndex);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultSize(UInt32 nIndex, out long lpFileSize);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateCreated(UInt32 nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateModified(UInt32 nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateAccessed(UInt32 nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetResultAttributes(UInt32 nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultFileListFileName(UInt32 nIndex);
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetResultRunCount(UInt32 nIndex);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateRun(UInt32 nIndex, out long lpFileTime);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_GetResultDateRecentlyChanged(UInt32 nIndex, out long lpFileTime);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultHighlightedFileName(UInt32 nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultHighlightedPath(UInt32 nIndex);
        [DllImport("Everything64.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultHighlightedFullPathAndFileName(UInt32 nIndex);
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_GetRunCountFromFileName(string lpFileName);
        [DllImport("Everything64.dll")]
        public static extern bool Everything_SetRunCountFromFileName(string lpFileName, UInt32 dwRunCount);
        [DllImport("Everything64.dll")]
        public static extern UInt32 Everything_IncRunCountFromFileName(string lpFileName);


        private void UsedEverythingApisButtonSearch_Click(object sender, EventArgs e)
        {
            string str = Tab2SearchFilesText.Text.ToString();
            if (string.IsNullOrWhiteSpace(str))
            {
                //Notice.Show("Noting To Find", "Warning", 5, MessageBoxIcon.Warning);
                Growl.Warning("Noting To Find", "WarningMsg");
                return;
            }

            Everything_SetSearchW(Tab2SearchFilesText.Text);
            Everything_SetRequestFlags(EVERYTHING_REQUEST_FILE_NAME | EVERYTHING_REQUEST_PATH | EVERYTHING_REQUEST_DATE_MODIFIED | EVERYTHING_REQUEST_SIZE);
            Everything_SetSort(13);
            Everything_QueryW(true);
            Tab2EverythingApisSearchingResultListView.Items.Clear();

            for (UInt32 i = 0; i < Everything_GetNumResults(); i++)
            {
                long date_modified;
                long size;
                var fullpath = new StringBuilder(1024); ;

                Everything_GetResultDateModified(i, out date_modified);
                Everything_GetResultSize(i, out size);
                Everything_GetResultFullPathName(i, fullpath, 1024);
                Tab2EverythingApisSearchingResultListView.Items.Add(
                    new { FileName = Marshal.PtrToStringUni(Everything_GetResultFileName(i)), FullPath = fullpath.ToString() });

                if (i + 1 >= 1024)
                {
                    Growl.Warning("文件数超过 1024, 将仅显示前1024个文件!! ", "WarningMsg");
                    break;
                }
            }
        }

        private void ListViewItem_PreviewMouseLeftButtonDown(object sender, RoutedEventArgs e)
        {

        }

        private void ListViewItem_PreviewMouseDoubleClick(object sender, RoutedEventArgs e)
        {
            var item = (sender as ListView).SelectedItem;
            if (item != null)
            {
                var nameOfProperty = "FullPath";
                var propertyInfo = item.GetType().GetProperty(nameOfProperty);
                var FullPath = propertyInfo.GetValue(item, null).ToString();
                System.Diagnostics.Process.Start("explorer.exe", FullPath);
            }
        }
        #endregion

    }
}

请添加图片描述
请添加图片描述

效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值