Qt 如何在打开的文件夹中鼠标选中某文件
发布网友
发布时间:2022-04-24 02:14
我来回答
共1个回答
热心网友
时间:2023-10-21 08:36
是在打开的文件夹中自动选中某文件吧?不需要用鼠标。
试试看这一段:
bool OpenFolderAndSelectFile(const char *filePath)
{
#ifdef Q_OS_WIN
LPITEMIDLIST pidl;
LPCITEMIDLIST cpidl;
LPSHELLFOLDER pDesktopFolder;
ULONG chEaten;
HRESULT hr;
WCHAR wfilePath[MAX_PATH+1] = { 0 };
::CoInitialize( NULL );
if ( SUCCEEDED( SHGetDesktopFolder( &pDesktopFolder ) ) )
{
// IShellFolder::ParseDisplayName要传入宽字节
LPWSTR lpWStr = NULL;
//#ifdef _UNICODE
// _tcscpy( wfilePath, strFilePath );
// lpWStr = wfilePath;
//#else
MultiByteToWideChar( CP_ACP, 0, (LPCSTR)filePath, -1, wfilePath, MAX_PATH );
lpWStr = wfilePath;
//#endif
hr = pDesktopFolder->ParseDisplayName( NULL, 0, lpWStr, &chEaten, &pidl, NULL );
if ( FAILED( hr ) )
{
pDesktopFolder->Release();
::CoUninitialize();
return FALSE;
}
cpidl = pidl;
// SHOpenFolderAndSelectItems是非公开的API函数,需要从shell32.dll获取
// 该函数只有XP及以上的系统才支持,Win2000和98是不支持的,考虑到Win2000
// 和98已经基本不用了,所以就不考虑了,如果后面要支持上述老的系统,则要
// 添加额外的处理代码
HMODULE hShell32DLL = ::LoadLibraryA("shell32.dll");
//ASSERT( hShell32DLL != NULL );
if( hShell32DLL != NULL )
{
typedef HRESULT (WINAPI *pSelFun)( LPCITEMIDLIST pidlFolder, UINT cidl, LPCITEMIDLIST *apidl, DWORD dwFlags );
pSelFun pFun = (pSelFun)::GetProcAddress( hShell32DLL, "SHOpenFolderAndSelectItems" );
//ASSERT( pFun != NULL );
if( pFun != NULL )
{
hr = pFun( cpidl, 0, NULL, 0 ); // 第二个参数cidl置为0,表示是选中文件
if ( FAILED( hr ) )
{
::FreeLibrary( hShell32DLL );
pDesktopFolder->Release();
::CoUninitialize();
return FALSE;
}
}
::FreeLibrary( hShell32DLL );
}
else
{
pDesktopFolder->Release();
::CoUninitialize();
return FALSE;
}
// 释放pDesktopFolder
pDesktopFolder->Release();
}
else
{
::CoUninitialize();
return FALSE;
}
::CoUninitialize();
return TRUE;
#else
QString pathIn(filePath);
QStringList scriptArgs;
scriptArgs << QLatin1String("-e") << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"").arg(pathIn.replace('\\', '/'));
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e") << QLatin1String("tell application \"Finder\" to activate");
QProcess::execute("/usr/bin/osascript", scriptArgs);
return true;
#endif
}