最近工作需要做一个鼠标可以拖拽移动图片的功能。
写了几个基本功能,勉强能用。这里记录一下。欢迎大神补充。
这个就是完成的功能。
下边的绿色是一个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; }
}
}
}
有好的建议,请在下方输入你的评论。
文章来源:https://www.toymoban.com/news/detail-598474.html
文章来源地址https://www.toymoban.com/news/detail-598474.html
到了这里,关于C#鼠标拖拽,移动图片实例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!