FolderBrowserDialogがWindows7環境で選択されたFolderを適切ないように表示事象
2021-07-20 11:17
阅读:534
一つの銀行内部システムがUpdateして、そもそもXPシステムの場合、順調で来たのに。
Windows7の場合、 FolderBrowserDialogがうまく動作してなかった。
調べて結果が、仕方ない。Windows Systemの既存Bugです。
色々調査を書けて、結論として二つの対応方針があります。
1、Windowチーム開発されたWindowsAPICodePack をDownloadして、
そのMicrosoft.WindowsAPICodePack.Dialogsを参照して、下記の例にするばよい。
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = @"C:\Users\wangj\Documents\WeChat Files\wxid_mw93g8weqm3e11\Msg";
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
textBox1.Text = dialog.FileName;
}
2、Windows APIを利用して。
public static class FolderBrowserLauncher
{
///
/// Using title text to look for the top level dialog window is fragile.
/// In particular, this will fail in non-English applications.
///
const string _topLevelSearchString = "Browse For Folder";
///
/// 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.
///
const int _dlgItemBrowseControl = 0;
const int _dlgItemTreeView = 100;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
///
/// Some of the messages that the Tree View control will respond to
///
private const int TV_FIRST = 0x1100;
private const int TVM_SELECTITEM = (TV_FIRST + 11);
private const int TVM_GETNEXTITEM = (TV_FIRST + 10);
private const int TVM_GETITEM = (TV_FIRST + 12);
private const int TVM_ENSUREVISIBLE = (TV_FIRST + 20);
private const int WM_CLOSE = 0x0010;
///
/// Constants used to identity specific items in the Tree View control
///
private const int TVGN_ROOT = 0x0;
private const int TVGN_NEXT = 0x1;
private const int TVGN_CHILD = 0x4;
private const int TVGN_FIRSTVISIBLE = 0x5;
private const int TVGN_NEXTVISIBLE = 0x6;
private const int TVGN_CARET = 0x9;
///
/// 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.
///
///
///
///
public static DialogResult ShowFolderBrowser(FolderBrowserDialog dlg, IWin32Window parent = null)
{
DialogResult result = DialogResult.Cancel;
Thread t3 = new Thread(() =>
{
int i = 30;
while (i>0)
{
i--;
System.Threading.Thread.Sleep(100);
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, WM_CLOSE, IntPtr.Zero, item);
SendMessage(hwndTV, TVM_ENSUREVISIBLE, IntPtr.Zero, item);
//if(i>20)
// SendMessage(hwndTV, 0x0000, IntPtr.Zero, IntPtr.Zero);
//retries = 0;
//SendKeys.Send("{TAB}{TAB}{RIGHT}");
}
}
}
}
}
});
t3.Start();
result = dlg.ShowDialog(parent);
return result;
}
}
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:FolderBrowserDialogがWindows7環境で選択されたFolderを適切ないように表示事象
文章链接:http://soscw.com/essay/106589.html
文章标题:FolderBrowserDialogがWindows7環境で選択されたFolderを適切ないように表示事象
文章链接:http://soscw.com/essay/106589.html
评论
亲,登录后才可以留言!