Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <windows.h>
#include <shobjidl.h>
#include <Commdlg.h>
#include <stdio.h>
//
// Global Variables and declarations.
//
UINT_PTR CALLBACK OfnHookProcOldStyle(
_In_ HWND hdlg,
_In_ UINT uiMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
//system("pause");
printf("uMsg: 0x%04x\n", uiMsg);
// MessageBox(NULL, msg, "hook", MB_OK);
return 0;
}
UINT CALLBACK OfnHookProc(HWND hDlg, UINT uMsg, UINT /*wParam*/, LONG lParam)
{
printf("uMsg: 0x%04x\n", uMsg);
switch (uMsg)
{
case WM_NOTIFY:
{
OFNOTIFY* pofNotify = (OFNOTIFY*)lParam;
switch (pofNotify->hdr.code)
{
case CDN_FOLDERCHANGE:
{
/* LPTSTR pszPathName;
pszPathName = new TCHAR[_MAX_DIR];
HWND hTrueDlg = GetParent(hDlg);
SendMessage(hTrueDlg, CDM_GETFOLDERPATH, _MAX_PATH, (LONG)pszPathName);
SetDlgItemText(hDlg, IDC_STATIC_HINT, pszPathName);
delete[] pszPathName;*/
return TRUE;
}
}
}
break;
case 0x001F:
return TRUE;
break;
case 0x000a:
return TRUE;
break;
}
return FALSE;
}
void test_getopenfilename() {
OPENFILENAME ofn;
// a another memory buffer to contain the file name
char szFile[1024];
// open a file name
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK | OFN_EXPLORER;
// ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK;
ofn.lpfnHook = OfnHookProcOldStyle;
GetOpenFileName(&ofn);
// Now simply display the file name
printf("file name: %s\n", ofn.lpstrFile);
}
void test_ifiledialog() {
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, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.
if (SUCCEEDED(hr))
{
wprintf(L"File Path: %ls\n", pszFilePath);
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
}
//int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
int main()
{
test_getopenfilename();
//test_ifiledialog();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.