zoukankan      html  css  js  c++  java
  • Winform窗体控件自适应大小

    自己写的winform窗体自适应大小代码,代码比较独立,很适合贴来贴去不会对原有程序造成影响,可以直接继承此类或者把代码复制到自己的代码里面直接使用

    借鉴了网上的一些资料,最后采用重写WndProc方法,这样可以兼顾窗体拖拽调整窗体大小和最大化、最小化方法,而且代码比较简练,代码侵入性较小

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace LzRabbit
    {
        public class AutoResizeForm : Form
        {
            const int WM_SYSCOMMAND = 0X112;//274
            const int SC_MAXIMIZE = 0XF030;//61488
            const int SC_MINIMIZE = 0XF020;//61472
            const int SC_RESTORE = 0XF120; //61728
            const int SC_CLOSE = 0XF060;//61536
            const int SC_RESIZE_Horizontal = 0XF002;//61442
            const int SC_RESIZE_Vertical = 0XF006;//61446
            const int SC_RESIZE_Both = 0XF008;//61448
    
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_SYSCOMMAND)
                {                switch (m.WParam.ToInt32())
                    {
                        case SC_MAXIMIZE:
                        case SC_RESTORE:
                        case SC_RESIZE_Horizontal:
                        case SC_RESIZE_Vertical:
                        case SC_RESIZE_Both:
                            if (WindowState == FormWindowState.Minimized)
                            {
                                base.WndProc(ref m);
                            }
                            else
                            {
                                Size beforeResizeSize = this.Size;
                                base.WndProc(ref m);
                                //窗口resize之后的大小
                                Size afterResizeSize = this.Size;
                                //获得变化比例
                                float percentWidth = (float)afterResizeSize.Width / beforeResizeSize.Width;
                                float percentHeight = (float)afterResizeSize.Height / beforeResizeSize.Height;
                                foreach (Control control in this.Controls)
                                {
                                    //按比例改变控件大小
                                    control.Width = (int)(control.Width * percentWidth);
                                    control.Height = (int)(control.Height * percentHeight);
                                    //为了不使控件之间覆盖 位置也要按比例变化
                                    control.Left = (int)(control.Left * percentWidth);
                                    control.Top = (int)(control.Top * percentHeight);
                                    //改变控件字体大小
                                    control.Font = new Font(control.Font.Name, control.Font.Size * Math.Min(percentHeight, percentHeight), control.Font.Style, control.Font.Unit);
                                }
                            }
                            break;
                        default:
                            base.WndProc(ref m);
                            break;
                    }
                }
                else
                {
                    base.WndProc(ref m);
                }
            }
        }
    }
  • 相关阅读:
    php isset()与empty()的使用
    , , 的区别
    让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    当导航条滚动到顶部时固定到顶部
    JS:window.onload的使用
    jquery里面.length和.size()有什么区别
    2017.4.13(内置函数)作业
    文件内容的增删改查
    用户登陆程序,密码三次错误自动锁定用户名。
  • 原文地址:https://www.cnblogs.com/lzrabbit/p/3333456.html
Copyright © 2011-2022 走看看