C#鼠标拖拽,移动图片实例

news2024/10/5 19:48:22

最近工作需要做一个鼠标可以拖拽移动图片的功能。

写了几个基本功能,勉强能用。这里记录一下。欢迎大神补充。

232323.png

这个就是完成的功能。

下边的绿色是一个pictureBox,白色框也是一个pictureBox,他们二者是子父级关系。

绿色是父级,白色框是子级。

这里插一个知识点:关于pictureBox的

白色框的pictureBox,我添加的image属性的图片是一张中心透明四周是白边回来Png图片,这个需要注意一下。

1:子级pictureBox如何在父级PictureBox上透明显示。添加下边这句话:

// 设置背景颜色为系统定义的颜色
pictureBox2.BackColor = Color.Transparent;

2:背景透明设置完成,但是现在还是不能够拖拽四周或四个角改变大小。添加下边这句话:

// 设置pictureBox大小可拖拽
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

到这里,小的pictureBox就已经背景透明并且可拖拽的显示在其父级上边了。

下边是我测试使用的Form1.cs文件的代码:

实例放在文末的压缩包中,有兴趣可以尝试下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
 
namespace cutCScan
{
    public partial class Form1 : Form
    {
        // 截取数据,获得对角线两点坐标。取到要截取的平面区域。
        // 平面区域中有若干个A扫,取其峰值,上下各加一个固定阈值(50)
        // 将此块数据都截取下来。
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // 设置背景颜色为系统定义的颜色
            pictureBox2.BackColor = Color.Transparent;
            pictureBox2.Parent = pictureBox1;
            //pictureBox2.BackgroundImageLayout = ImageLayout.Stretch;//设置背景图片自动适应
            // 设置pictureBox大小可拖拽
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
        }
 
 
        /// <summary>
        ///  有关鼠标样式的相关枚举
        /// </summary>
        private enum EnumMousePointPosition
        {
            MouseSizeNone = 0, //无
            MouseSizeRight = 1, //拉伸右边框
            MouseSizeLeft = 2,  //拉伸左边框
            MouseSizeBottom = 3, //拉伸下边框
            MouseSizeTop = 4, //拉伸上边框
            MouseSizeTopLeft = 5,//拉伸左上角
            MouseSizeTopRight = 6,//拉伸右上角
            MouseSizeBottomLeft = 7,//拉伸左下角
            MouseSizeBottomRight = 8,//拉伸右下角
            MouseDrag = 9 //鼠标拖动
        }
 
        #region 鼠标移动变量
        const int Band = 5;//范围半径
        const int MinWidth = 10;//最低宽度
        const int MinHeight = 10;//最低高度
        private EnumMousePointPosition m_MousePointPosition; //鼠标样式枚举
        private Point m_lastPoint; //光标上次移动的位置
        private Point m_endPoint; //光标移动的当前位置
 
 
        #endregion
 
       
 
        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            m_lastPoint.X = e.X;
            m_lastPoint.Y = e.Y;
            m_endPoint.X = e.X;
            m_endPoint.Y = e.Y;
        }
 
        /// <summary>
        /// 鼠标离开控件的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
            this.Cursor = Cursors.Arrow;
        }
        /// <summary>
        /// 鼠标移过控件的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            Control lCtrl = (sender as Control);//获得事件源
            //左键按下移动
            if (e.Button == MouseButtons.Left)
            {
                switch (m_MousePointPosition)
                {
                    case EnumMousePointPosition.MouseDrag:
                        lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X;
                        lCtrl.Top = lCtrl.Top + e.Y - m_lastPoint.Y;
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y;
                        m_endPoint.X = e.X;
                        m_endPoint.Y = e.Y; //记录光标拖动的当前点                     
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        lCtrl.Width = lCtrl.Width + e.X - m_endPoint.X;
                        lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y;
                        m_endPoint.X = e.X;
                        m_endPoint.Y = e.Y; //记录光标拖动的当前点     
                        break;
                    case EnumMousePointPosition.MouseSizeRight:
                        lCtrl.Width = lCtrl.Width + e.X - m_endPoint.X;
                        lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y;
                        m_endPoint.X = e.X;
                        m_endPoint.Y = e.Y; //记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        lCtrl.Top = lCtrl.Top + (e.Y - m_lastPoint.Y);
                        lCtrl.Height = lCtrl.Height - (e.Y - m_lastPoint.Y);
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X;
                        lCtrl.Width = lCtrl.Width - (e.X - m_lastPoint.X);
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X;
                        lCtrl.Width = lCtrl.Width - (e.X - m_lastPoint.X);
                        lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y;
                        m_endPoint.X = e.X;
                        m_endPoint.Y = e.Y; //记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        lCtrl.Top = lCtrl.Top + (e.Y - m_lastPoint.Y);
                        lCtrl.Width = lCtrl.Width + (e.X - m_endPoint.X);
                        lCtrl.Height = lCtrl.Height - (e.Y - m_lastPoint.Y);
                        m_endPoint.X = e.X;
                        m_endPoint.Y = e.Y; //记录光标拖动的当前点        
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X;
                        lCtrl.Top = lCtrl.Top + (e.Y - m_lastPoint.Y);
                        lCtrl.Width = lCtrl.Width - (e.X - m_lastPoint.X);
                        lCtrl.Height = lCtrl.Height - (e.Y - m_lastPoint.Y);
                        break;
                    default:
                        break;
                }
                if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
                if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
            }
            else
            {
                //'判断光标的位置状态
                m_MousePointPosition = MousePointPosition(lCtrl.Size, e);
                switch (m_MousePointPosition)  //改变光标
                {
                    case EnumMousePointPosition.MouseSizeNone:
                        this.Cursor = Cursors.Arrow;//箭头
                        break;
                    case EnumMousePointPosition.MouseDrag:
                        this.Cursor = Cursors.SizeAll;//四方向
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        this.Cursor = Cursors.SizeNS;//南北
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        this.Cursor = Cursors.SizeNS;//南北
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        this.Cursor = Cursors.SizeWE;//东西
                        break;
                    case EnumMousePointPosition.MouseSizeRight://*/
                        this.Cursor = Cursors.SizeWE;//东西
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        this.Cursor = Cursors.SizeNESW;//东北到南西
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        this.Cursor = Cursors.SizeNWSE;//东南到西北
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        this.Cursor = Cursors.SizeNWSE;//东南到西北
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        this.Cursor = Cursors.SizeNESW;//东北到南西
                        break;
                    default:
                        break;
                }
            }
        }
 
        /// <summary>
        /// 坐标位置判定
        /// </summary>
        /// <param name="size"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
        {
            if ((e.X >= -1 * Band) | (e.X <= size.Width) |
                (e.Y >= -1 * Band) | (e.Y <= size.Height))
            {
                if (e.X < Band)
                {
                    if (e.Y < Band)
                    {
                        return EnumMousePointPosition.MouseSizeTopLeft;
                    }
                    else
                    {
                        if (e.Y > -1 * Band + size.Height)
                        {
                            return EnumMousePointPosition.MouseSizeBottomLeft;
                        }
                        else
                        {
                            return EnumMousePointPosition.MouseSizeLeft;
                        }
                    }
                }
                else
                {
                    if (e.X > -1 * Band + size.Width)
                    {
                        if (e.Y < Band)
                        { return EnumMousePointPosition.MouseSizeTopRight; }
                        else
                        {
                            if (e.Y > -1 * Band + size.Height)
                            { return EnumMousePointPosition.MouseSizeBottomRight; }
                            else
                            {
                                return EnumMousePointPosition.MouseSizeRight;
                            }
                        }
                    }
                    else
                    {
                        if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTop;
                        }
                        else
                        {
                            if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottom;
                            }
                            else
                            { return EnumMousePointPosition.MouseDrag; }
                        }
                    }
                }
            }
            else
            { return EnumMousePointPosition.MouseSizeNone; }
        }
    }
}

有好的建议,请在下方输入你的评论。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/768755.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

网约车订单自助测试演进与落地实践

1► 业务背景 网约车业务作为滴滴的核心业务&#xff0c;系统架构复杂、整个订单链路涉及众多下游服务&#xff0c;整体迭代频率高&#xff0c;同时在产品形态上通过不同品类提供差异化服务能力&#xff0c;整体品类从最初的专车、快车延展到如今多个品类&#xff0c;这就导致在…

Android 分别使用Java和Kotlin给Textview设置第三方字体、APP全局字体、 Android X字体设置

前言 本文介绍Android实现全局设置自定义字体和局部设置自定义字体即单个TextView设置字体&#xff0c;同时也提供了一些优秀的三方字体框架&#xff0c;基本可以满足开发者对字体设置的全部要求。 使用自定义字体前后效果图 一、assets是什么&#xff1f; 首先需要了解Andr…

【11】STM32·HAL库开发-STM32CubeMX简介、安装 | 新建STM32CubeMX工程步骤

目录 1.STM32CubeMX简介&#xff08;了解&#xff09;2.STM32CubeMX安装&#xff08;了解&#xff09;2.1STM32CubeMX软件获取2.1.1获取Java软件2.1.2获取STM32CubeMX软件 2.2搭建Java运行环境2.3安装STM32CubeMX软件&#xff08;必须先2.2再2.3&#xff09;2.4下载和关联STM32…

若依cloud(RuoYi-Cloud)新增业务模块和功能模块增删改查演示

前言 看了几篇文章感觉都不太满意&#xff0c;索性自己来写一篇。 一、后端 后端新建业务模块流程大致如下&#xff1a; 1、后端新建一个&#xff08;在ruoyi-module模块下&#xff09;业务模块&#xff0c;仿照已有的模块将此模块配置好&#xff0c;例如仿照系统模块&…

6、PHP语法要点(1)

PHP的语法跟C语言还是类似的。 1、变量不用定义即可直接使用。 2、函数外定义的变量为全局变量。global 关键字用于函数内访问全局变量。 3、static 用于保持函数内局部变量的值。但在函数外依然不能访问。 4、PHP 将所有全局变量存储在一个名为 $GLOBALS[index] 的数组中。…

GptFu0k——ChatGpt连接源爬取器

最近ChatGPT的热度下去了&#xff0c;但是我相信&#xff0c;很多真正需要的人还是一直在用的&#xff0c;为了解决ChatGPT账号的问题还有网络连接问题&#xff0c;通常会花费大量时间去寻找&#xff0c;为了解决这个棘手的问题&#xff0c;GptFu0k横空出世&#xff0c;全网爬取…

提高LLaMA-7B的数学推理能力

概述 这篇文章探讨了利用多视角微调方法提高数学推理的泛化能力。数学推理在相对较小的语言模型中仍然是一个挑战&#xff0c;许多现有方法倾向于依赖庞大但效率低下的大语言模型进行知识蒸馏。研究人员提出了一种避免过度依赖大语言模型的新方法&#xff0c;该方法通过有效利…

Ctfshow Crypto全

目录 各种各样的编码 crypto0(凯撒) crypto2(jsfuck) crypto3(aaencode) crypto4(知p q e求d) crypto5(知p q e c求d) crypto6(Rabbit) crypto7(Ook!) crypto8(BrainFuck) crypto9(serpent) crypto10(quoted-printable) crypto11(md5) crypto12(埃特巴什码) cryp…

AI Chat 设计模式:8. 门面(外观)模式

本文是该系列的第八篇&#xff0c;采用问答式的方式展开&#xff0c;问题由我提出&#xff0c;答案由 Chat AI 作出&#xff0c;灰色背景的文字则主要是我的一些思考和补充。 问题列表 Q.1 请介绍一下门面模式A.1Q.2 该模式由哪些角色组成呢A.2Q.3 举一个门面模式的例子A.3Q.4…

vue的computed复习

1.复杂 data 的处理方式 三个案例&#xff1a;  案例一&#xff1a;我们有两个变量&#xff1a; firstName 和 lastName &#xff0c;希望它们拼接之后在界面上显示&#xff1b;  案例二&#xff1a;我们有一个分数&#xff1a; score  当 score 大于 60 的时候&#xf…

云拨测全面升级丨单次拨测低至 0.001 元

作者&#xff1a;少焉 随着云原生、微服务技术的发展&#xff0c;可观测需求变得越来越强烈&#xff0c;作为可观测技术的重要能力之一&#xff0c;云拨测&#xff08;Synthetics Monitor&#xff09;由于其零侵入、开箱即用、主动式监测手段&#xff0c;也受到很多用户的青睐…

arcgis建筑物平均高度

主要用到相交和属性表的汇总功能。 路网 建筑物栋 相交结果 右键&#xff0c;bh列汇总 原始块有392&#xff0c;这里只有389&#xff0c;说明有的地块没有建筑&#xff0c;所以应该将表连接到原始街区上检查是否合理&#xff0c;以及随机验证一个结果是否正确。 连接结果&…

【问题总结】基于docker-compose实现nginx转发redis

目录&#xff1a; 文章目录 需求简介&#xff1a;Q1: nginx的http模块和http模块有什么不同Q2: 可以都使用stream模块进行配置吗 Docker环境下如何转发1 修改docker-compose2 修改nginx.conf3 测试连接 需求简介&#xff1a; 需要在192.168.3.11的ngnix上&#xff0c;转发192.…

wordpress 导入数据后 为什么总是跳转到安装页面

在WordPress导入数据后跳转到安装页面的问题可能由以下原因引起&#xff1a; 数据库连接问题&#xff1a;在导入数据之前&#xff0c;确保您的WordPress配置文件中的数据库连接信息正确且完整。打开 wp-config.php 文件&#xff0c;确保数据库的名称、用户名、密码和主机信息是…

springboot整合spring security的简单入门案例

一 工程结构 二 配置操作 2.1 pom文件配置 <!-- Spring Security依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency> 2.2 config配置 Config…

vue中预览pdf

情况一 如果后端返回的pdf地址&#xff0c;粘贴到浏览器的url框中&#xff0c;可以在浏览器中直接进行预览的&#xff0c;那么我们就用window.open&#xff0c;或 a标签&#xff0c;或iframe标签通过设置src进行预览即可 法1&#xff1a;可以直接使用window.open&#xff08;…

css学习知识总结

一、css与html连接&#xff1a; 可以将css语句放在html内部&#xff0c;一般放在<head>之下&#xff0c;定义在<style>中&#xff0c;格式一般是一个“.”然后加上一个“名称”再加上一个“{}”&#xff0c;再在“{}”内部定义具体的语句。 二、调整元素 2.1 字体…

网络 socket

文章目录 概念和 TCP、UDP 区别和 HTTP 区别 概念 为网络通信提供的接口&#xff0c;定义了应用程序如何访问网络资源、如何发送和接收数据等&#xff0c;Socket 是一个包含了IP地址和端口号的组合&#xff0c;当一个应用程序想要与另一个应用程序通信时&#xff0c;它会向特定…

6.3.5 利用Wireshark进行协议分析(五)----捕获并分析ICMP报文

6.3.5 利用Wireshark进行协议分析&#xff08;五&#xff09;----捕获并分析ICMP报文 一、捕获ICMP报文 打开Wireshark&#xff0c;选择网络接口并点击开始按钮。分组列表面板不断刷新抓渠道的数据包&#xff0c;为了过滤出我们所要分析的ICMP报文&#xff0c;我们在过滤框中输…

线程池学习(六)线程池状态转化

线程池状态定义 // runState is stored in the high-order bits // 线程池创建之后的初始状态&#xff0c;这种状态下可以执行任务private static final int RUNNING -1 << COUNT_BITS; // 线程池不再接收新的任务&#xff0c;但是会将队列中的任务执行完 private s…