winform屏幕自適應(yīng)

Winform的窗體自適應(yīng)一直是PC桌面編程的難題,WPF的布局空間很強(qiáng)大,但是學(xué)習(xí)成本太高,介紹一種簡(jiǎn)單的在winform中可以實(shí)現(xiàn)的方式。

思路:1.在窗口程序中使用InitializeSize可以記錄窗體初始位置
2.窗口大小變化后,會(huì)激發(fā)SizeChanged事件,計(jì)算出控件拉伸比例并重新繪制控件。


![QQ截圖20170606105746.png](http://upload-images.jianshu.io/upload_images/6777122-19a6c9cc05536b9c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
   autoFit asc = new autoFit();  
    public Form1()
    {
        InitializeComponent();

    }
    private void Form1_Load(object sender, EventArgs e)
    {
        asc.controllInitializeSize(this);  
    }

    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        asc.controlAutoSize(this);  
    }

自適應(yīng)類
AutoFit.cs

    public struct controlRect
    {
        public int Left;
        public int Top;
        public int Width;
        public int Height;
    }


    public void controllInitializeSize(Form mForm)
    {
        // if (ctrl_first == 0)  
        {
            //  ctrl_first = 1;  
            oldCtrl = new List<controlRect>();
            controlRect cR;
            cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;
            oldCtrl.Add(cR);
            foreach (Control c in mForm.Controls)
            {
                controlRect objCtrl;
                objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height;
                oldCtrl.Add(objCtrl);
            }
        }
        // this.WindowState = (System.Windows.Forms.FormWindowState)(2);//記錄完控件的初始位置和大小后,再最大化  
        //0 - Normalize , 1 - Minimize,2- Maximize  
    } 


    public void controlAutoSize(Form mForm)
    {
        //int wLeft0 = oldCtrl[0].Left; ;//窗體最初的位置  
        //int wTop0 = oldCtrl[0].Top;  
        ////int wLeft1 = this.Left;//窗體當(dāng)前的位置  
        //int wTop1 = this.Top;  
        float wScale = (float)mForm.Width / (float)oldCtrl[0].Width;//新舊窗體之間的比例,與最早的舊窗體  
        float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;//.Height;  
        int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
        int ctrlNo = 1;//第1個(gè)是窗體自身的 Left,Top,Width,Height,所以窗體控件從ctrlNo=1開(kāi)始  
        foreach (Control c in mForm.Controls)
        {
            ctrLeft0 = oldCtrl[ctrlNo].Left;
            ctrTop0 = oldCtrl[ctrlNo].Top;
            ctrWidth0 = oldCtrl[ctrlNo].Width;
            ctrHeight0 = oldCtrl[ctrlNo].Height;
            //c.Left = (int)((ctrLeft0 - wLeft0) * wScale) + wLeft1;//新舊控件之間的線性比例  
            //c.Top = (int)((ctrTop0 - wTop0) * h) + wTop1;  
            c.Left = (int)((ctrLeft0) * wScale);//新舊控件之間的線性比例??丶恢弥幌鄬?duì)于窗體,所以不能加 + wLeft1  
            c.Top = (int)((ctrTop0) * hScale);//  
            c.Width = (int)(ctrWidth0 * wScale);//只與最初的大小相關(guān),所以不能與現(xiàn)在的寬度相乘 (int)(c.Width * w);  
            c.Height = (int)(ctrHeight0 * hScale);//  
            ctrlNo += 1;
        }
    }  
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容