在最小化和恢复父表单时调整子表单的大小。 [英] Child forms resized when parent form is minimized and restored.

查看:77
本文介绍了在最小化和恢复父表单时调整子表单的大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



最小化和恢复MainForm时,子表单的大小会发生变化。



我已将Form1设置为Form2的所有者。我已将ShowOnTaskBar设置为Form2的false。按照以下步骤更改窗体的大小。



  1. 运行示例。
  2. 最大化form2
  3. 单击任务栏中的应用程序以最小化此应用程序。
  4. 恢复应用程序。

观察结果:



Form2大小已更改。大小已在下面的调用堆栈下更改。



            System.Windows.Forms.Control.OnSizeChanged(System.EventArgs e)      


    ;         System.Windows.Forms.Control.UpdateBounds(int x,int y,int width,int height,int clientWidth,int clientHeight)           


    ;         System.Windows.Forms.Control.UpdateBounds() 


    ;         System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)


            System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m)          


    ;         System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m)   


    ;         System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m)           


    ;         System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   


    ;         System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd,int msg,System.IntPtr wparam,System.IntPtr lparam)  


    ;         [原生到管理过渡]  


    ;         [管理到原生过渡]  


    ;         System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m)        


    ;         System.Windows.Forms.Form.DefWndProc(ref System.Windows.Forms.Message m)          


    ;         System.Windows.Forms.Control.WmWindowPosChanged(ref System.Windows.Forms.Message m) 


    ;         System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)


            System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m)          


    ;         System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m)   


    ;         System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m)           


    ;         System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   


    ;         System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd,int msg,System.IntPtr wparam,System.IntPtr lparam)  



我还附上了样本,可以从以下位置下载。



示例:
https://1drv.ms/u/s!Aj7X0FcGE8p_gxDmg-r6wAYmDVvz



你可以告诉我如何在最小化之前恢复具有相同窗口状态的Form2吗?如果您需要进一步的详细信息,请告知我们。





解决方案

您好,


解决方案来自:http://stackoverflow.com/questions/18234312/maximized-owned-form-not-restoring-correctly


通过 覆盖WndProc方法来拦截Windows消息。请填写下面的form2。

 private FormWindowState _lastState; 
public FormWindowState LastWindowState {get {return _lastState;

private void Form2_VisibleChanged(object sender,EventArgs e)
{
WindowState = _lastState;
}
protected override void WndProc(ref message m)
{
const Int32 WM_SYSCOMMAND = 0x0112;
const Int32 SC_MAXIMIZE = 0xF030;
const Int32 SC_MINIMIZE = 0xF020;
const Int32 SC_RESTORE = 0xF120;

switch(m.Msg)
{
case WM_SYSCOMMAND:
{
Int32 command = m.WParam.ToInt32()& 0xfff0;
switch(command)
{
case SC_MAXIMIZE:
_lastState = FormWindowState.Maximized;
休息;
case SC_MINIMIZE:
_lastState = FormWindowState.Minimized;
休息;
case SC_RESTORE:
_lastState = FormWindowState.Normal;
休息;
}
}
休息;
}
base.WndProc(ref m);
}

最好的问候,


Bob



Hi,

The size of the child form is changed when minimizing and restoring the MainForm.

I have set the Form1 as owner of the Form2. I have set the ShowOnTaskBar as false for Form2. On following the below steps the size of the windows forms is changed.

  1. Run the sample.
  2. Maximize form2
  3. Click on the application in taskbar to minimize this application.
  4. Restore the application.

Observed result:

Form2 size has been changed. The size has been changed under the below call stack.

            System.Windows.Forms.Control.OnSizeChanged(System.EventArgs e)      

            System.Windows.Forms.Control.UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight)           

            System.Windows.Forms.Control.UpdateBounds() 

            System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)

            System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m)          

            System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m)   

            System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m)           

            System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   

            System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  

            [Native to Managed Transition]  

            [Managed to Native Transition]  

            System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m)        

            System.Windows.Forms.Form.DefWndProc(ref System.Windows.Forms.Message m)          

            System.Windows.Forms.Control.WmWindowPosChanged(ref System.Windows.Forms.Message m) 

            System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)

            System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m)          

            System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m)   

            System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m)           

            System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   

            System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  

I have also attached the sample and this can be downloaded from the below location.

Sample : https://1drv.ms/u/s!Aj7X0FcGE8p_gxDmg-r6wAYmDVvz

Could you please let me know how to restore the Form2 with same window state before it gets minimized? Please let mw know if you need further details on this.


解决方案

Hi,

Solution from :http://stackoverflow.com/questions/18234312/maximized-owned-form-not-restoring-correctly

Via override for WndProc method to intercept windows message. Do below at form2 .

        private FormWindowState _lastState;
        public FormWindowState LastWindowState { get { return _lastState; } }

        private void Form2_VisibleChanged(object sender, EventArgs e)
        {
            WindowState = _lastState;
        }
        protected override void WndProc(ref Message m)
        {
            const Int32 WM_SYSCOMMAND = 0x0112;
            const Int32 SC_MAXIMIZE = 0xF030;
            const Int32 SC_MINIMIZE = 0xF020;
            const Int32 SC_RESTORE = 0xF120;

            switch (m.Msg)
            {
                case WM_SYSCOMMAND:
                    {
                        Int32 command = m.WParam.ToInt32() & 0xfff0;
                        switch (command)
                        {
                            case SC_MAXIMIZE:
                                _lastState = FormWindowState.Maximized;
                                break;
                            case SC_MINIMIZE:
                                _lastState = FormWindowState.Minimized;
                                break;
                            case SC_RESTORE:
                                _lastState = FormWindowState.Normal;
                                break;
                        }
                    }
                    break;
            }
            base.WndProc(ref m);
        }

Best Regards,

Bob


这篇关于在最小化和恢复父表单时调整子表单的大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆