C# FolderBrowserDialog滚动条自动滚动到设定的文件夹

文章介绍了一种方法,通过C#代码利用DllImport调用WindowsAPI,使FolderBrowserDialog在打开时能自动滚动到预设的文件夹位置,解决了路径深导致的滚动问题。主要涉及SendMessageW、FindWindowExW等函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们使用文件夹选择对话框时,有时候设定的路径藏得比较深,需要滚动比较长,我们可以通过下面的方法让滚动条自动滚动到我们选定的文件夹位置。

代码来自于一个论坛,国外的一位网友写的,我微调了延时可以稳定工作了

  //让fileBrowerDialog自动滚动到所选中的目录位置
    public enum GW
        {
            HWNDFIRST = 0,
            HWNDLAST = 1,
            HWNDNEXT = 2,
            HWNDPREV = 3,
            OWNER = 4,
            CHILD = 5,
            ENABLEDPOPUP = 6
        }
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageW", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr SendMessageW(IntPtr hWnd, uint msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindowExW", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr FindWindowExW(IntPtr hWndParent, IntPtr hWndChildAfter, [MarshalAs(UnmanagedType.LPWStr)] string lpszClass, [MarshalAs(UnmanagedType.LPWStr)] string lpszWindow);
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindow", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        public static extern UInt32 GetWindow(IntPtr hwnd, UInt32 wCmd);
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetDesktopWindow", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr GetDesktopWindow();
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetClassNameA", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        public static extern int GetClassName(IntPtr hwnd, System.Text.StringBuilder lpClassName, int nMaxCount);
        //让fileBrowerDialog自动滚动到所选中的目录位置
      private  void FbgAutoScroll(FolderBrowserDialog Fbg, IntPtr _Owner)
        {
            IntPtr hwnd = System.IntPtr.Zero;
            StringBuilder sClassname = new StringBuilder(256);
            //Thread.Sleep(100); //necessary to let FolderBrowserDialog construct its window,原作者给了50ms经常第一次不行,改为200就好了
            Thread.Sleep(500);//原作者默认的时间太短第一次弹窗不能正常工作
            hwnd = GetDesktopWindow(); //Desktop window handle.
            hwnd = (System.IntPtr)GetWindow(hwnd, (UInt32)GW.CHILD); //We will find all children.
            while (!(hwnd == (System.IntPtr)0))
            {
                if (GetWindow(hwnd, (UInt32)GW.OWNER) == (UInt32)_Owner) //If one window is owned by our main window...
                {
                    GetClassName(hwnd, sClassname, 255);
                    if (sClassname.ToString() == "#32770") //Check if the class is FolderBrowserDialog.
                    {
                        break; //Then we found it.
                    }
                }
                hwnd = (System.IntPtr)GetWindow(hwnd, (UInt32)GW.HWNDNEXT); //Next window.
            } //If no found then exit.
            if (hwnd == (System.IntPtr)0)
            {
                return;
            }
            IntPtr hChild = (System.IntPtr)0;
            IntPtr hTreeView = (System.IntPtr)0;
            int i = 0;
            do
            {
                i += 1;
                if (i > 1000) //Security to avoid infinite loop.
                {
                    return;
                }
                hChild = FindWindowExW(hwnd, hChild, null, null); //Look for children windows of FolderBrowserDialog.
                hTreeView = FindWindowExW(hChild, (System.IntPtr)0, "SysTreeView32", null); //Look for treeview of FolderBrowserDialog.
                Thread.Sleep(10); //delay necessary because FolderBrowserDialog is in construction, then treeview maybe not yet exist.
            } while (hTreeView == (System.IntPtr)0);
            if (SendMessageW(hwnd, 0x46A, 1, Fbg.SelectedPath) == (System.IntPtr)0) //Send message BFFM_SETEXPANDED to FolderBrowserDialog.
            {
                SendMessageW(hTreeView, 0x7, 0, null); //Send message WM_SETFOCUS to the treeeview.          
            }
        }
 	 private void button2_Click(object sender, EventArgs e)
        {
            string selectpath = @"D:\jason\app\build\generated\res\pngs\debug\";
            FolderBrowserDialog Fbg = new FolderBrowserDialog();
            Fbg.SelectedPath = selectpath;
            Fbg.Description = "请选择目标文件夹";
            IntPtr Me_handle = this.Handle; // Store the main handle to compare after with each window's owner.
            Task.Run(() => FbgAutoScroll(Fbg, Me_handle)); // Here's the trick, run an asynchronous task to modify the folder dialog.
            if (Fbg.ShowDialog(this) == DialogResult.OK)
            {
                Debug.Print(Fbg.SelectedPath);
            }
        }

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值