加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#当Winform大小改变时(包括点击窗体最大化),窗体中的控件大小也随之改变

(2018-01-14 23:02:13)
标签:

it

//网上很多人应该在找这段代码,结合几位网友程序片段原创

        private Size beforeResizeSize = Size.Empty;
        private Size endResizeSize = Size.Empty;

        protected override void OnResizeBegin(EventArgs e)
        {
            base.OnResizeBegin(e);
            beforeResizeSize = this.Size;
        }
        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResizeEnd(e);
            //窗口resize之后的大小
            Size endResizeSize = this.Size;
            UpdateControlSizes(beforeResizeSize, endResizeSize);
        }

 

        const int WM_SYSCOMMAND = 0x112;
        const int SC_CLOSE = 0xF060;
        const int SC_MINIMIZE = 0xF020;
        const int SC_MAXIMIZE = 0xF030;
        const int SC_NOMAL = 0xF120;
        const int SC_MAXIMIZE2 = 0xF032;
        const int SC_NOMAL2 = 0xF122;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                if (m.WParam.ToInt32() == SC_MINIMIZE) //是否点击最小化
                {
                    //.....................
                }
                if (m.WParam.ToInt32() == SC_MAXIMIZE || m.WParam.ToInt32() == SC_MAXIMIZE2) //是否点击最大化
                {
                    isJustClickedMaximizebox = true;
                    beforeResizeSize = this.Size;
                }
                if (m.WParam.ToInt32() == SC_NOMAL || m.WParam.ToInt32() == SC_NOMAL2) //是否点击还原
                {
                    isJustClickedNomalbox = true;
                }
                if (m.WParam.ToInt32() == SC_CLOSE) //是否点击关闭
                //.....................
                }
            }
            base.WndProc(ref m);
        }

 

        Boolean isJustClickedMaximizebox = false;
        Boolean isJustClickedNomalbox = false;
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (isJustClickedMaximizebox)
            {
                endResizeSize = this.Size;
                UpdateControlSizes(beforeResizeSize, endResizeSize);
                isJustClickedMaximizebox = false;
            }
            if (isJustClickedNomalbox)
            {
                UpdateControlSizes(endResizeSize, beforeResizeSize);
                isJustClickedNomalbox = false;
            }
        }

 

        private void UpdateControlSizes(Size beforeResizeSize, Size endResizeSize)
        {
            //获得变化比例
            float percentWidth = (float)endResizeSize.Width / beforeResizeSize.Width;
            float percentHeight = (float)endResizeSize.Height / beforeResizeSize.Height;
            foreach (Control control in this.Controls)
            {
                if (control is DataGridView)
                {
                    continue;
                }
                //按比例改变控件大小
                control.Width = (int)(control.Width * percentWidth);
                //为了不使控件之间覆盖 位置也要按比例变化
                control.Left = (int)(control.Left * percentWidth);
            }
        }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有