Last active
May 22, 2016 10:19
-
-
Save tkymx/08762c662e18e9722f616dc1fbb04ccf to your computer and use it in GitHub Desktop.
ファイル読み込みダイアログ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <shlobj.h> | |
#include <map> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include "utillity.h" | |
static int CALLBACK SHBrowseProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData) | |
{ | |
if (uMsg == BFFM_INITIALIZED && lpData) | |
{ | |
// デフォルトで選択させるパスの指定 | |
SendMessage(hWnd, BFFM_SETSELECTION, TRUE, lpData); | |
} | |
return 0; | |
} | |
static void SHFree(ITEMIDLIST* pidl) | |
{ | |
IMalloc* pMalloc; | |
SHGetMalloc(&pMalloc); | |
if (pMalloc) | |
{ | |
pMalloc->Free(pidl); | |
pMalloc->Release(); | |
} | |
} | |
BOOL GetOpenDirectoryName(HWND hwndOwner, LPCTSTR lpszDefaultFolder, LPTSTR lpszBuffer, DWORD dwBufferSize) | |
{ | |
BROWSEINFO bi; | |
ZeroMemory(&bi, sizeof(BROWSEINFO)); | |
bi.hwndOwner = hwndOwner; | |
bi.lpfn = SHBrowseProc; // コールバック関数を指定 | |
bi.lParam = (LPARAM)lpszDefaultFolder; // デフォルトで選択させておくフォルダを指定 | |
bi.lpszTitle = TEXT("フォルダを選択してください"); // タイトルの指定 | |
ITEMIDLIST* pidl = SHBrowseForFolder(&bi); // フォルダダイアログの起動 | |
if (pidl) | |
{ | |
// 選択されたフォルダ名を取得 | |
TCHAR szSelectedFolder[_MAX_PATH]; | |
SHGetPathFromIDList(pidl, szSelectedFolder); | |
SHFree(pidl); | |
if ((DWORD)lstrlen(szSelectedFolder) < dwBufferSize) | |
{ | |
lstrcpy(lpszBuffer, szSelectedFolder); | |
// フォルダが選択された | |
return TRUE; | |
} | |
} | |
// フォルダは選択されなかった | |
return FALSE; | |
} | |
std::string get_ini(LPCTSTR _filepath, LPCTSTR _key) | |
{ | |
std::ifstream ifs(_filepath); | |
if (!ifs.is_open()) | |
return "c:\\"; | |
std::vector< std::pair<std::string, std::string> > inimap; | |
std::string str; | |
while (std::getline(ifs, str)) | |
{ | |
int index = str.find_first_of("\t"); | |
std::string key = str.substr(0, index); | |
std::string value = str.substr(index + 1, -1); | |
if (key == _key) | |
{ | |
ifs.close(); | |
return value; | |
} | |
} | |
ifs.close(); | |
return ""; | |
} | |
void set_ini(LPCTSTR _filepath, LPCTSTR _key, LPCTSTR _value) | |
{ | |
std::map< std::string, std::string> inimap; | |
std::ifstream ifs(_filepath); | |
if (ifs.is_open()) | |
{ | |
std::string str; | |
while (std::getline(ifs, str)) | |
{ | |
int index = str.find_first_of("\t"); | |
std::string key = str.substr(0, index); | |
std::string value = str.substr(index + 1, -1); | |
inimap.insert(std::make_pair(key, value)); | |
} | |
ifs.close(); | |
} | |
inimap[_key] = _value; | |
std::ofstream ofs(_filepath); | |
if (!ofs.is_open()) | |
return; | |
for (auto pair : inimap) | |
{ | |
ofs << pair.first << "\t" << pair.second << std::endl; | |
} | |
ofs.close(); | |
} | |
std::string get_dir_path(std::string key) | |
{ | |
TCHAR lpszBuffer[256]; | |
std::string init_image_path = get_ini("setting.ini", key.c_str()); | |
if (GetOpenDirectoryName(NULL, init_image_path.c_str(), lpszBuffer, 256) != TRUE) | |
return ""; | |
set_ini("setting.ini", key.c_str(), lpszBuffer); | |
return lpszBuffer; | |
} | |
std::vector<std::string> get_file_path_in_dir(const std::string& dir_name, const std::string& extension , Direction direction) | |
{ | |
HANDLE hFind; | |
WIN32_FIND_DATA win32fd;//defined at Windwos.h | |
std::vector<std::string> file_names; | |
//拡張子の設定 | |
std::string search_name = dir_name + "\\*." + extension; | |
hFind = FindFirstFile(search_name.c_str(), &win32fd); | |
if (hFind == INVALID_HANDLE_VALUE) { | |
throw std::runtime_error("file not found"); | |
} | |
do { | |
if (win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | |
} | |
else { | |
if (strstr(win32fd.cFileName, get_str(direction)) != NULL){ | |
file_names.push_back(win32fd.cFileName); | |
} | |
} | |
} while (FindNextFile(hFind, &win32fd)); | |
FindClose(hFind); | |
return file_names; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment