WPF效果第一百九十七篇之Path范围内拖拽

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF效果第一百九十七篇之Path范围内拖拽相关的知识,希望对你有一定的参考价值。

前面效果中分享了彩色马蹄图的效果;这不今天再次在马蹄图的基础上,实现只能在Path内的拖拽效果;闲话不多扯直接看效果:

1、关于拖拽顶点实现色域范围选择,参考:

https://www.codeproject.com/Tips/828310/The-simplest-WPF-diagram-designer-part

2、通过Blend绘制了色度图中间区域的Path:

<Path Data="M95.75,76 L92.9375,78.6875 90.936644,81.186535 89.24817,82.935546 87.310123,86.185366 C87.310123,86.185366 85.310174,90.058861 85.122632,90.058861 84.93509,90.058861 83.934871,92.371344 83.934871,92.371344 L82.810165,94.183441 80.685364,98.619329 79.060687,103.1182 77.309941,107.80453 75.996727,111.42806 74.996561,115.11403 74.308945,117.98822 72.621704,123.92405 70.746437,132.10985 69.746295,138.04575 68.871167,142.48258 67.18343,151.41775 66.183801,161.60202 65.245666,167.64207 64.66225,174.22327 64.245183,181.88785 C64.245183,181.88785 63.745109,188.2191 63.745109,188.63566 63.745109,189.05221 63.870129,197.96605 63.870129,197.96605 L62.994995,206.33872 62.494919,220.33482 63.120014,235.45561 63.370053,250.45144 63.870129,259.07403 64.661917,270.61249 65.161993,286.60767 67.162299,304.26902 68.162451,322.43092 69.662681,334.59416 71.079564,344.59103 71.57964,355.00442 73.163216,364.16884 74.33006,375.83224 75.371886,384.78771 77.184663,395.59716 78.559873,405.28142 79.872574,414.15388 81.247784,422.31822 83.081398,431.56524 84.164897,441.64536 86.498586,453.64192 89.249007,467.88783&#xd;&#xa;92.06181,484.44595 95.56233,502.25338 99.125359,518.37379 101.16733,528.20432 106.25142,548.8651 110.66874,567.35983 112.66904,574.60777 115.08606,583.43859 117.83647,593.01919 120.87972,600.7881 124.58747,613.18011 127.75461,622.34417 130.00495,629.42549 135.67245,645.6292 139.00714,653.58502 143.19531,664.76933 145.75729,670.39298 148.3826,675.7027 150.38289,679.95148 154.44608,688.01296 157.07087,693.07319 159.82075,696.94707 162.32161,700.57104&#xd;&#xa;164.88499,704.19501 168.57302,708.63124 173.01066,713.50533 177.32378,717.62863 181.51239,721.25307 184.63785,724.37666 187.07571,726.68899 189.01399,727.93861 190.51422,728.12605 192.3895,726.93938 196.39011,724.81498 200.70327,723.003 205.51599,720.50371 214.26728,716.3174 222.64352,712.1311 228.83194,708.94451 237.83379,704.63324 246.3356,700.50941 254.46237,696.94793 259.52515,694.07375 273.02722,687.38815 286.71682,680.64008 300.46841,673.892&#xd;&#xa;320.15835,664.70712 327.78449,660.89571 335.78568,656.95933 344.30778,652.73137 352.10043,648.98244 356.78914,646.42067 374.04171,638.17302 387.58542,631.17502 408.67156,621.21952 433.80032,609.34791 471.80571,590.47825 513.56224,570.1091 557.35995,548.97107 581.94696,536.47463 592.07429,532.07828 600.07476,527.99605 604.24205,525.99662 607.40919,524.41373 609.07605,523.53898 610.20122,522.95582 611.15934,522.12272 610.20086,521.41459 607.1591,517.74897&#xd;&#xa;604.242,514.66651 600.99152,511.20916 597.85564,507.59561 594.19885,503.72171 591.32342,500.69132 587.15618,496.2135 582.48882,491.29824 577.7589,486.3829 574.25839,482.63397 570.98756,479.33292 566.75731,474.73005 565.06952,473.01171 562.2566,470.13753 559.94376,467.45079 553.77622,460.97355 547.47316,454.39201 542.0036,448.39372 537.64878,443.52011 530.60611,436.10564 528.20992,433.50222 524.50104,429.69081 521.66728,426.87911 512.06169,416.73617&#xd;&#xa;501.01838,405.03117 492.35043,395.7005 475.30623,377.53901 460.86659,362.29336 444.13494,345.25655 431.2997,331.01061 416.67249,315.97311 401.71194,299.68608 391.54376,288.93914 384.61567,281.26471 379.50031,276.19278 374.32248,270.56952 370.04057,266.44555 366.29001,262.61331 362.28945,258.61459 356.7886,252.65781 351.9129,247.3678 348.20402,243.36894 342.95324,237.87051 339.84861,234.76723 334.22278,228.83142 329.36787,224.16593 325.24225,220.08376&#xd;&#xa;323.49199,217.87605 319.84564,214.12728 316.88687,210.8782 313.28217,207.25424 308.61481,202.79718 303.73908,197.44454 299.92602,193.67478 295.94626,189.71757 292.32072,185.96864 287.23663,181.42827 283.56942,177.53355 278.13111,172.63911 273.85965,168.64025 268.69221,163.84995 256.02366,152.16578 244.89701,142.2936 243.08424,140.45037 239.23992,137.01385 235.67689,133.63981 228.20703,127.45408 222.39367,122.86164 216.11148,117.67562 211.70458,114.20785&#xd;&#xa;206.66216,110.34437 200.59876,105.88731 195.86889,102.61741 189.30542,98.202002 185.8049,95.952644 180.09571,92.203521 174.5949,88.621401 162.69729,81.87313 149.69537,75.250214 145.17387,73.250784 139.49073,70.985807 134.53686,69.126761 130.16121,67.720911 123.67067,66.919431 120.87859,66.643137 117.87814,66.335556 116.14872,66.419199 114.77352,66.440026 113.27329,66.689955 111.81474,66.939884 110.06448,67.29395 108.46008,67.814635 106.50145,68.626903&#xd;&#xa;104.60534,69.626619 102.81341,70.584679 101.35486,71.501085 99.625434,72.646592 98.166884,73.667134 96.958371,74.708504 96.395787,75.333326 z"
      Name="ColorSpaceBackPath" Fill="#02FFFFFF" Stretch="Fill" Height="661.979" Width="548.583"
      Canvas.Left="1.5" Canvas.Top="54.3"
      Stroke="Transparent" StrokeThickness="0"/>

3、查了资料说可以用下面方式:

ColorSpaceBackPath.Data.FillContains(point);

4、效果是有了,可能是我姿势不对,没达到我的预期;这时我又无脑的想到了HitTest,毕竟前面也玩了好几次了;再玩一次也能接受:

VisualTreeHelper.HitTest(MyCanvas, new HitTestFilterCallback(OnHitTestFilterCallback), new HitTestResultCallback(OnHitTestResultCallback), new PointHitTestParameters(currentPoint));

5、在OnHitTestFilterCallback中:

string name = path.Name;
if(name.Equals("ColorSpaceBackPath"))

    return HitTestFilterBehavior.ContinueSkipChildren;

6、在OnHitTestResultCallback中:

Path path = result.VisualHit as Path;
if(path != null)

    string name = path.Name;
    if(name.Equals("ColorSpaceBackPath"))
    
        CurrentShape.Left = CurrentPoint.X;
        CurrentShape.Top = CurrentPoint.Y;
        return HitTestResultBehavior.Stop;
    

最终简单的效果先这样吧;以后有时间的话,可以再去摸索一下更复杂的效果;编程不息、Bug不止、无Bug、无生活;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!

以上是关于WPF效果第一百九十七篇之Path范围内拖拽的主要内容,如果未能解决你的问题,请参考以下文章

WPF效果第一百九十六篇之彩色马蹄形图

WPF效果第一百九十篇之再耍ListBox

WPF效果第一百九十四篇之伸缩面板

WPF效果第一百九十一篇之框选ListBox

WPF效果第一百九十三篇之登录实现

WPF效果第一百九十五篇之又玩ListBox