c#不规则窗体之移动窗体

来源:互联网 发布:米兔机器人如何编程 编辑:程序博客网 时间:2024/05/20 13:09
using System;using System.Drawing;using System.Windows.Forms;namespace 不规则窗体{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            this.Load += new EventHandler(Form_Load);            this.closeBtn.Click += new EventHandler(CloseBtn_Click);            this.MouseDown += new MouseEventHandler(Mouse_Down);            this.MouseMove += new MouseEventHandler(Mouse_Move);            this.MouseUp += new MouseEventHandler(Mouse_Up);        }        Bitmap bmap = null;        private void Form_Load(object sender, EventArgs e)        {            string path = AppDomain.CurrentDomain.BaseDirectory;            path = System.IO.Path.Combine(path, @"image\bg.bmp");            bmap = new Bitmap(path);            bmap.MakeTransparent(Color.White);        }        protected override void OnPaint(PaintEventArgs e)        {            e.Graphics.DrawImage((Image)bmap, new Point(0, 0));            //base.OnPaint(e);        }        private void CloseBtn_Click(object sender, EventArgs e)        {            Application.Exit();        }        private int mx=0, my=0;        private bool status = false;//鼠标状态,是否按住并在拖动,是则为true        private void Mouse_Down(object sender, MouseEventArgs e)        {            if (e.Button ==MouseButtons.Left)            {                mx = e.X;                my = e.Y;                status = true;            }        }        /// <summary>        /// 鼠标移动        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Mouse_Move(object sender, MouseEventArgs e)        {            if (status)            {                int nx = e.X;                int ny = e.Y;                this.Left += (nx - mx);                this.Top += (ny - my);            }        }        /// <summary>        /// 松开鼠标        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Mouse_Up(object sender, MouseEventArgs e)        {            status = false;        }    }}
注:窗体的TransparentKey要设置成图片的背景色
原创粉丝点击