我用过JNA库和这个小应用程序接口(JnaFileChooser) https://github.com/steos/jnafilechooser
JnaFileChooser fc = new JnaFileChooser();
fc.addFilter("All Files", "*");
fc.addFilter("Pictures", "jpg", "jpeg", "png", "gif", "bmp");
if (fc.showDialog(parent)) {
File f = fc.getSelectedFile();
// do something with f}
但是如何使用JNA访问此对话框“选择文件夹”

发布于 2020-07-19 22:53:09
整个对话都是在本机端控制的。您正在使用的包已经在访问该对话框和该按钮。
通过跟踪JnaFileChooser类的源代码,此对话框是WindowsFolderBrowser类的一部分。使用结合使用SHBrowseForFolder()函数和SHGetPathFromIDList的对话框将出现,并在按下Select Folder按钮时返回path。
final Pointer pidl = Shell32.SHBrowseForFolder(params);
if (pidl != null)
// MAX_PATH is 260 on Windows XP x32 so 4kB should
// be more than big enough
final Pointer path = new Memory(1024 * 4);
Shell32.SHGetPathFromIDListW(pidl, path);
final String filePath = path.getWideString(0);
final File file = new File(filePath);
Ole32.CoTaskMemFree(pidl);
return file;
}传递给此函数的params变量是控制对话框的本机类型BROWSEINFO。您可以在代码中看到如何将一些内容分配给它(代码的缩写版本):
final Shell32.BrowseInfo params = new Shell32.BrowseInfo();
params.hwndOwner = Native.getWindowPointer(parent);
params.ulFlags = Shell32.BIF_RETURNONLYFSDIRS | Shell32.BIF_USENEWUI;
params.lpszTitle = title;如果您想要更改有关该对话框的任何其他内容,则需要使用回调。BROWSEINFO中的一个元素是BFFCALLBACK lpfn;,您可以在其中定义该函数,例如,params.lpfn =已定义的回调函数。
BFFCALLBACK的文档指出,您将使用SendMessage选项通过BFFM_SETOKTEXT更改OK按钮文本。
https://stackoverflow.com/questions/62967836
复制相似问题