简体   繁体   English

如何在不最大化表单的情况下获得最大的客户端大小?

[英]How to get a form's maximized client size without maximizing it?

How to get the client size of a Form when it's maximized without maximize it? 如何在最大化而不最大化Form的情况下获取其客户端大小?

From example, I want to create a Bitmap with the same size of the maximized Form 's client size, how can I do that? 从示例中,我想创建一个与最大化Form的客户端大小相同大小的Bitmap ,该怎么办?

Try 尝试

Screen.FromControl(this).GetWorkingArea();

to calculate the size (without taskbar) then substract the difference between the Forms ClientSize / Size. 计算尺寸(没有任务栏),然后减去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: 更新2:

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM