Article View: borland.public.cppbuilder.nativeapi
Article #4212Re: Positioning the SHBrowseForFolder window
From: Remy Lebeau (TeamB)
Date: Tue, 07 Mar 2006 07:03
Date: Tue, 07 Mar 2006 07:03
159 lines
5729 bytes
5729 bytes
"Dennis Jones" <nospam@nospam.com> wrote in message
news:440d053b$1@newsgroups.borland.com...
> Yes, of course it does, because you failed to set the
> BIF_NEWDIALOGSTYLE flag, without which you
> cannot resize the browse window!
You never said anything earlier about doing that. Next time you want
someone to reproduce your problem, always provide specific details about
what you are actually doing in the first place.
> If I couldn't resize the browse window, I wouldn't be having any trouble
> either! The whole point of this discussion was to find out why it won't
> restore to its previous size *before* I need to center it. So, try
> setting the BIF_NEWDIALOGSTYLE flag in your code first
Using window hooks and subclassing, you can catch when the window is
becoming visible and make it centered before it is shown onscreen.
The following code works for me (yes, I realize that this is a bit complex
for the task, but it works nontheless):
#ifdef STRICT
#define MYDLGWNDPROC WNDPROC
#else
#define MYDLGWNDPROC FARPROC
#endif
MYDLGWNDPROC OldDlgProc = NULL;
HWND hFolderDlg = NULL;
HHOOK hHook = NULL;
LRESULT CALLBACK DlgWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
lParam)
{
if( uMsg == WM_SHOWWINDOW )
{
RECT r;
::GetWindowRect(hwnd, &r);
int iWidth = (r.right-r.left);
int iHeight = (r.bottom-r.top);
::MoveWindow(
hwnd,
Form1->Left + (Form1->Width/2)-(iWidth/2),
Form1->Top + (Form1->Height/2)-(iHeight/2),
iWidth,
iHeight,
FALSE);
}
return ::CallWindowProc(OldDlgProc, hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if( nCode == HCBT_CREATEWND )
{
HWND hDlg = (HWND) wParam;
CBT_CREATEWND *lpcw = (CBT_CREATEWND*) lParam;
if( lpcw->lpcs->hwndParent == Form1->Handle )
{
hFolderDlg = hDlg;
OldDlgProc = (DLGWNDPROC) ::SetWindowLong(hDlg, GWL_WNDPROC,
(LONG) DlgWndProc);
}
}
else if( nCode == HCBT_DESTROYWND )
{
if( (hFolderDlg) && (hFolderDlg == (HWND) wParam) )
{
::SetWindowLong(hFolderDlg, GWL_WNDPROC, (LONG) OldDlgProc);
hFolderDlg = NULL;
}
}
return ::CallNextHookEx(hHook, nCode, wParam, lParam);
}
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM /*lParam*/,
LPARAM lpData)
{
if( uMsg == BFFM_INITIALIZED )
{
LPITEMIDLIST pidl = NULL;
SHPathToPIDL((char*) lpData, &pidl);
if( pidl )
{
::SendMessage(hwnd, BFFM_SETSELECTION, FALSE, (LPARAM)
pidl);
::CoTaskMemFree(pidl);
}
}
return 0;
}
bool BrowseForFolder(HWND hWnd, char *buf, char *prompt)
{
bool result = false;
LPITEMIDLIST pidlDrives = NULL;
if( SUCCEEDED(::SHGetSpecialFolderLocation(hWnd, CSIDL_DRIVES,
&pidlDrives)) )
{
if( SUCCEEDED(::CoInitialize(NULL)) )
{
hHook = ::SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, NULL,
::GetCurrentThreadId());
if( hHook )
{
BROWSEINFO bi = {0};
bi.hwndOwner = hWnd;
bi.pidlRoot = pidlDrives;
bi.pszDisplayName = buf;
bi.lpszTitle = prompt;
bi.ulFlags = BIF_RETURNONLYFSDIRS |
BIF_RETURNFSANCESTORS | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM) buf;
LPITEMIDLIST pidlBrowse = ::SHBrowseForFolder(&bi);
if( pidlBrowse )
{
result = ::SHGetPathFromIDList(pidlBrowse, buf);
::CoTaskMemFree(pidlBrowse);
}
::UnhookWindowsHookEx(hHook);
hHook = NULL;
}
::CoUninitialize();
}
::CoTaskMemFree(pidlDrives);
}
return result;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char buf[MAX_PATH+1];
strcpy(buf, "C:\\Windows");
if( BrowseForFolder(Handle, buf, "Testing") )
ShowMessage(buf);
else
ShowMessage("none");
}
Gambit
Technical Details
Message-ID:
<440d2feb$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<440cc788$1@newsgroups.borland.com> <440cd987$1@newsgroups.borland.com> <440d053b$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,722adf2ba600e8df X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <440cc788$1@newsgroups.borland.com> <440cd987$1@newsgroups.borland.com> <440d053b$1@newsgroups.borland.com> Subject: Re: Positioning the SHBrowseForFolder window Date: Mon, 6 Mar 2006 23:03:08 -0800 Lines: 162 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <440d2feb$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1141714923 68.69.17.41 (6 Mar 2006 23:02:03 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail