// This is getFolderPath.h
#include "shlobj.h"
#include <string>
class getFolderPath
{
public:
getFolderPath(void);
~getFolderPath(void);
static bool GetFolderPath(std::string& folderpath, HWND);
};
// This is getFolderPath.cpp
// You have to add "stdafx.h" library if you make your project in Visual Stdio
#include "getFolderPath.h"
getFolderPath::getFolderPath(void)
{
/*hOwner = hWnd;*/
}
getFolderPath::~getFolderPath(void)
{
}
bool getFolderPath::GetFolderPath(std::string& folderpath,HWND hOwner)
{
bool retVal = false;
// The BROWSEINFO struct tells the shell
// how it should display the dialog.
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_USENEWUI;
bi.hwndOwner = hOwner;
bi.lpszTitle = NULL;
// must call this if using BIF_USENEWUI
::OleInitialize(NULL);
// Show the dialog and get the itemIDList for the selected folder.
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);
if(pIDL != NULL)
{
// Create a buffer to store the path, then get the path.
char buffer[_MAX_PATH] = {L'\0'};
if(::SHGetPathFromIDListA(pIDL, buffer) != 0)
{
// Set the string value.
folderpath = buffer;
retVal = true;
}
// free the item id list
CoTaskMemFree(pIDL);
}
::OleUninitialize();
return retVal;
}
How you can use my class.
+ Create two file .h and .cpp with their content as i wrote above.
+ Coppy two file into your project, add "getFolderPath.h" library in you programme
+ In main programme or when you process an event such as click to a button, you report an std::string variable to call GetFolderPath function. Example: std::string path = "";
- If your application is console application, hOwner = NULL - If it is a window application or a MFC application, you have to use it's HWND for GetFolderPath function. You can get it's HWND by this function: this->GetSafeHWND();
If success, (GetFolderPath return true) path will contain your path to folder which you choose
I use static function: static bool GetFolderPath(std::string& folderpath, HWND); so you can call it although you don't create new objectThis is example which i do with console application:
#include "getPathFolder.h"
#include <string>
int main()
{
std::string sPath = "";
if(getPathFoder::GetFolderPath(sPath,NULL))
{
printf("You have just chosen this folder: %s",sPath.c_str());
}
else
{
// Process when can get folder's path
}
return 1;
}
Best wishes for you, thanks for visiting my blog!
Do your best, the rest will come!
Không có nhận xét nào:
Đăng nhận xét