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

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要设置成图片的背景色
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值