[转] – c# 拖动和改变大小的无边框窗体

c# 拖动和改变大小的无边框窗体和控件
2010-06-28 15:56

//BY WGSCD

//Date: 28 jun 2010

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 testSelectRegion
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        
                this.FormBorderStyle = FormBorderStyle.None;
                this.DoubleBuffered = true;
                this.SetStyle(ControlStyles.ResizeRedraw, true);
         

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    
            private const int cGrip = 16;      // Grip size
            private const int cCaption = 25;   // Caption bar height;

            protected override void OnPaint(PaintEventArgs e)
            {
                Rectangle rc = new Rectangle(this.ClientSize.Width – cGrip, this.ClientSize.Height – cGrip, cGrip, cGrip);
                ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
                rc = new Rectangle(0, 0, this.ClientSize.Width, 32);
                e.Graphics.FillRectangle(Brushes.DarkBlue, rc);

            }

            protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x84)
                { // Trap WM_NCHITTEST
                    Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                    pos = this.PointToClient(pos);
                    Console.WriteLine(pos);
                    if (pos.Y < cCaption)
                    {
                        m.Result = (IntPtr)2; // HTCAPTION
                        return;
                    }
                    if (pos.X >= this.ClientSize.Width – cGrip && pos.Y >= this.ClientSize.Height – cGrip)
                    {
                        m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                        return;
                    }
                }
                base.WndProc(ref m);
            }

            #region “拖动窗体”

            private bool moveFlag = false;
            private int x = 0;
            private int y = 0;

            protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
            {
                if (moveFlag && (e.Button == MouseButtons.Left))
                    this.SetBounds(Left + e.X – x, Top + e.Y – y, this.Width, this.Height);
                base.OnMouseMove(e);
            }

            protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
            {
                if (!moveFlag && e.Clicks >= 1)
                    moveFlag = true;
                x = e.X;
                y = e.Y;
                base.OnMouseDown(e);
            }

            protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
            {
                if (moveFlag)
                    moveFlag = false;
                base.OnMouseUp(e);
            }

        #endregion “拖动窗体”

    }
}

———————————————

可拖动控件

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

namespace testSelectRegion
{
    public partial class tt : UserControl
    {
        public tt()
        {
            InitializeComponent();

         //   this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
         

        }

            private const int cGrip = 16;      // Grip size
            private const int cCaption = 25;   // Caption bar height;

            protected override void OnPaint(PaintEventArgs e)
            {
                Rectangle rc = new Rectangle(this.ClientSize.Width – cGrip, this.ClientSize.Height – cGrip, cGrip, cGrip);

                ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
                rc = new Rectangle(0, 0, this.ClientSize.Width, 32);
                e.Graphics.FillRectangle(Brushes.Coral, rc);

            }

            protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x84)
                { // Trap WM_NCHITTEST
                    Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                    pos = this.PointToClient(pos);
                    Console.WriteLine(pos);
                    if (pos.Y < cCaption)
                    {
                        m.Result = (IntPtr)2; // HTCAPTION
                        return;
                    }
                    if (pos.X >= this.ClientSize.Width – cGrip && pos.Y >= this.ClientSize.Height – cGrip)
                    {
                        m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                        return;
                    }
                }
                base.WndProc(ref m);
            }

            private void tt_Resize(object sender, EventArgs e)
            {

                label1.Text = this.Size.ToString();

            }
     

          private bool moveFlag = false;
        private int x = 0;
        private int y = 0;

        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag && (e.Button == MouseButtons.Left))
                this.SetBounds(Left + e.X – x, Top + e.Y – y, this.Width, this.Height);
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (!moveFlag && e.Clicks >= 1)
                moveFlag = true;
            x = e.X;
            y = e.Y;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag)
                moveFlag = false;
            base.OnMouseUp(e);
        }

    }
}

引文来源  c# 拖动和改变大小的无边框窗体和控件_自由奔腾_百度空间