NEWS

[C#] Hướng dẫn nhúng Embed Windows Explorer vào Winform làm File Manager

[C#] Hướng dẫn nhúng Embed Windows Explorer vào Winform làm File Manager
Đăng bởi: Thảo Meo - Lượt xem: 8432 11:23:31, 06/02/2020DEVEXPRESS   In bài viết

Xin chào các bạn, hôm nay mình tiếp tục hướng dẫn các bạn cách nhúng Embed Windows Explorer vào Winform trong lập trình C# làm File Manager.

Trong bài viết trước, mình cũng đã có hướng dẫn các bạn cách làm ứng dụng File Manager bằng cách sử dụng kết hợp công cụ: TreeView và ListView.

Nhưng trong bài viết này, mình sẽ hướng dẫn các bạn nhúng luôn Windows Explorer vào Winform của mình luôn.

Để nhúng cửa sổ Windows Explorer vào, mình sẽ sử dụng thư viện Windows API Code Pack phiên bản 1.1 được cung cấp bởi Microsoft.

Lưu ý: Thư viện này chỉ sử dụng được trên Windows 7 trở lên thôi nhé các bạn.

Dưới đây là giao diện demo ứng dụng File Manager C# của mình:

exploer_windows

Khi các bạn nhúng vào thì sử dụng được các chức năng bình thường như trong Windows Explorer.

Đầu tiên, các bạn cần import hai thư viện Windows API code pack vào: Microsoft.WindowsAPICodePack.Shell.dllMicrosoft.WindowsAPICodePack.dll. (download cuối bài viết)

Các bạn kéo file Microsoft.WindowsAPICodePack.Shell.dll vào thanh toolboxs trong Visual Studio, các bạn sẽ thấy thêm component Explorer Brower Control, các bạn lấy control này để sử dụng.

explorerbrowser_control

Source code Demo ứng dụng ExplorerBrowserControl C#:

using Microsoft.WindowsAPICodePack.Controls;
using Microsoft.WindowsAPICodePack.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Timer uiDecoupleTimer = new System.Windows.Forms.Timer();
        AutoResetEvent selectionChanged = new AutoResetEvent(false);
        AutoResetEvent itemsChanged = new AutoResetEvent(false);
        public Form1()
        {
            InitializeComponent();
            explorerBrowser.NavigationLog.NavigationLogChanged += new EventHandler<NavigationLogEventArgs>(NavigationLog_NavigationLogChanged);
            uiDecoupleTimer.Tick += new EventHandler(uiDecoupleTimer_Tick);

            explorerBrowser.ItemsChanged += new EventHandler(explorerBrowser_ItemsChanged);
            explorerBrowser.SelectionChanged += new EventHandler(explorerBrowser_SelectionChanged);

            uiDecoupleTimer.Interval = 100;
            uiDecoupleTimer.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            explorerBrowser.Navigate((ShellObject)KnownFolders.Computer);
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            explorerBrowser.NavigateLogLocation(NavigationLogDirection.Backward);
        }

        void explorerBrowser_SelectionChanged(object sender, EventArgs e)
        {
            selectionChanged.Set();
        }

        void explorerBrowser_ItemsChanged(object sender, EventArgs e)
        {
            itemsChanged.Set();
            var a = explorerBrowser.NavigationLog.CurrentLocation.Properties;
        }

        private void btn_forward_Click(object sender, EventArgs e)
        {
            explorerBrowser.NavigateLogLocation(NavigationLogDirection.Forward);
        }
        public void NavigationLog_NavigationLogChanged(object sender, NavigationLogEventArgs args)
        {
            
            BeginInvoke(new MethodInvoker(delegate ()
            {              
                if (args.CanNavigateBackwardChanged)
                {
                    this.btn_back.Enabled = explorerBrowser.NavigationLog.CanNavigateBackward;
                }
                if (args.CanNavigateForwardChanged)
                {
                    this.btn_forward.Enabled = explorerBrowser.NavigationLog.CanNavigateForward;
                }

                if (args.LocationsChanged)
                {
                   
                    foreach (ShellObject shobj in this.explorerBrowser.NavigationLog.Locations)
                    {
                        if (shobj.ParsingName.Contains(@""))
                        {
                            txt_location.Text = shobj.ParsingName;
                        }
                        else
                        {
                            txt_location.Text = shobj.Name;

                        }
                    }
                }

                if (this.explorerBrowser.NavigationLog.CurrentLocationIndex == -1)
                   txt_location.Text = "";
                else
                {
                    if (explorerBrowser.NavigationLog.CurrentLocation.ParsingName.Contains(@""))
                    {
                        txt_location.Text = explorerBrowser.NavigationLog.CurrentLocation.ParsingName;
                    }
                    else
                    {
                        txt_location.Text = explorerBrowser.NavigationLog.CurrentLocation.Name;

                    }
                   
                }

            }));
        }

        public void uiDecoupleTimer_Tick(object sender, EventArgs e)
        {
            if (selectionChanged.WaitOne(1))
            {
                StringBuilder itemsText = new StringBuilder();

                foreach (ShellObject item in explorerBrowser.SelectedItems)
                {
                    if (item != null)
                        itemsText.AppendLine(item.GetDisplayName(DisplayNameType.Default));
                }

                lbl_fileName_select.Text = itemsText.ToString();          
            }
           
        }
 
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn nhúng Embed Windows Explorer vào Winform làm File Manager
Đăng bởi: Thảo Meo - Lượt xem: 8432 11:23:31, 06/02/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.