如何在Form
最大化而不最大化的情况下获得客户端大小?
例如,我想创建一个Bitmap
与最大化Form
的客户端大小相同的大小,我该怎么做?
尝试
Screen.FromControl(this).GetWorkingArea();
计算大小(没有任务栏)然后减去 Forms ClientSize / Size 之间的差异。希望有效,没有测试过。
更新:
有点hacky,但我尝试了它并且它有效。
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;
更新2:
这是一个更好的解决方案。我禁用窗体的绘制,最大化它,获取客户区,将其恢复正常并返回结果。效果很好,没有闪烁什么的。
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);
}
试试下面的链接,
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.workingarea%28VS.71%29.aspx