首页 > Windows开发 > 详细

wpf实现qq截图八个点以及旋转功能

时间:2019-04-09 18:58:30      阅读:160      评论:0      收藏:0      [点我收藏+]

一、先用canvas面板实现界面的效果和位置

技术分享图片

1.1wpf 标签如下:

<Canvas Name="canvas" Background="Brown">
<Canvas.RenderTransform>
<RotateTransform x:Name="rot" Angle="20"></RotateTransform>
</Canvas.RenderTransform>
<Rectangle x:Name="rect1" Fill="Aqua" Cursor="SizeNWSE" >
</Rectangle>
<Rectangle x:Name="rect2" Fill="Aqua" Cursor="SizeNS">
</Rectangle>
<Rectangle x:Name="rect3" Fill="Aqua" Cursor="SizeNESW"/>
<Rectangle x:Name="rect4" Fill="Aqua" Cursor="SizeWE"/>
<Rectangle x:Name="rect5" Fill="Aqua" Cursor="SizeNWSE"/>
<Rectangle x:Name="rect6" Fill="Aqua" Cursor="SizeNS"/>
<Rectangle x:Name="rect7" Fill="Aqua" Cursor="SizeNESW"/>
<Rectangle x:Name="rect8" Fill="Aqua" Cursor="SizeWE"/>
<Rectangle x:Name="rect9" Fill="Aqua" Cursor="Hand" >
</Rectangle>
</Canvas>

二、C#代码如下:

2.1去掉边框

 

Left = 0;
Top = 0;

//旋转图标光标的设置路径
StreamResourceInfo sri = Application.GetResourceStream(new Uri(@"imgs/旋转-2.cur", UriKind.Relative));
RotateCursor = new Cursor(sri.Stream);

DrawRects();//调用方法

2.2 定义变量

private bool IsMouseDown = false;//初始鼠标的状态为false
private HitWhitchTypes HitWhitch;//定义对象
private Point MouseStartPos2;
private Rect box = new Rect(80, 80, 60, 80);//盒子的区域
private Rect startBox;//
private double startDegree;//开始的度数
private const double RectSize = 20;//八个小盒子的大小
private double Degree = 20;//结束的度数

private Cursor RotateCursor;//定义变量鼠标

//定义11种击中的状态,初始化为none
public enum HitWhitchTypes
{
none,
rect_left_top1,
rect_top2,
rect_right_top3,
rect_right4,
rect_right_bottom5,
rect_bottom6,
rect_left_bottom7,
rect_left8,
box_rotate,
rect_box
};

2.3 鼠标移动、拖动(击中盒子)

//鼠标移动、拖动
private void Window_MouseMove(object sender, MouseEventArgs e)
{

if (IsMouseDown)
{
//获取面板的鼠标位置
var p2 = e.GetPosition(outerCanvas);
var v2 = p2 - MouseStartPos2;

//旋转的角度
var angle = -startDegree / 180.0 * Math.PI;
var angle_back = -angle;
box_rotate.CenterX = angle_back;

var rotateBackV = new Vector(
v2.X * Math.Cos(angle_back) + v2.Y * Math.Sin(angle_back),
v2.Y * Math.Cos(angle_back) - v2.X * Math.Sin(angle_back)
);
//定义两个变量计算cos和sin
var cos_angle = Math.Cos(angle);
var sin_angle = Math.Sin(angle);
// 什么都没有击中
if (HitWhitch == HitWhitchTypes.none)
{ }
//击中左上角的盒子
else if (HitWhitch == HitWhitchTypes.rect_left_top1)
{
//获取新的位置宽和高
var newWidth = startBox.Width - rotateBackV.X;
var newHeight = startBox.Height - rotateBackV.Y;

//用数学公式换算新的X轴和Y轴
var newX = startBox.Left + startBox.Width / 2 * (1 + cos_angle) + startBox.Height / 2 * sin_angle
- (newWidth / 2 * (1 + cos_angle) + newHeight / 2 * sin_angle);
var newY = startBox.Top + startBox.Height / 2 * (1 + cos_angle) - startBox.Width / 2 * sin_angle
- (newHeight / 2 * (1 + cos_angle) - newWidth / 2 * sin_angle);

//判断新的位置
if (newWidth < 0)
{
box.X = newX + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newX;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newY + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newY;
box.Height = newHeight;
}
DrawRects();

}//击中上边中间的盒子
else if (HitWhitch == HitWhitchTypes.rect_top2)
{
//获取新的位置宽和高
var newWidth = startBox.Width;
var newHeight = startBox.Height - rotateBackV.Y;
//定义变量获取开始的位置
var startLeftBottomV = new Vector(-startBox.Width / 2, startBox.Height / 2);
//定义变量获取旋转以后的位置(用数学公式)
var startRotatedLeftBottomV = new Vector(
startLeftBottomV.X * cos_angle + startLeftBottomV.Y * sin_angle,
startLeftBottomV.Y * cos_angle - startLeftBottomV.X * sin_angle);
//定义变量获取开始的中心位置
var startCenter = new Vector(startBox.Left + startBox.Width / 2,
startBox.Top + startBox.Height / 2);
//定义变量获取新的位置
var newLeftBottomV = new Vector(
-newWidth / 2, newHeight / 2);
//定义变量获取旋转后的位置
var newRotatedLeftBottomV = new Vector(
newLeftBottomV.X * cos_angle + newLeftBottomV.Y * sin_angle,
newLeftBottomV.Y * cos_angle - newLeftBottomV.X * sin_angle);
//最新的中心位置=开始的中心位置+开始旋转的位置-新的旋转后的位置
var newCenter = startCenter + startRotatedLeftBottomV - newRotatedLeftBottomV;
var newLeft = newCenter.X - newWidth / 2;//定义变量获取新的左边的距离
var newTop = newCenter.Y - newHeight / 2;//定义变量获取上边的离盒子的距离

if (newWidth < 0)
{
box.X = newLeft + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newLeft;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newTop + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newTop;
box.Height = newHeight;
}
DrawRects();
}//击中右上角的盒子
else if (HitWhitch == HitWhitchTypes.rect_right_top3)
{
//获取新的位置宽和高
var newWidth = startBox.Width + rotateBackV.X;
var newHeight = startBox.Height - rotateBackV.Y;

var startLeftBottomV = new Vector(-startBox.Width / 2, startBox.Height / 2);
var startRotatedLeftBottomV = new Vector(
startLeftBottomV.X * cos_angle + startLeftBottomV.Y * sin_angle,
startLeftBottomV.Y * cos_angle - startLeftBottomV.X * sin_angle);
var startCenter = new Vector(startBox.Left + startBox.Width / 2,
startBox.Top + startBox.Height / 2);

var newLeftBottomV = new Vector(
-newWidth / 2, newHeight / 2);
var newRotatedLeftBottomV = new Vector(
newLeftBottomV.X * cos_angle + newLeftBottomV.Y * sin_angle,
newLeftBottomV.Y * cos_angle - newLeftBottomV.X * sin_angle);

var newCenter = startCenter + startRotatedLeftBottomV - newRotatedLeftBottomV;
var newLeft = newCenter.X - newWidth / 2;
var newTop = newCenter.Y - newHeight / 2;

if (newWidth < 0)
{
box.X = newLeft + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newLeft;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newTop + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newTop;
box.Height = newHeight;
}
DrawRects();
}//击中右边中间的盒子
else if (HitWhitch == HitWhitchTypes.rect_right4)
{
//获取新的位置宽和高
var newWidth = startBox.Width + rotateBackV.X;
var newHeight = startBox.Height;

var startLeftBottomV = new Vector(-startBox.Width / 2, startBox.Height / 2);
var startRotatedLeftBottomV = new Vector(
startLeftBottomV.X * cos_angle + startLeftBottomV.Y * sin_angle,
startLeftBottomV.Y * cos_angle - startLeftBottomV.X * sin_angle);
var startCenter = new Vector(startBox.Left + startBox.Width / 2,
startBox.Top + startBox.Height / 2);

var newLeftBottomV = new Vector(
-newWidth / 2, newHeight / 2);
var newRotatedLeftBottomV = new Vector(
newLeftBottomV.X * cos_angle + newLeftBottomV.Y * sin_angle,
newLeftBottomV.Y * cos_angle - newLeftBottomV.X * sin_angle);

var newCenter = startCenter + startRotatedLeftBottomV - newRotatedLeftBottomV;
var newLeft = newCenter.X - newWidth / 2;
var newTop = newCenter.Y - newHeight / 2;

if (newWidth < 0)
{
box.X = newLeft + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newLeft;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newTop + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newTop;
box.Height = newHeight;
}
DrawRects();
}//击中右下角盒子
else if (HitWhitch == HitWhitchTypes.rect_right_bottom5)
{
//获取新的位置宽和高
var newWidth = startBox.Width + rotateBackV.X;
var newHeight = startBox.Height + rotateBackV.Y;
//定义变量左上角不动
var startTopleft = new Vector(-startBox.Width / 2, -startBox.Height / 2);
//旋转后的左上角位置
var startRotatedTopleft = new Vector(
startTopleft.X * cos_angle + startTopleft.Y * sin_angle,
startTopleft.Y * cos_angle - startTopleft.X * sin_angle);
//开始的中心位
var startCenter = new Vector(startBox.Left + startBox.Width / 2,
startBox.Top + startBox.Height / 2);
//新的左上角位置
var newLeftTopleft = new Vector(
-newWidth / 2, -newHeight / 2);
//新的旋转后的新位置
var newRotatedTopleft = new Vector(
newLeftTopleft.X * cos_angle + newLeftTopleft.Y * sin_angle,
newLeftTopleft.Y * cos_angle - newLeftTopleft.X * sin_angle);
//新的中心位置
var newCenter = startCenter + startRotatedTopleft - newRotatedTopleft;
var newLeft = newCenter.X - newWidth / 2;
var newTop = newCenter.Y - newHeight / 2;

if (newWidth < 0)
{
box.X = newLeft + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newLeft;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newTop + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newTop;
box.Height = newHeight;
}

DrawRects();

}//击中下边中间的盒子
else if (HitWhitch == HitWhitchTypes.rect_bottom6)
{
//获取新的位置宽和高
var newWidth = startBox.Width;
var newHeight = startBox.Height + rotateBackV.Y;
//定义变量左上角不动
var startTopleft = new Vector(-startBox.Width / 2, -startBox.Height / 2);
//旋转后的左上角位置
var startRotatedTopleft = new Vector(
startTopleft.X * cos_angle + startTopleft.Y * sin_angle,
startTopleft.Y * cos_angle - startTopleft.X * sin_angle);
//开始的中心位
var startCenter = new Vector(startBox.Left + startBox.Width / 2,
startBox.Top + startBox.Height / 2);
//新的左上角位置
var newLeftTopleft = new Vector(
-newWidth / 2, -newHeight / 2);
//新的旋转后的新位置
var newRotatedTopleft = new Vector(
newLeftTopleft.X * cos_angle + newLeftTopleft.Y * sin_angle,
newLeftTopleft.Y * cos_angle - newLeftTopleft.X * sin_angle);
//新的中心位置
var newCenter = startCenter + startRotatedTopleft - newRotatedTopleft;
var newLeft = newCenter.X - newWidth / 2;
var newTop = newCenter.Y - newHeight / 2;

if (newWidth < 0)
{
box.X = newLeft + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newLeft;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newTop + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newTop;
box.Height = newHeight;
}

DrawRects();


}//击中左下角盒子
else if (HitWhitch == HitWhitchTypes.rect_left_bottom7)
{
//获取左下角的位置宽和高
var newWidth = startBox.Width - rotateBackV.X;
var newHeight = startBox.Height + rotateBackV.Y;
//定义变量右上角不动
var startTopright = new Vector(startBox.Width / 2, -startBox.Height / 2);
//开始的右上角位置
//RotateVector();
var startRotatedTopright = new Vector(
startTopright.X * cos_angle + startTopright.Y * sin_angle,
startTopright.Y * cos_angle - startTopright.X * sin_angle);
//开始的中心位置
var startCenter = new Vector(startBox.Left + startBox.Width / 2,
startBox.Top + startBox.Height / 2);
//新的右上角位置
var newLeftTopright = new Vector(
newWidth / 2, -newHeight / 2);
//新的旋转后的新位置
var newRotatedTopright = new Vector(
newLeftTopright.X * cos_angle + newLeftTopright.Y * sin_angle,
newLeftTopright.Y * cos_angle - newLeftTopright.X * sin_angle);
//新的中心位置
var newCenter = startCenter + startRotatedTopright - newRotatedTopright;
var newLeft = newCenter.X - newWidth / 2;
var newTop = newCenter.Y - newHeight / 2;

if (newWidth < 0)
{
box.X = newLeft + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newLeft;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newTop + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newTop;
box.Height = newHeight;
}
DrawRects();
}//击中左边中间的盒子
else if (HitWhitch == HitWhitchTypes.rect_left8)
{
//获取新的位置宽和高
var newWidth = startBox.Width - rotateBackV.X;
var newHeight = startBox.Height;

//换算新的X轴和Y轴
var newX = startBox.Left + startBox.Width / 2 * (1 + cos_angle) + startBox.Height / 2 * sin_angle
- (newWidth / 2 * (1 + cos_angle) + newHeight / 2 * sin_angle);
var newY = startBox.Top + startBox.Height / 2 * (1 + cos_angle) - startBox.Width / 2 * sin_angle
- (newHeight / 2 * (1 + cos_angle) - newWidth / 2 * sin_angle);

if (newWidth < 0)
{
box.X = newX + newWidth;
box.Width = -newWidth;
}
else
{
box.X = newX;
box.Width = newWidth;
}

if (newHeight < 0)
{
box.Y = newY + newHeight;
box.Height = -newHeight;
}
else
{
box.Y = newY;
box.Height = newHeight;
}
DrawRects();
}//击中盒子旋转的按钮
else if (HitWhitch == HitWhitchTypes.box_rotate)
{
var v1 = new Vector(0, -40 - startBox.Height / 2);
var v3 = v1 + rotateBackV;
var diffAngle = Math.Atan2(v3.X, -v3.Y);
var diffDegree = diffAngle / Math.PI * 180;
Degree = startDegree + diffDegree;
DrawRects();
}//击中截图的截取区域
else if (HitWhitch == HitWhitchTypes.rect_box)
{
//拖动选中的区域
box.X = startBox.X + v2.X;
box.Y = startBox.Y + v2.Y;
DrawRects();
}
}

//鼠标移动箭头图标的变化
else
{
var p = e.GetPosition(path);//定义一个变量相对于path左上角旋转后的位置
//rect1左上角的位置X和Y轴都要变化
if (rect_left_top1.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_left_top1;
Cursor = Cursors.SizeNWSE;//获取用户选中的图标
}//中间的上下拉动变化
else if (rect_top2.Rect.Contains(p))
{

HitWhitch = HitWhitchTypes.rect_top2;
Cursor = Cursors.SizeNS;

}//右上角的变化
else if (rect_right_top3.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_right_top3;
Cursor = Cursors.SizeNESW;
}//右边的中间
else if (rect_right4.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_right4;
Cursor = Cursors.SizeWE;
}//右下角
else if (rect_right_bottom5.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_right_bottom5;
Cursor = Cursors.SizeNWSE;
}//底部中间
else if (rect_bottom6.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_bottom6;
Cursor = Cursors.SizeNS;
}//左下角
else if (rect_left_bottom7.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_left_bottom7;
Cursor = Cursors.SizeNESW;
}//左边中间
else if (rect_left8.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_left8;
Cursor = Cursors.SizeWE;
}
//点击旋转
else if (circular.Rect.Contains(p))
{
Cursor = RotateCursor;
HitWhitch = HitWhitchTypes.box_rotate;
//Cursor = Cursors.Hand;
}
//击中盒子中间截取区域
else if (rect_box.Rect.Contains(p))
{
HitWhitch = HitWhitchTypes.rect_box;
Cursor = Cursors.SizeAll;
}//什么都没有击中
else
{
HitWhitch = HitWhitchTypes.none;
Cursor = Cursors.Arrow;
}

 //鼠标初始化位置

//鼠标初始位置
public void DrawRects()
{

path.SetValue(Canvas.LeftProperty, box.Left);
path.SetValue(Canvas.TopProperty, box.Top);

box_rotate.CenterX = box.Width / 2;
box_rotate.CenterY = box.Height / 2;
box_rotate.Angle = Degree;

rect_left_top1.Rect = new Rect(-RectSize / 2, -RectSize / 2, RectSize, RectSize);

rect_top2.Rect = new Rect(box.Width / 2 - RectSize / 2, -RectSize / 2, RectSize, RectSize);

rect_right_top3.Rect = new Rect(box.Width - RectSize / 2, -RectSize / 2, RectSize, RectSize);

rect_right4.Rect = new Rect(box.Width - RectSize / 2, box.Height / 2 - RectSize / 2, RectSize, RectSize);

rect_right_bottom5.Rect = new Rect(box.Width - RectSize / 2, box.Height - RectSize / 2, RectSize, RectSize);

rect_bottom6.Rect = new Rect(box.Width / 2 - RectSize / 2, box.Height - RectSize / 2, RectSize, RectSize);

rect_left_bottom7.Rect = new Rect(-RectSize / 2, box.Height - RectSize / 2, RectSize, RectSize);

rect_left8.Rect = new Rect(-RectSize / 2, box.Height / 2 - RectSize / 2, RectSize, RectSize);
//旋转
circular.Rect = new Rect(box.Width / 2 - RectSize / 2, -40 - RectSize / 2, RectSize, RectSize);
//八个盒子的大小
rect_box.Rect = new Rect(0, 0, box.Width, box.Height);
//连接第二个和旋转的直线
line.StartPoint = new Point(box.Width / 2, -RectSize / 2 );
line.EndPoint = new Point(box.Width / 2, -40 + RectSize / 2);

}

//鼠标按下时候的位置
private void Window_MouseLeftMouseDown(object sender, MouseButtonEventArgs e)
{
IsMouseDown = true;
startBox = box;
startDegree = Degree;

MouseStartPos2 = e.GetPosition(outerCanvas);

}

//鼠标抬起
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
IsMouseDown = false;
}

wpf实现qq截图八个点以及旋转功能

原文:https://www.cnblogs.com/xzy98/p/10656023.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!