窗体中拖动panel,并且不会拖动至窗体外部程序实现方法。

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.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Point pt;
        MouseEventHandler hand;
        public Form1()
        {
            InitializeComponent();        
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            pt = Cursor.Position;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Cursor = new Cursor(Cursor.Current.Handle);
                Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
                Cursor.Clip = new Rectangle(new Point(this.Location.X,  this.Location .Y + 30), new Size(this.Width, this.Height - 30));
                int px = Cursor.Position.X - pt.X;
                int py = Cursor.Position.Y - pt.Y;
                panel1.Location = new Point(panel1.Location.X + px, panel1.Location.Y + py);
                pt = Cursor.Position;               
            }
        }     

        private void Form1_Load(object sender, EventArgs e)
        {
            hand = new MouseEventHandler(panel1_MouseMove);
            panel1.MouseMove += hand;
               this.Cursor = Cursors.Default;
        }   

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            Cursor.Clip = new Rectangle(new Point(0, 0), new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height));
            System.Diagnostics.Process[] ss = System.Diagnostics.Process.GetProcesses();

            foreach (System.Diagnostics.Process s in ss)
            {
                if (s.ProcessName.ToLower() == "test")
                {
                    s.Kill();      
                }          
            }
        }      
    
    }
}

原文地址:https://www.cnblogs.com/gaolijun1986/p/2114066.html