1,界面出了一点小问题,就是我先在一些edit ctrl里面填写一些内容,然后又在下面打开了选择文件对话框,填入文件路径,这个时候上面的那些edit ctrl里面的内容就没有了,请问这应该怎样解决?
我的写法是,点击完打开文件对话框之后选择路径,然后updatedata(0);
2,我想点击按钮之后,像操作系统打开“我的电脑”“我的文档”一样打开我想要的一个文件目录。比方说“c:\new”;
求助,谢谢了。。。
第一个问题,可能没有图不好明白,其实就是,我一打开“选择文件”对话框之后,以前填写过的内容就会消失,怎么解决这个问题?我为edit ctrl变量添加的是Cstring类型的变量。
8 个解决方案
#1
不要UpdateData()更新所有控件,你可以选择文件对话框等后,GetWindowText/SetWindowText来更新特定控件
#2
打开之前先updatedata一下,将控件中的数据更新到变量中
#3
选择路径
HRESULT CFileSystemTool::SelectFolder(CString& strFolder)
{
REGISTER_FUNC( CFileSystemTool::SelectFolder(CString& strFolder) );
HRESULT hr = E_FAIL;
strFolder.Empty();
try
{
BROWSEINFO bi;
TCHAR pBuffer[MAX_PATH];
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = pBuffer;
bi.lpszTitle = _T("选择路径");
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.iImage = NULL ;
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
VERIFY_TRUE( pIDList != NULL );
SHGetPathFromIDList(pIDList, pBuffer);
strFolder = pBuffer;
LPMALLOC lpMalloc = NULL;
if( SUCCEEDED(SHGetMalloc(&lpMalloc)) )
{
//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();
}
hr = S_OK;
}
my_catch;
return hr;
}
#4
不要调用
updatedata(0);
应该调用
updatedata(TRUE);
updatedata(0);
应该调用
updatedata(TRUE);
#5
edit ctrl里面填写一些内容后添加updatedata(0).
2同意楼上
2同意楼上
#6
同意楼上的楼上
#7
第一个问题大家都说了
第二个问题,自己写的一个打开文件方法,可以定位到具体的某个文件,当然你第二个参数也可以为空
BOOL OpenFolderAndSelectFile(const CString& strFileFolder, const CString& strFileName)
{
HRESULT hr;
HWND hWnd = NULL;
IDispatch *pDoc = NULL;
IShellLink *pIsl = NULL;
ITEMIDLIST *pIdlFile = NULL, *pIdlFolder = NULL;
HMODULE hModule1 = NULL, hModule2 = NULL;
IWebBrowserApp *pIWebBrowserApp = NULL;
IShellFolderViewDual *pIShellFolderViewDual = NULL;
::CoInitialize(NULL);
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pIsl);
if ( hr != S_OK )
return FALSE;
if ( !strFileName.IsEmpty() )
{
if ( pIsl->SetPath(strFileName) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFile) != S_OK )
{
pIsl->Release();
return FALSE;
}
hModule1 = LoadLibrary(_T("Shell32"));
if ( hModule1 == NULL )
{
pIsl->Release();
return FALSE;
}
FreeLibrary(hModule1);
}
if ( !strFileFolder.IsEmpty() )
{
if ( pIsl->SetPath(strFileFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
pIsl->Release();
hModule2 = LoadLibrary(_T("Shdocvw"));
if ( hModule2 == NULL )
return FALSE;
pfnSHGETIDISPATCHFORFOLDER pfnSHGetIDispatchForFolder = (pfnSHGETIDISPATCHFORFOLDER)GetProcAddress(hModule2, "SHGetIDispatchForFolder");
if ( pfnSHGetIDispatchForFolder )
{
hr = pfnSHGetIDispatchForFolder(pIdlFolder, &pIWebBrowserApp);
if ( hr != S_OK )
{
FreeLibrary(hModule2);
return FALSE;
}
hr = pIWebBrowserApp->get_HWND((SHANDLE_PTR*)&hWnd);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
::SetForegroundWindow(hWnd);
::ShowWindow(hWnd, SW_SHOWNORMAL);
hr = pIWebBrowserApp->get_Document(&pDoc);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
hr = pDoc->QueryInterface(IID_IShellFolderViewDual, (LPVOID*)&pIShellFolderViewDual);
if ( hr != S_OK )
{
pDoc->Release();
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
pDoc->Release();
pIWebBrowserApp->Release();
COleVariant bszFile(pIdlFile);
if(pIShellFolderViewDual != NULL)
{
/// 选中相应的选项
pIShellFolderViewDual->SelectItem(bszFile, 0x1d);
pIShellFolderViewDual->Release();
}
FreeLibrary(hModule2);
return TRUE;
}
FreeLibrary(hModule2);
}
return FALSE;
}
第二个问题,自己写的一个打开文件方法,可以定位到具体的某个文件,当然你第二个参数也可以为空
BOOL OpenFolderAndSelectFile(const CString& strFileFolder, const CString& strFileName)
{
HRESULT hr;
HWND hWnd = NULL;
IDispatch *pDoc = NULL;
IShellLink *pIsl = NULL;
ITEMIDLIST *pIdlFile = NULL, *pIdlFolder = NULL;
HMODULE hModule1 = NULL, hModule2 = NULL;
IWebBrowserApp *pIWebBrowserApp = NULL;
IShellFolderViewDual *pIShellFolderViewDual = NULL;
::CoInitialize(NULL);
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pIsl);
if ( hr != S_OK )
return FALSE;
if ( !strFileName.IsEmpty() )
{
if ( pIsl->SetPath(strFileName) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFile) != S_OK )
{
pIsl->Release();
return FALSE;
}
hModule1 = LoadLibrary(_T("Shell32"));
if ( hModule1 == NULL )
{
pIsl->Release();
return FALSE;
}
FreeLibrary(hModule1);
}
if ( !strFileFolder.IsEmpty() )
{
if ( pIsl->SetPath(strFileFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
pIsl->Release();
hModule2 = LoadLibrary(_T("Shdocvw"));
if ( hModule2 == NULL )
return FALSE;
pfnSHGETIDISPATCHFORFOLDER pfnSHGetIDispatchForFolder = (pfnSHGETIDISPATCHFORFOLDER)GetProcAddress(hModule2, "SHGetIDispatchForFolder");
if ( pfnSHGetIDispatchForFolder )
{
hr = pfnSHGetIDispatchForFolder(pIdlFolder, &pIWebBrowserApp);
if ( hr != S_OK )
{
FreeLibrary(hModule2);
return FALSE;
}
hr = pIWebBrowserApp->get_HWND((SHANDLE_PTR*)&hWnd);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
::SetForegroundWindow(hWnd);
::ShowWindow(hWnd, SW_SHOWNORMAL);
hr = pIWebBrowserApp->get_Document(&pDoc);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
hr = pDoc->QueryInterface(IID_IShellFolderViewDual, (LPVOID*)&pIShellFolderViewDual);
if ( hr != S_OK )
{
pDoc->Release();
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
pDoc->Release();
pIWebBrowserApp->Release();
COleVariant bszFile(pIdlFile);
if(pIShellFolderViewDual != NULL)
{
/// 选中相应的选项
pIShellFolderViewDual->SelectItem(bszFile, 0x1d);
pIShellFolderViewDual->Release();
}
FreeLibrary(hModule2);
return TRUE;
}
FreeLibrary(hModule2);
}
return FALSE;
}
#8
楼主第二个问题可以用
ShellExecute()
第二个参数为"explore"
ShellExecute()
第二个参数为"explore"
#1
不要UpdateData()更新所有控件,你可以选择文件对话框等后,GetWindowText/SetWindowText来更新特定控件
#2
打开之前先updatedata一下,将控件中的数据更新到变量中
#3
选择路径
HRESULT CFileSystemTool::SelectFolder(CString& strFolder)
{
REGISTER_FUNC( CFileSystemTool::SelectFolder(CString& strFolder) );
HRESULT hr = E_FAIL;
strFolder.Empty();
try
{
BROWSEINFO bi;
TCHAR pBuffer[MAX_PATH];
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = pBuffer;
bi.lpszTitle = _T("选择路径");
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.iImage = NULL ;
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
VERIFY_TRUE( pIDList != NULL );
SHGetPathFromIDList(pIDList, pBuffer);
strFolder = pBuffer;
LPMALLOC lpMalloc = NULL;
if( SUCCEEDED(SHGetMalloc(&lpMalloc)) )
{
//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();
}
hr = S_OK;
}
my_catch;
return hr;
}
#4
不要调用
updatedata(0);
应该调用
updatedata(TRUE);
updatedata(0);
应该调用
updatedata(TRUE);
#5
edit ctrl里面填写一些内容后添加updatedata(0).
2同意楼上
2同意楼上
#6
同意楼上的楼上
#7
第一个问题大家都说了
第二个问题,自己写的一个打开文件方法,可以定位到具体的某个文件,当然你第二个参数也可以为空
BOOL OpenFolderAndSelectFile(const CString& strFileFolder, const CString& strFileName)
{
HRESULT hr;
HWND hWnd = NULL;
IDispatch *pDoc = NULL;
IShellLink *pIsl = NULL;
ITEMIDLIST *pIdlFile = NULL, *pIdlFolder = NULL;
HMODULE hModule1 = NULL, hModule2 = NULL;
IWebBrowserApp *pIWebBrowserApp = NULL;
IShellFolderViewDual *pIShellFolderViewDual = NULL;
::CoInitialize(NULL);
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pIsl);
if ( hr != S_OK )
return FALSE;
if ( !strFileName.IsEmpty() )
{
if ( pIsl->SetPath(strFileName) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFile) != S_OK )
{
pIsl->Release();
return FALSE;
}
hModule1 = LoadLibrary(_T("Shell32"));
if ( hModule1 == NULL )
{
pIsl->Release();
return FALSE;
}
FreeLibrary(hModule1);
}
if ( !strFileFolder.IsEmpty() )
{
if ( pIsl->SetPath(strFileFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
pIsl->Release();
hModule2 = LoadLibrary(_T("Shdocvw"));
if ( hModule2 == NULL )
return FALSE;
pfnSHGETIDISPATCHFORFOLDER pfnSHGetIDispatchForFolder = (pfnSHGETIDISPATCHFORFOLDER)GetProcAddress(hModule2, "SHGetIDispatchForFolder");
if ( pfnSHGetIDispatchForFolder )
{
hr = pfnSHGetIDispatchForFolder(pIdlFolder, &pIWebBrowserApp);
if ( hr != S_OK )
{
FreeLibrary(hModule2);
return FALSE;
}
hr = pIWebBrowserApp->get_HWND((SHANDLE_PTR*)&hWnd);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
::SetForegroundWindow(hWnd);
::ShowWindow(hWnd, SW_SHOWNORMAL);
hr = pIWebBrowserApp->get_Document(&pDoc);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
hr = pDoc->QueryInterface(IID_IShellFolderViewDual, (LPVOID*)&pIShellFolderViewDual);
if ( hr != S_OK )
{
pDoc->Release();
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
pDoc->Release();
pIWebBrowserApp->Release();
COleVariant bszFile(pIdlFile);
if(pIShellFolderViewDual != NULL)
{
/// 选中相应的选项
pIShellFolderViewDual->SelectItem(bszFile, 0x1d);
pIShellFolderViewDual->Release();
}
FreeLibrary(hModule2);
return TRUE;
}
FreeLibrary(hModule2);
}
return FALSE;
}
第二个问题,自己写的一个打开文件方法,可以定位到具体的某个文件,当然你第二个参数也可以为空
BOOL OpenFolderAndSelectFile(const CString& strFileFolder, const CString& strFileName)
{
HRESULT hr;
HWND hWnd = NULL;
IDispatch *pDoc = NULL;
IShellLink *pIsl = NULL;
ITEMIDLIST *pIdlFile = NULL, *pIdlFolder = NULL;
HMODULE hModule1 = NULL, hModule2 = NULL;
IWebBrowserApp *pIWebBrowserApp = NULL;
IShellFolderViewDual *pIShellFolderViewDual = NULL;
::CoInitialize(NULL);
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pIsl);
if ( hr != S_OK )
return FALSE;
if ( !strFileName.IsEmpty() )
{
if ( pIsl->SetPath(strFileName) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFile) != S_OK )
{
pIsl->Release();
return FALSE;
}
hModule1 = LoadLibrary(_T("Shell32"));
if ( hModule1 == NULL )
{
pIsl->Release();
return FALSE;
}
FreeLibrary(hModule1);
}
if ( !strFileFolder.IsEmpty() )
{
if ( pIsl->SetPath(strFileFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
if ( pIsl->GetIDList(&pIdlFolder) != S_OK )
{
pIsl->Release();
return FALSE;
}
pIsl->Release();
hModule2 = LoadLibrary(_T("Shdocvw"));
if ( hModule2 == NULL )
return FALSE;
pfnSHGETIDISPATCHFORFOLDER pfnSHGetIDispatchForFolder = (pfnSHGETIDISPATCHFORFOLDER)GetProcAddress(hModule2, "SHGetIDispatchForFolder");
if ( pfnSHGetIDispatchForFolder )
{
hr = pfnSHGetIDispatchForFolder(pIdlFolder, &pIWebBrowserApp);
if ( hr != S_OK )
{
FreeLibrary(hModule2);
return FALSE;
}
hr = pIWebBrowserApp->get_HWND((SHANDLE_PTR*)&hWnd);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
::SetForegroundWindow(hWnd);
::ShowWindow(hWnd, SW_SHOWNORMAL);
hr = pIWebBrowserApp->get_Document(&pDoc);
if ( hr != S_OK )
{
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
hr = pDoc->QueryInterface(IID_IShellFolderViewDual, (LPVOID*)&pIShellFolderViewDual);
if ( hr != S_OK )
{
pDoc->Release();
pIWebBrowserApp->Release();
FreeLibrary(hModule2);
return FALSE;
}
pDoc->Release();
pIWebBrowserApp->Release();
COleVariant bszFile(pIdlFile);
if(pIShellFolderViewDual != NULL)
{
/// 选中相应的选项
pIShellFolderViewDual->SelectItem(bszFile, 0x1d);
pIShellFolderViewDual->Release();
}
FreeLibrary(hModule2);
return TRUE;
}
FreeLibrary(hModule2);
}
return FALSE;
}
#8
楼主第二个问题可以用
ShellExecute()
第二个参数为"explore"
ShellExecute()
第二个参数为"explore"