鼠标点击拖动图片随着转动的问题

Magerlun 2012-04-23 02:11:14
这是一个可以实现鼠标点击图片并拖动,图片就会跟随鼠标转动的代码。鼠标点中图片后鼠标也会改变图标。问题是图片识别鼠标可拖动的矩形区域,一开始设定为与图片的位置大小一样,但是随着图片的转动,矩形识别区域并没有改变,还是初始的设定,我想实现点击图片转动,并且识别鼠标可拖动的区域就是图片所占的位置。比如说大图片矩形识别区域就大,小图片矩形识别区域就小,并且图片转动了,识别区域也随着图片一起改变,请问如何实现,最好写出代码,因为本人是新手中的新手啊!
代码:
namespace 图片只旋转不移动
{
public partial class Form1 : Form
{
private bool mouseInEllipse = false; // 鼠标是否在圆形中
private Rectangle rectangle ;
private String StrFileName = "";
float va;
Bitmap image = null;
double pi = Math.PI;
PointF Pcenter;
public Form1()
{
InitializeComponent();
}
// 装入图片
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
this.StrFileName = this.openFileDialog1.FileName;
fuya(1, this.pictureBox1.CreateGraphics());
}
//根据角度绘制图片
public void fuya(float va, Graphics graphics)
{
if (this.StrFileName.Trim() == "")
return;
this.pictureBox1.Image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);//给pictureBox一个内存图像
Graphics g = Graphics.FromImage(this.pictureBox1.Image); //创建用于绘图的Graphics,用这个Graphics对象实例g来绘制你的图形
g.Clear(System.Drawing.SystemColors.Control);
if (image == null)
{
image = new Bitmap(this.StrFileName);
}
TextureBrush MyBrush = new TextureBrush(image);

//获取当前窗口的中心点
Rectangle rect = new Rectangle(0, 0, this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);
PointF center = new PointF(this.pictureBox1.Location.X + rect.Width / 2, this.pictureBox1.Location.Y + rect.Height / 2);
float offsetX = 0;
float offsetY = 0;
offsetX = center.X - image.Width / 2;
offsetY = center.Y - image.Height / 2;
//构造图片显示区域:让图片的中心点与窗口的中心点一致
RectangleF picRect = new RectangleF(offsetX, offsetY, image.Width, image.Height);
Pcenter = new PointF(picRect.X + picRect.Width / 2, picRect.Y + picRect.Height / 2);
// 绘图平面以图片的中心点旋转
g.TranslateTransform(Pcenter.X, Pcenter.Y);
g.RotateTransform(va);
//定义鼠标进入与否的图片区域
int a = (int)picRect.X;
int b = (int)picRect.Y;
rectangle = new Rectangle(a, b, image.Width, image.Height);
//恢复绘图平面在水平和垂直方向的平移
g.TranslateTransform(-Pcenter.X, -Pcenter.Y);
//绘制图片并延时
g.DrawImage(image, picRect);
//重置绘图平面的所有变换
g.ResetTransform();
}

private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false;//记录鼠标按键是否按下
private bool isMouseMove = false;
int xOffset;
int yOffset;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
GraphicsPath vGraphicsPath = new GraphicsPath();
vGraphicsPath.AddRectangle(rectangle); // 添加矩形路径
Region vRegion = new Region(vGraphicsPath);
mouseInEllipse = vRegion.IsVisible(e.Location); // 判断点是否在矩形中
string path = Application.StartupPath;
if (mouseInEllipse == true)
{
//鼠标拖动图片时,图标变换
Image fn = Image.FromFile(path + @"/image/log.ico");
Bitmap bitmap = new Bitmap(fn);
IntPtr handle = bitmap.GetHicon();
Cursor myCursor = new Cursor(handle);
this.Cursor = myCursor;
if (isMouseMove == true)
{
double ltx = (e.Location.X - rectangle.X - rectangle.Width / 2);
double lty = (e.Location.Y - rectangle.Y - rectangle.Height / 2);
//左边
if (ltx > 0)
{
//左上
if (lty < 0)
{
double ltyy = -lty;
va = (float)(Math.Round(Math.Atan2(ltx, ltyy) * (180 / pi), 0));
}
//左下
if (lty > 0)
{
va = 90 + Convert.ToInt16(Math.Round(Math.Atan2(lty, ltx) * (180 / pi), 0));
}
}
//右边
if (ltx < 0)
{
//右下
if (lty > 0)
{
va = 90 + Convert.ToInt16(Math.Round(Math.Atan2(lty, ltx) * (180 / pi), 0));
}
//右上
if (lty < 0)
{
va = 90 + Convert.ToInt16(Math.Round(Math.Atan2(lty, ltx) * (180 / pi), 0));
}
}
}
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
GraphicsPath vGraphicsPath = new GraphicsPath();
vGraphicsPath.AddRectangle(rectangle); // 添加矩形路径
Region vRegion = new Region(vGraphicsPath);
mouseInEllipse = vRegion.IsVisible(e.Location); // 判断点是否在矩形中
if (mouseInEllipse == true)
{
if (isMouseDown == true)
{
double ltx = (e.Location.X - rectangle.X - rectangle.Width / 2);
double lty = (e.Location.Y - rectangle.Y - rectangle.Height / 2);
//左边
if (ltx > 0)
{
//左上
if (lty < 0)
{
double ltyy = -lty;
va = (float)(Math.Round(Math.Atan2(ltx, ltyy) * (180 / pi), 0));
}
//左下
if (lty > 0)
{
va = 90 + Convert.ToInt16(Math.Round(Math.Atan2(lty, ltx) * (180 / pi), 0));
}
}
//右边
if (ltx < 0)
{
//右下
if (lty > 0)
{
va = 90 + Convert.ToInt16(Math.Round(Math.Atan2(lty, ltx) * (180 / pi), 0));
}
//右上
if (lty < 0)
{
va = 90 + Convert.ToInt16(Math.Round(Math.Atan2(lty, ltx) * (180 / pi), 0));
}
}
}
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
this.Cursor = null;
}
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
fuya(va,e.Graphics);
if (this.StrFileName.Trim() == "")
return;
if (image == null)
{
image = new Bitmap(this.StrFileName);
}
int r = (image.Height / 2 + 30);
float point1 = (System.Single)(524 + r* Math.Sin(va * (pi) / 180));
float point2 = (System.Single)(425 - r * Math.Cos(va * (pi) / 180 ));
e.Graphics.DrawEllipse(new Pen(Color.LightSeaGreen,2),point1,point2,15.0f,15.0f);
}
}
}
...全文
504 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magerlun 2012-04-24
  • 打赏
  • 举报
回复
求帮忙啊!!!! 没人回答吗
LOVE_GG 2012-04-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
lz接好了!用.net提供的矩阵操作。看了这个帖子一定能解决你的问题。
http://topic.csdn.net/u/20111201/15/1f460276-2c8e-4243-80dc-c2a8bd1b39d5.html

能不能帮我具体的写写代码呢,你的帖子里方法很多,不知道怎么用。
[/Quote]
用矩阵操作,代码就自己写了吧。google一下c#矩阵操作。
Magerlun 2012-04-23
  • 打赏
  • 举报
回复
求大神解救啊!!!
Magerlun 2012-04-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
lz接好了!用.net提供的矩阵操作。看了这个帖子一定能解决你的问题。
http://topic.csdn.net/u/20111201/15/1f460276-2c8e-4243-80dc-c2a8bd1b39d5.html
[/Quote]
能不能帮我具体的写写代码呢,你的帖子里方法很多,不知道怎么用。
LOVE_GG 2012-04-23
  • 打赏
  • 举报
回复
lz接好了!用.net提供的矩阵操作。看了这个帖子一定能解决你的问题。
http://topic.csdn.net/u/20111201/15/1f460276-2c8e-4243-80dc-c2a8bd1b39d5.html

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧