none
C#实现标题栏功能(拖拽和最大化) RRS feed

答案

  • 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;
    
    namespace X.WinFormsApp
    {
        /// <summary>
        /// 标题:简单鼠标操作
        /// 日期:2009-05-11
        /// 描述:用C#怎么实现标题栏功能 左键单击退拽左键双击最大化
        /// </summary>
        public partial class X200905111129 : Form
        {
            public X200905111129()
            {
                InitializeComponent();
    
                this.MouseDown += new MouseEventHandler(X200905111129_MouseDown);
                this.MouseMove += new MouseEventHandler(X200905111129_MouseMove);
                this.DoubleClick += new EventHandler(X200905111129_DoubleClick);
            }
    
            private void X200905111129_DoubleClick(object sender, EventArgs e)
            {
                // 最大化
                this.WindowState = FormWindowState.Maximized;
            }
    
            #region " 左键拖动 "
    
            private Point mPoint;
    
            private void X200905111129_MouseDown(object sender, MouseEventArgs e)
            {
                mPoint = e.Location;
            }
    
            private void X200905111129_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int newX = this.Location.X + e.X - mPoint.X;
                    int newY = this.Location.Y + e.Y - mPoint.Y;
                    this.Location = new Point(newX, newY);
                }
            }
            #endregion
        }
    }
    
    

    知识改变命运,奋斗成就人生!
    2009年5月11日 3:39
    版主