NO

Author Topic: FileFolderBrowse example  (Read 499 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1640
FileFolderBrowse example
« on: October 26, 2016, 04:44:05 pm »
I didn't find this example in this forum:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>

#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "comdlg32.lib")

static UINT_PTR CALLBACK OFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
if (uiMsg == WM_INITDIALOG) {
HWND hWnd = GetParent(hdlg);
if (hWnd) {
HWND hCtl;
hCtl = GetDlgItem(hWnd, 0x441);
ShowWindow(hCtl, SW_HIDE);
hCtl = GetDlgItem(hWnd, 0x470);
ShowWindow(hCtl, SW_HIDE);
hCtl = GetDlgItem(hWnd, 0x442);
ShowWindow(hCtl, SW_HIDE);
hCtl = GetDlgItem(hWnd, 0x47C);
ShowWindow(hCtl, SW_HIDE);
}
}
return 0;
}

static BOOL FileFolderBrowse(HWND hwnd, TCHAR *szFolder, TCHAR *szTitle)
{
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = TEXT("Folder only\0$$$.$$$\0\0");
ofn.lpstrFile = szFolder;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY
| OFN_ENABLEHOOK | OFN_ENABLESIZING | OFN_DONTADDTORECENT;
ofn.lpstrTitle = szTitle;
ofn.lpfnHook = OFNHookProc;
#if (_WIN32_WINNT >= 0x0500)
ofn.FlagsEx = OFN_EX_NOPLACESBAR;
#endif
lstrcpy(szFolder, "Folder$$$");
if (GetOpenFileName(&ofn)) {
*(szFolder + ofn.nFileOffset) = 0; // strip fake file
return TRUE;
} else
return FALSE;
}

int __cdecl WinMainCRTStartup(void)
{
TCHAR szFolder[MAX_PATH];
if (FileFolderBrowse(0, szFolder, TEXT("Select folder"))) {
MessageBox(0, szFolder, 0, MB_OK);
}
ExitProcess(0);
}
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1612
Re: FileFolderBrowse example
« Reply #1 on: October 27, 2016, 12:43:02 pm »
Nice!  :D

Try also this:
Code: [Select]
#define UNICODE 1
#define _UNICODE 1
#include <windows.h>
#include <shobjidl.h>

#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "ole32.lib")

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hInst, PWSTR pCmdLine, int nCmdShow)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog *pFileOpen;

// Create the FileOpenDialog object.
hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_ALL, &IID_IFileOpenDialog, (void **)(&pFileOpen));

if (SUCCEEDED(hr))
{
// We browse only for folders.
// Comment line below to browse for all.
hr = pFileOpen->lpVtbl->SetOptions(pFileOpen, FOS_PICKFOLDERS);

// Show the Open dialog box.
hr = pFileOpen->lpVtbl->Show(pFileOpen, NULL);

// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->lpVtbl->GetResult(pFileOpen, &pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->lpVtbl->GetDisplayName(pItem, SIGDN_FILESYSPATH, &pszFilePath);

// Display the file name to the user.
if (SUCCEEDED(hr))
{
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
CoTaskMemFree(pszFilePath);
}
else
if (hr == E_INVALIDARG)
MessageBox(NULL, L"This is a system object, or an invalid path", L"Error", MB_OK|MB_ICONHAND);

pItem->lpVtbl->Release(pItem);
}
}
pFileOpen->lpVtbl->Release(pFileOpen);
}
CoUninitialize();
}
return 0;
}
« Last Edit: October 27, 2016, 12:52:18 pm by frankie »