Reputation: 2436
How to get the client size of a Form
when it's maximized without maximize it?
From example, I want to create a Bitmap
with the same size of the maximized Form
's client size, how can I do that?
Upvotes: 0
Views: 4882
Reputation: 31723
Try
Screen.FromControl(this).GetWorkingArea();
to calculate the size (without taskbar) then substract the difference between the Forms ClientSize / Size. Hope that works, haven't tested it.
Update:
A bit hacky, but I tried it and it works.
var frm = new Form();
frm.Opacity = 100;
frm.WindowState = FormWindowState.Maximized;
frm.Show();
while (!frm.IsHandleCreated)
System.Threading.Thread.Sleep(1);
var result = frm.ClientSize;
frm.Close();
return result;
Update2:
This is a nicer solution. I disable painting of the Form, maximize it, get the client area, set it back to normal and return the result. Works well, without flicker or something.
private static Size GetMaximizedClientSize(Form form)
{
var original = form.WindowState;
try
{
BeginUpdate(form);
form.WindowState = FormWindowState.Maximized;
return form.ClientSize;
}
finally
{
form.WindowState = original;
EndUpdate(form);
}
}
[DllImport("User32.dll")]
private extern static int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private enum Message : int
{
WM_SETREDRAW = 0x000B, // int 11
}
/// <summary>
/// Calls user32.dll SendMessage(handle, WM_SETREDRAW, 0, null) native function to disable painting
/// </summary>
/// <param name="c"></param>
public static void BeginUpdate(Control c)
{
SendMessage(c.Handle, (int)Message.WM_SETREDRAW, new IntPtr(0), IntPtr.Zero);
}
/// <summary>
/// Calls user32.dll SendMessage(handle, WM_SETREDRAW, 1, null) native function to enable painting
/// </summary>
/// <param name="c"></param>
public static void EndUpdate(Control c)
{
SendMessage(c.Handle, (int)Message.WM_SETREDRAW, new IntPtr(1), IntPtr.Zero);
}
Upvotes: 4
Reputation: 6490
Try the following link,
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.workingarea%28VS.71%29.aspx
Upvotes: 2