/*
* FolderBrowser.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
#pragma once
#include <sol\Object.h>
#include <sol\String.h>
#include <shlobj.h>
#include <shellapi.h>
namespace SOL {
class FolderBrowser :public Object {
private:
HWND hwnd;
String title;
public:
/**
* Constructor
*/
FolderBrowser(HWND hwnd=NULL) {
this->hwnd = hwnd;
this->title = _T("Please select a folder");
OleInitialize(NULL);
}
public:
/**
* Destructor
*/
~FolderBrowser() {
OleUninitialize();
}
public:
void setTitle(const TCHAR* string) {
this->title = string;
}
public:
/**
* @param startingDir [IN] Starting folder's directory
* @param selectedFolder [OUT] Selected folder's full path
* @return BOOL
*/
BOOL show(const TCHAR* startingDir, String& selectedFullPath)
{
BOOL rc = FALSE;
selectedFullPath = ""; //Initialize to empty string.
LPMALLOC lpMalloc = NULL;
if (FAILED(SHGetMalloc(&lpMalloc))) {
return rc;
}
TCHAR fileName[_MAX_PATH];
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.hwndOwner = this->hwnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = fileName;
bi.lpszTitle = (TCHAR*)(this->title); //
bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_SHAREABLE|\
BIF_USENEWUI|BIF_BROWSEFORCOMPUTER|BIF_VALIDATE;
bi.lpfn = BrowseForFolderCallback;
bi.lParam = (LPARAM)startingDir;
// Display Folder Selection Dialog and wait a user's selection.
LPITEMIDLIST itemIdList = SHBrowseForFolder(&bi);
if (itemIdList != NULL) {
TCHAR* buffer = (TCHAR*)lpMalloc->Alloc(_MAX_PATH);
if (buffer != NULL) {
// Convert itemIdlist to fullPath buffer
if (SHGetPathFromIDList(itemIdList, buffer)) {
selectedFullPath = buffer;
rc = TRUE;
}
}
lpMalloc->Free(buffer);
lpMalloc->Free(itemIdList);
}
lpMalloc->Release();
return rc;
}
private:
/**
*/
static int CALLBACK BrowseForFolderCallback(HWND hWnd, UINT uMsg,
LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED) {
if (lpData !=NULL) {
//lpData = bi.lParam = startingDir
SendMessage(hWnd, BFFM_SETSELECTION, (WPARAM)TRUE, lpData);
}
}
return 0;
}
};
}
|