C#实现无标题窗体拖动方法封装提取及应用

方法封装提取代码如下:

using System;
using System.Collections.Generic;
using System.Text;


//Point
using System.Drawing;

//DllImport
using System.Runtime.InteropServices;

//MouseEventArgs
using System.Windows.Forms;





namespace FinancialManage.com.smile
{
    public class SmilePublicLibrary
    {


        #region 鼠标拖动控件或窗体,实现窗体的移动

        bool beginMove = false;//初始化

        Point winFormCPoint = new Point(0, 0);
        

        //导入DLL
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out Point pt);

      
        public void FMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                beginMove = true;

                GetCursorPos(out winFormCPoint);
                //winFormCPoint.X = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标

                //winFormCPoint.Y = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标

            }
        }


        public void FMouseMove(Control control)
        {
            if (beginMove)
            {
                Point p = new Point(0,0);
                GetCursorPos(out p);

                control.Left += p.X - winFormCPoint.X;//根据鼠标x坐标确定窗体的左边坐标x

                control.Top += p.Y - winFormCPoint.Y;//根据鼠标的y坐标窗体的顶部,即Y坐标

                winFormCPoint.X = p.X;

                winFormCPoint.Y = p.Y;

            }
        }



        public void FMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                winFormCPoint.X = 0; //设置初始状态
                winFormCPoint.Y = 0;
                beginMove = false;

            }
        }

        #endregion
    }
}

方法应用:

 SmilePublicLibrary SPL = new SmilePublicLibrary();


        #region 实现窗体的移动

        bool beginMove = false;//初始化

        Point winFormCPoint = new Point(0, 0);

        //拖动窗体部分
        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            SPL.FMouseDown(e);
        }

        private void Login_MouseMove(object sender, MouseEventArgs e)
        {
            SPL.FMouseMove(this);
        }

        private void Login_MouseUp(object sender, MouseEventArgs e)
        {
            SPL.FMouseUp(e);
        }

        //拖动图片部分
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            SPL.FMouseDown(e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            SPL.FMouseMove(this);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            SPL.FMouseUp(e);
        }


        #endregion

 

转载于:https://www.cnblogs.com/Smile2019/archive/2012/09/27/2704959.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值