C# FolderBrowserDialog Scroll to Selected Path 文件夹选择框初始位置没有滚动到默认文件夹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# FolderBrowserDialog Scroll to Selected Path 文件夹选择框初始位置没有滚动到默认文件夹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3255字,纯文字阅读大概需要5分钟。
内容图文
![C# FolderBrowserDialog Scroll to Selected Path 文件夹选择框初始位置没有滚动到默认文件夹](/upload/InfoBanner/zyjiaocheng/1295/b83593c13c454b7eaf9bc8bb2dd494ce.jpg)
public static class FolderBrowserLauncher { /// <summary> /// Using title text to look for the top level dialog window is fragile. /// In particular, this will fail in non-English applications. /// </summary> const string _topLevelSearchString = "Browse For Folder"; ///<summary>/// These should be more robust. We find the correct child controls in the dialog /// by using the GetDlgItem method, rather than the FindWindow(Ex) method, /// because the dialog item IDs should be constant. ///</summary>constint _dlgItemBrowseControl = 0; constint _dlgItemTreeView = 100; [DllImport("user32.dll", SetLastError = true)] staticextern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] staticextern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem); [DllImport("user32.dll", CharSet = CharSet.Auto)] staticextern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); ///<summary>/// Some of the messages that the Tree View control will respond to ///</summary>privateconstint TV_FIRST = 0x1100; privateconstint TVM_SELECTITEM = (TV_FIRST + 11); privateconstint TVM_GETNEXTITEM = (TV_FIRST + 10); privateconstint TVM_GETITEM = (TV_FIRST + 12); privateconstint TVM_ENSUREVISIBLE = (TV_FIRST + 20); ///<summary>/// Constants used to identity specific items in the Tree View control ///</summary>privateconstint TVGN_ROOT = 0x0; privateconstint TVGN_NEXT = 0x1; privateconstint TVGN_CHILD = 0x4; privateconstint TVGN_FIRSTVISIBLE = 0x5; privateconstint TVGN_NEXTVISIBLE = 0x6; privateconstint TVGN_CARET = 0x9; ///<summary>/// Calling this method is identical to calling the ShowDialog method of the provided /// FolderBrowserDialog, except that an attempt will be made to scroll the Tree View /// to make the currently selected folder visible in the dialog window. ///</summary>///<param name="dlg"></param>///<param name="parent"></param>///<returns></returns>publicstatic DialogResult ShowFolderBrowser(FolderBrowserDialog dlg, IWin32Window parent = null) { DialogResult result = DialogResult.Cancel; int retries = 10; using (Timer t = new Timer()) { t.Tick += (s, a) => { if (retries > 0) { --retries; IntPtr hwndDlg = FindWindow((string)null, _topLevelSearchString); if (hwndDlg != IntPtr.Zero) { IntPtr hwndFolderCtrl = GetDlgItem(hwndDlg, _dlgItemBrowseControl); if (hwndFolderCtrl != IntPtr.Zero) { IntPtr hwndTV = GetDlgItem(hwndFolderCtrl, _dlgItemTreeView); if (hwndTV != IntPtr.Zero) { IntPtr item = SendMessage(hwndTV, (uint)TVM_GETNEXTITEM, new IntPtr(TVGN_CARET), IntPtr.Zero); if (item != IntPtr.Zero) { SendMessage(hwndTV, TVM_ENSUREVISIBLE, IntPtr.Zero, item); retries = 0; t.Stop(); } } } } } else { //// We failed to find the Tree View control. //// As a fall back (and this is an UberUgly hack), we will send // some fake keystrokes to the application in an attempt to force // the Tree View to scroll to the selected item. // t.Stop(); SendKeys.Send("{TAB}{TAB}{DOWN}{DOWN}{UP}{UP}"); } }; t.Interval = 10; t.Start(); result = dlg.ShowDialog(parent); } return result; } }
原文:http://www.cnblogs.com/swtool/p/5780111.html
内容总结
以上是互联网集市为您收集整理的C# FolderBrowserDialog Scroll to Selected Path 文件夹选择框初始位置没有滚动到默认文件夹全部内容,希望文章能够帮你解决C# FolderBrowserDialog Scroll to Selected Path 文件夹选择框初始位置没有滚动到默认文件夹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。