C#窗體布局技巧


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace QQFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region  公共變量
        IntPtr Tem_Handle;//獲取控件及窗體的句柄
        Point CPoint;//獲取控件中鼠標的坐標
        static int Tem_place = 0;
        int Frm_Height = 0;
        int FrmHeight = 0;

        #endregion

        #region  API聲明
        //獲取當前鼠標下可視化控件的句柄
        [DllImport("user32.dll")]
        public static extern int WindowFromPoint(int xPoint, int yPoint);
        //獲取指定句柄的父級句柄
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        public static extern IntPtr GetParent(IntPtr hWnd);
        //獲取屏幕的大小
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        private static extern int GetSystemMetrics(int mVal);
        #endregion

        #region  獲取當前鼠標下可視化控件的句柄
        /// <summary>
        /// 獲取當前鼠標下可視化控件的句柄
        /// </summary>
        /// <param x="int">當前鼠標的X坐標</param>
        /// <param y="int">當前鼠標的Y坐標</param>
        public IntPtr FormNameAt(int x, int y)
        {
            IntPtr Tem_hWnd;//設置存儲句柄的變量
            Tem_Handle = (IntPtr)(WindowFromPoint(x, y));//獲取當前鼠標下可視化控件的句柄
            Tem_hWnd = Tem_Handle;//記錄原始句柄
            while (Tem_hWnd != ((IntPtr)0))//遍歷該句柄的父級句柄
            {
                Tem_Handle = Tem_hWnd;//記錄當前句柄
                Tem_hWnd = GetParent(Tem_hWnd);//獲取父級句柄
            }
            return Tem_Handle;//返回最底層的父級句柄
        }
        #endregion


        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Top < 3 && Tem_place==0)//如果窗體被移到屏幕的頂部
                {
                    if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//當鼠標移致到該窗體上
                    {
                        panel_Title.Tag = 1;//設置標識,用於判斷窗體在屏幕頂部
                        timer2.Enabled = false;//不對窗體進行拉伸操作
                        this.Top = 0;//使窗體致頂
                    }
                    else
                    {
                        panel_Title.Tag = 1;//設置標識,用於判斷窗體在屏幕頂部
                        timer2.Enabled = true;//將窗體在頂部進行隱藏
                    }
                }
                else
                {
                    if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3)//如果窗體被移到屏幕的左端或右端
                    {
                        if (this.Left < 3)//如果窗體被移到屏幕的左端
                        {
                            if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//當鼠標移致到該窗體上
                            {
                                panel_Title.Tag = 2;//設置標識,用於判斷窗體在屏幕左端
                                timer2.Enabled = false;
                                Frm_Height = this.Height;
                                this.Left = 0;//使窗體致左
                                this.Top = 0;
                                this.Height = Screen.AllScreens[0].Bounds.Height;
                                Tem_place = 1;
                            }
                            else
                            {
                                panel_Title.Tag = 2;
                                timer2.Enabled = true;//將窗體在左端進行隱藏
                            }
                        }
                        if (this.Right > GetSystemMetrics(0) - 3)//如果窗體被移到屏幕的右端
                        {
                            if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//當鼠標移致到該窗體上
                            {
                                panel_Title.Tag = 3;//設置標識,用於判斷窗體在屏幕右端
                                timer2.Enabled = false;
                                Frm_Height = this.Height;
                                this.Left = GetSystemMetrics(0) - this.Width;//使窗體致右
                                this.Top = 0;
                                this.Height = Screen.AllScreens[0].Bounds.Height;
                                Tem_place = 1;
                            }
                            else
                            {
                                panel_Title.Tag = 3;
                                timer2.Enabled = true;//將窗體在右端進行隱藏
                            }
                        }

                    }
                }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            switch (Convert.ToInt16(panel_Title.Tag.ToString()))//對標識進行判斷
            {
                case 1://頂端隱藏
                    {
                        if (this.Top < 5)
                            this.Top = -(this.Height - 2);
                        break;
                    }
                case 2://左端隱藏
                    {
                        if (this.Left < 5)
                            this.Left = -(this.Width - 2);
                        break;
                    }
                case 3://右端隱藏
                    {
                        if ((this.Left + this.Width) > (GetSystemMetrics(0) - 5))
                            this.Left = GetSystemMetrics(0) - 2;
                        break;
                    }
            }
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region  利用窗體上的控件移動窗體
        /// <summary>
        /// 利用控件移動窗體
        /// </summary>
        /// <param Frm="Form">窗體</param>
        /// <param e="MouseEventArgs">控件的移動事件</param>
        public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加命名空間using System.Windows.Forms;
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//獲取當前鼠標的屏幕坐標
                myPosittion.Offset(CPoint.X, CPoint.Y);//重載當前鼠標的位置
                Frm.DesktopLocation = myPosittion;//設置當前窗體在屏幕上的位置
                Tem_place = 0;
                this.Height = FrmHeight;
            }
        }
        #endregion

        private void panel_Title_MouseDown(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            CPoint = new Point(-e.X, -e.Y);
        }

        private void panel_Title_MouseMove(object sender, MouseEventArgs e)
        {
            FrmMove(this, e);
        }

        private void panel_Title_MouseUp(object sender, MouseEventArgs e)
        {
            timer1.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Frm_Height = this.Height;
            FrmHeight = this.Height;
            this.TopMost = true;
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM