I want the FolderBrowserDialog to be larger when first shown so that it can show more than three-level directory. Is that anyway to do it or is there anyway to override a similiar dialog?
Asked
Active
Viewed 4,977 times
2 Answers
0
I shamelessly copied Hans's code and modified it to achieve what you want.
private void button1_Click(object sender, EventArgs e)
{
using (new SizeWinDialog(this)//This refers to wpf Window
{
PreferredSize = new Size(150, 150)//Change this size to whatever you want
})
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
}
}
class SizeWinDialog : IDisposable
{
private int mTries = 0;
private Window mOwner;
public SizeWinDialog(Window owner)
{
mOwner = owner;
owner.Dispatcher.BeginInvoke(new Action(findDialog));
}
public Size PreferredSize { get; set; }
private void findDialog()
{
// Enumerate windows to find the message box
if (mTries < 0) return;
EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero))
{
if (++mTries < 10) mOwner.Dispatcher.BeginInvoke(new MethodInvoker(findDialog));
}
}
private bool checkWindow(IntPtr hWnd, IntPtr lp)
{
// Checks if <hWnd> is a dialog
StringBuilder sb = new StringBuilder(260);
GetClassName(hWnd, sb, sb.Capacity);
if (sb.ToString() != "#32770") return true;
// Got it
RECT dlgRect;
GetWindowRect(hWnd, out dlgRect);
SetWindowPos(new HandleRef(this, hWnd), new HandleRef(), dlgRect.Left, dlgRect.Top, PreferredSize.Width, PreferredSize.Height, 20 | 2);
return false;
}
public void Dispose()
{
mTries = -1;
}
// P/Invoke declarations
private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy,
int flags);
private struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
}

Community
- 1
- 1

Sriram Sakthivel
- 72,067
- 7
- 111
- 189
-
Thank you for your help. It's pleasant because this is the first answer I got on this platform. I'm sorry I didn't say my app is developed using WPF. I tried to apply this method to my app but I find it needs a owner of a form instead of a window which don't have the method of BeginInvoke. I am not familiar with Winform and don't know how to solve it. – EleganceKill May 27 '14 at 08:02
-
@EleganceKill That's not a big deal, now it should work with Wpf `Window`. Updated my code. – Sriram Sakthivel May 27 '14 at 08:10
-
It works,but the size won't change.I find the PreferredSize isn't used in the class.You can modify your answer replacing 100 with Width and height of PreferredSize.(I don't know the exact meaning of each parameter) Thank you very much. – EleganceKill May 27 '14 at 08:35
-
Sorry,I have to ask you again that how to set the position because it seems not to be the center of screen...-.-! – EleganceKill May 27 '14 at 08:47
-
Take a look at [SetWindowPos](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx) api, You need to modify 3rd and 4th parameters inside `checkWindow` method. Also refer the linked answer by Hans passant, that does the center. I removed it :( – Sriram Sakthivel May 27 '14 at 08:49
0
I converted the code sample above back to WinForms from the WPF version that is shown. I also created two companion classes that will size a system dialog and will place it at a specific offset from the parent form. They are used like this:
using (new OffsetWinDialog(this) { PreferredOffset = new Point(75, 75 )})
using (new SizeWinDialog(this) { PreferredSize = new Size(400, 600)})
{
DialogResult result = dlgFolderBrowser.ShowDialog();
if (result == DialogResult.Cancel)
return;
}
The original post (below) shows how to place the system dialog at the center of the parent. Together, these three classes will let you control the initial size and position of the system dialogs. Rather than repeat the code here, please refer to the following thread.
Winforms-How can I make MessageBox appear centered on MainForm?

Community
- 1
- 1

RonSanderson
- 41
- 6