前言
接着上周写的 绘制箭头继续更新。
1.WPF实现截屏「仿微信」
2.WPF 实现截屏控件之移动(二)「仿微信」
3.WPF 截图控件之伸缩(三) 「仿微信」
4.WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
5.WPF 截图控件之绘制箭头(五)「仿微信」
正文
一、在绘制箭头的时候鼠标的当前Point超出Rect的区域时就不对了。所以需要做计算求相似点转换公式如下
公式代码
01
—
代码如下
if (drawArrow.Left <= rect.Left)
{
var a = rect.Left - pointStart.X;
var d = current.X - rect.Left;
var bc = current.Y - pointStart.Y;
var c = bc / (a / d + 1);
var b = a / d * c;
if (current.Y > pointStart.Y)
current.Y = pointStart.Y + b;
else
current.Y = pointStart.Y - b;
current.X = pointStart.X + a;
}
else if (drawArrow.Right >= rect.Right)
{
var a = rect.Right - pointStart.X;
var d = current.X - rect.Right;
var bc = current.Y - pointStart.Y;
var c = bc / (a / d + 1);
var b = a / d * c;
if (current.Y > pointStart.Y)
current.Y = pointStart.Y + b;
else
current.Y = pointStart.Y - b;
current.X = pointStart.X + a;
}
else if (drawArrow.Top <= rect.Top)
{
var a = pointStart.Y - rect.Top;
var d = rect.Top - current.Y;
var bc = current.X - pointStart.X;
var c = bc / (a / d + 1);
var b = a / d * c;
if (current.X > pointStart.X)
current.X = pointStart.X + b;
else
current.X = pointStart.X - b;
current.Y = pointStart.Y;
}
else if (drawArrow.Bottom >= rect.Bottom)
{
var a = rect.Bottom - pointStart.Y;
var d = current.Y - rect.Bottom;
var bc = current.X - pointStart.X;
var c = bc / (a / d + 1);
var b = a / d * c;
if (current.X > pointStart.X)
current.X = pointStart.X + b;
else
current.X = pointStart.X - b;
current.Y = pointStart.Y + a;
}
如效果如果只是右侧的话判定没问题,如右上或者右下就会判定不对。
if (current.X < rect.Left
||
current.X > rect.Right
||
current.Y < rect.Top
||
current.Y > rect.Bottom)
{
if (current.X >= vPoint.X && current.Y < vPoint.Y)
{
double a1 = (current.Y - vPoint.Y) / (current.X - vPoint.X);
double b1 = vPoint.Y - a1 * vPoint.X;
double xTop = (rect.Top - b1) / a1;
double yRight = a1 * rect.Right + b1;
if (xTop <= rect.Right)
{
current.X = xTop;
current.Y = rect.Top;
}
else
{
current.X = rect.Right;
current.Y = yRight;
}
}
else if (current.X > vPoint.X && current.Y > vPoint.Y)
{
double a1 = (current.Y - vPoint.Y) / (current.X - vPoint.X);
double b1 = vPoint.Y - a1 * vPoint.X;
double xBottom = (rect.Bottom - b1) / a1;
double yRight = a1 * rect.Right + b1;
if (xBottom <= rect.Right)
{
current.X = xBottom;
current.Y = rect.Bottom;
}
else
{
current.X = rect.Right;
current.Y = yRight;
}
}
else if (current.X < vPoint.X && current.Y < vPoint.Y)
{
double a1 = (current.Y - vPoint.Y) / (current.X - vPoint.X);
double b1 = vPoint.Y - a1 * vPoint.X;
double xTop = (rect.Top - b1) / a1;
double yLeft = a1 * rect.Left + b1;
if (xTop >= rect.Left)
{
current.X = xTop;
current.Y = rect.Top;
}
else
{
current.X = rect.Left;
current.Y = yLeft;
}
}
else if (current.X < vPoint.X && current.Y > vPoint.Y)
{
double a1 = (current.Y - vPoint.Y) / (current.X - vPoint.X);
double b1 = vPoint.Y - a1 * vPoint.X;
double xBottom = (rect.Bottom - b1) / a1;
double yLeft = a1 * rect.Left + b1;
if (xBottom <= rect.Left)
{
current.X = rect.Left;
current.Y = yLeft;
}
else
{
current.X = xBottom;
current.Y = rect.Bottom;
}
}
}
完整代码如下
-
ScreenCut GitHub[1] -
ScreenCut.xaml Gitee[2] -
ScreenCut Gitee[3] -
ScreenCut.xaml Gitee[4]
02
—
效果预览
项目地址
-
框架名:WPFDevelopers -
作者:WPFDevelopers -
参与者:吴锋、闫佳慧 -
GitHub[5] -
Gitee[6]
参考资料
ScreenCut GitHub: https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers/Controls/ScreenCut/ScreenCut.cs
[2]ScreenCut.xaml Gitee: https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers/Themes/ScreenCut.xaml
[3]ScreenCut Gitee: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers/Controls/ScreenCut/ScreenCut.cs
[4]ScreenCut.xaml Gitee: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers/Themes/ScreenCut.xaml
[5]GitHub:: https://github.com/WPFDevelopersOrg
[6]Gitee:: https://gitee.com/WPFDevelopersOrg
原文始发于微信公众号(WPF开发者):WPF 截图控件之绘制箭头(六)「仿微信」
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/55164.html