I have a form and I am trying to open a folder browser dialog that is positioned on top of the form and not in the center of the screen. (Windows 7, Visual Studio Express 2012)
the form name is "form"
I have tried the following with a FolderBrowserDialog:
browseFolderDialog = gcnew FolderBrowserDialog();
DialogResult result = browseFolderDialog->ShowDialog(form);
I have also tried using SHBrowseForFolder providing the handle of the form as hwnd:
System::String^ BrowseUtility::getFolder(System::Windows::Forms::Form^ form)
{
//HWND hwnd = GetForegroundWindow();
System::Windows::Forms::Control ^ctrl1 = form; // Form derives from Control
System::IntPtr wrappedHandle = form->Handle;
void *windowHandle1 = wrappedHandle.ToPointer();
HWND hwnd = reinterpret_cast<HWND>(windowHandle1);
System::String^ result = L"-1";
TCHAR szFolder[MAX_PATH];
if (GetFolderSelection(hwnd, szFolder, TEXT("Please select a folder.")))
{
result = gcnew System::String(szFolder);
}
return result;
}
BOOL BrowseUtility::GetFolderSelection(HWND hWnd, LPTSTR szBuf, LPCTSTR szTitle)
{
LPITEMIDLIST pidl = NULL;
BROWSEINFO bi = { 0 };
BOOL bResult = FALSE;
bi.hwndOwner = hWnd;
bi.pszDisplayName = szBuf;
bi.pidlRoot = NULL;
bi.lpszTitle = szTitle;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
if ((pidl = SHBrowseForFolder(&bi)) != NULL)
{
bResult = SHGetPathFromIDList(pidl, szBuf);
CoTaskMemFree(pidl);
}
return bResult;
}
Note I have even tried providing hWnd as HWND hwnd = GetForegroundWindow();
The folder browser still shows in the center of the screen.
Any ideas would be a help - I know this can't be that hard