wpf 带阴影 无边框_原创:一个带阴影的Border(WPF控件)成品

本文介绍了如何在WPF中创建一个带阴影且无边框的Border控件,通过自定义ShadowBorder类并注册相关依赖属性,实现了阴影效果的调整,包括阴影宽度、角度和颜色等。同时,该控件还支持边角圆角和背景颜色等特性。
摘要由CSDN通过智能技术生成

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Windows.Controls;6 usingSystem.Windows;7 usingSystem.Windows.Media;8 9 namespaceYZKFrame.Controls.WpfControls10 {11 ///12 ///带阴影的边框13 ///14 publicclassShadowBorder : Decorator15 {16 17 staticShadowBorder()18 {19 20 DefaultStyleKeyProperty.OverrideMetadata(21 typeof(ShadowBorder),22 newFrameworkPropertyMetadata(typeof(ShadowBorder)));23 24 ShadowLightArcProperty=DependencyProperty.Register(25 "ShadowLightArc",typeof(double),typeof(ShadowBorder),26 newFrameworkPropertyMetadata((double)45,27 FrameworkPropertyMetadataOptions.AffectsRender28 |FrameworkPropertyMetadataOptions.AffectsArrange29 |FrameworkPropertyMetadataOptions.AffectsMeasure));30 31 ShadowWidthProperty=DependencyProperty.Register(32 "ShadowWidth",typeof(double),typeof(ShadowBorder),33 newFrameworkPropertyMetadata((double)4,34 FrameworkPropertyMetadataOptions.AffectsRender35 |FrameworkPropertyMetadataOptions.AffectsArrange36 |FrameworkPropertyMetadataOptions.AffectsMeasure));37 38 ShadowBrushProperty=DependencyProperty.Register(39 "ShadowBrush",typeof(Brush),typeof(ShadowBorder),40 newFrameworkPropertyMetadata(null,41 FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender42 |FrameworkPropertyMetadataOptions.AffectsRender));43 44 BorderCornerRadiusProperty=DependencyProperty.Register(45 "BorderCornerRadius",typeof(CornerRadius),typeof(ShadowBorder),46 newFrameworkPropertyMetadata(newCornerRadius(4),47 FrameworkPropertyMetadataOptions.AffectsRender48 |FrameworkPropertyMetadataOptions.AffectsArrange49 |FrameworkPropertyMetadataOptions.AffectsMeasure));50 51 52 BackgroundProperty=53 Panel.BackgroundProperty.AddOwner(typeof(ShadowBorder),54 newFrameworkPropertyMetadata(null,55 FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender56 |FrameworkPropertyMetadataOptions.AffectsRender));57 58 BorderBrushProperty=59 DependencyProperty.Register("BorderBrush",typeof(Brush),60 typeof(ShadowBorder),newFrameworkPropertyMetadata(newSolidColorBrush(Color.FromArgb(255,100,100,100)),61 FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender62 |FrameworkPropertyMetadataOptions.AffectsRender));63 64 BorderThicknessProperty=65 DependencyProperty.Register("BorderThickness",typeof(Thickness),66 typeof(ShadowBorder),67 newFrameworkPropertyMetadata(newThickness(1),68 FrameworkPropertyMetadataOptions.AffectsRender69 |FrameworkPropertyMetadataOptions.AffectsMeasure,70 newPropertyChangedCallback(ShadowBorder.OnBorderThicknessChanged)),71 newValidateValueCallback(ShadowBorder.IsThicknessValid));72 73 PaddingProperty=74 DependencyProperty.Register("Padding",75 typeof(Thickness),typeof(ShadowBorder),76 newFrameworkPropertyMetadata(newThickness(),77 FrameworkPropertyMetadataOptions.AffectsRender78 |FrameworkPropertyMetadataOptions.AffectsMeasure),79 newValidateValueCallback(ShadowBorder.IsThicknessValid));80 81 MeasureDataProperty=DependencyProperty.Register(82 "MeasureData",typeof(ShadowBorderMeasureData),83 typeof(ShadowBorder),84 newFrameworkPropertyMetadata(newShadowBorderMeasureData(),85 FrameworkPropertyMetadataOptions.None));86 87 BorderGeometryCacheProperty=DependencyProperty.Register(88 "BorderGeometryCache",typeof(StreamGeometry),typeof(ShadowBorder),89 newFrameworkPropertyMetadata(null,90 FrameworkPropertyMetadataOptions.None));91 92 X_ShadowGeometryCacheProperty=DependencyProperty.Register(93 "X_ShadowGeometryCache",typeof(Geometry),typeof(ShadowBorder),94 newFrameworkPropertyMetadata(null,95 FrameworkPropertyMetadataOptions.None));96 97 Y_ShadowGeometryCacheProperty=DependencyProperty.Register(98 "Y_ShadowGeometryCache",typeof(Geometry),typeof(ShadowBorder),99 newFrameworkPropertyMetadata(null,100 FrameworkPropertyMetadataOptions.None));101 102 BackgroundGeometryCacheProperty=DependencyProperty.Register(103 "BackgroundGeometryCache",typeof(Geometry),typeof(ShadowBorder),104 newFrameworkPropertyMetadata(null,105 FrameworkPropertyMetadataOptions.None));106 107 X_ShadowBrushCacheProperty=108 DependencyProperty.Register("X_ShadowBrushCache",typeof(Brush),109 typeof(ShadowBorder),newFrameworkPropertyMetadata(null,110 FrameworkPropertyMetadataOptions.None));111 112 Y_ShadowBrushCacheProperty=113 DependencyProperty.Register("Y_ShadowBrushCache",typeof(Brush),114 typeof(ShadowBorder),newFrameworkPropertyMetadata(null,115 FrameworkPropertyMetadataOptions.None));116 117 ShadowColorProperty=118 DependencyProperty.Register("ShadowColor",typeof(Color),119 typeof(ShadowBorder),newFrameworkPropertyMetadata(Color.FromArgb(255,146,146,146),120 FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender121 |FrameworkPropertyMetadataOptions.AffectsRender,122 newPropertyChangedCallback(ShadowBorder.OnShadowColorChanged)));123 }124 125 #region属性依赖项126 127 publicstaticreadonlyDependencyProperty ShadowBrushProperty;128 129 publicstaticreadonlyDependencyProperty ShadowLightArcProperty;130 131 publicstaticreadonlyDependencyProperty ShadowWidthProperty;132 133 publicstaticreadonlyDependencyProperty BorderCornerRadiusProperty;134 135 publicstaticreadonlyDependencyProperty BackgroundProperty;136 137 publicstaticreadonlyDependencyProperty BorderBrushProperty;138 139 publicstaticreadonlyDependencyProperty BorderThicknessProperty;140 141 publicstaticreadonlyDependencyProperty PaddingProperty;142 143 publicstaticreadonlyDependencyProperty MeasureDataProperty;144 145 publicstaticreadonlyDependencyProperty ShadowColorProperty;146 147 privatestaticreadonlyDependencyProperty BorderGeometryCacheProperty;148 149 privatestaticreadonlyDependencyProperty X_ShadowGeometryCacheProperty;150 151 privatestaticreadonlyDependencyProperty Y_ShadowGeometryCacheProperty;152 153 privatestaticreadonlyDependencyProperty BackgroundGeometryCacheProperty;154 155 privatestaticreadonlyDependencyProperty X_ShadowBrushCacheProperty;156 157 privatestaticreadonlyDependencyProperty Y_ShadowBrushCacheProperty;158 159 #endregion160 161 #region私有静态方法162 163 privatestaticboolIsThicknessValid(objectvalue)164 {165 Thickness thickness=(Thickness)value;166 returnthickness.IsValid(false,false,false,false);167 }168 169 privatestaticvoidOnBorderThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)170 {171 }172 173 privatestaticvoidOnShadowColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)174 {175 ShadowBorder border=(ShadowBorder)d;176 border.OnShadowColorChanged((Color)e.NewValue, (Color)e.OldValue);177 }178 179 #endregion180 181 #region属性182 183 #region私有184 185 privateShadowBorderMeasureData MeasureData186 {187 get188 {189 return(ShadowBorderMeasureData)GetValue(MeasureDataProperty);190 }191 }192 193 privateStreamGeometry BorderGeometryCache194 {195 get{return(StreamGeometry)GetValue(BorderGeometryCacheProperty); }196 set{ SetValue(BorderGeometryCacheProperty, value); }197 }198 199 privateGeometry X_ShadowGeometryCache200 {201 get{return(Geometry)GetValue(X_ShadowGeometryCacheProperty); }202 set{ SetValue(X_ShadowGeometryCacheProperty, value); }203 }204 205 privateGeometry Y_ShadowGeometryCache206 {207 get{return(Geometry)GetValue(Y_ShadowGeometryCacheProperty); }208 set{ SetValue(Y_ShadowGeometryCacheProperty, value); }209 }210 211 privateGeometry BackgroundGeometryCache212 {213 get{return(Geometry)GetValue(BackgroundGeometryCacheProperty); }214 set{ SetValue(BackgroundGeometryCacheProperty, value); }215 }216 217 privateBrush X_ShadowBrushCache218 {219 get220 {221 return(Brush)base.GetValue(X_ShadowBrushCacheProperty);222 }223 set224 {225 base.SetValue(X_ShadowBrushCacheProperty, value);226 }227 }228 229 privateBrush Y_ShadowBrushCache230 {231 get232 {233 return(Brush)base.GetValue(Y_ShadowBrushCacheProperty);234 }235 set236 {237 base.SetValue(Y_ShadowBrushCacheProperty, value);238 }239 }240 #endregion241 242 #region公有243 ///244 ///获取或设置阴影颜色属性245 ///246 publicColor ShadowColor247 {248 get{return(Color)GetValue(ShadowColorProperty); }249 set{ SetValue(ShadowColorProperty, value); }250 }251 ///252 ///获取或设置阴影画刷253 ///254 publicBrush ShadowBrush255 {256 get257 {258 return(Brush)base.GetValue(ShadowBrushProperty);259 }260 set261 {262 base.SetValue(ShadowBrushProperty, value);263 }264 }265 ///266 ///获取或设置阴影光源照射的角度:相对于x轴顺时针角度,度为单位267 ///268 publicdoubleShadowLightArc269 {270 get271 {272 return(double)base.GetValue(ShadowLightArcProperty);273 }274 set275 {276 base.SetValue(ShadowLightArcProperty, value);277 }278 }279 ///280 ///获取或设置阴影宽度281 ///282 publicdoubleShadowWidth283 {284 get285 {286 return(double)base.GetValue(ShadowWidthProperty);287 }288 set289 {290 base.SetValue(ShadowWidthProperty, value);291 }292 }293 ///294 ///获取或设置边框的倒角295 ///296 publicCornerRadius BorderCornerRadius297 {298 get299 {300 return(CornerRadius)base.GetValue(BorderCornerRadiusProperty);301 }302 set303 {304 base.SetValue(BorderCornerRadiusProperty, value);305 }306 }307 308 publicBrush Background309 {310 get311 {312 return(Brush)base.GetValue(BackgroundProperty);313 }314 set315 {316 base.SetValue(BackgroundProperty, value);317 }318 }319 320 publicBrush BorderBrush321 {322 get323 {324 return(Brush)base.GetValue(BorderBrushProperty);325 }326 set327 {328 base.SetValue(BorderBrushProperty, value);329 }330 }331 332 publicThickness BorderThickness333 {334 get335 {336 return(Thickness)base.GetValue(BorderThicknessProperty);337 }338 set339 {340 base.SetValue(BorderThicknessProperty, value);341 }342 }343 344 publicThickness Padding345 {346 get347 {348 return(Thickness)base.GetValue(PaddingProperty);349 }350 set351 {352 base.SetValue(PaddingProperty, value);353 }354 }355 #endregion356 357 #endregion358 359 #region私有方法360 361 protectedvirtualvoidOnShadowColorChanged(Color newcolor,Color oldcolor)362 {363 Brush x_b;364 Brush y_b;365 MeasureData.SetShadowBrush(ShadowLightArc,366 ShadowColor,outx_b,outy_b);367 X_ShadowBrushCache=x_b;368 Y_ShadowBrushCache=y_b;369 }370 371 #endregion372 373 #region已重载方法374 375 protectedoverrideSize MeasureOverride(Size constraint)376 {377 ShadowBorderMeasureData measuredata=MeasureData;378 measuredata.MeasureData(constraint,379 ShadowWidth,380 ShadowLightArc,381 BorderCornerRadius,382 BorderThickness,383 Padding);384 base.MeasureOverride(measuredata.ChildSize);385 returnconstraint;386 }387 388 protectedoverrideSize ArrangeOverride(Size arrangeSize)389 {390 ShadowBorderMeasureData measuredata=MeasureData;391 measuredata.ArrangeData(arrangeSize,392 ShadowWidth,393 ShadowLightArc,394 BorderCornerRadius,395 BorderThickness,396 Padding);397 UIElement child=this.Child;398 if(child!=null)399 child.Arrange(measuredata.ChildRect);400 StreamGeometry back=newStreamGeometry();401 using(StreamGeometryContext context=back.Open())402 {403 measuredata.SetBackgroundGeometry(context);404 }405 back.Freeze();406 BackgroundGeometryCache=back;407 if(measuredata.HasBorder)408 {409 StreamGeometry x=newStreamGeometry();410 using(StreamGeometryContext context=x.Open())411 {412 measuredata.SetBorderGeometry(context);413 }414 x.Freeze();415 BorderGeometryCache=x;416 }417 else418 BorderGeometryCache=null;419 if(measuredata.HasShadow)420 {421 Geometry x_shadow;422 Geometry y_shadow;423 measuredata.GetShadowGeometry(outx_shadow,outy_shadow);424 if(x_shadow!=null&&x_shadow.CanFreeze)425 x_shadow.Freeze();426 if(y_shadow!=null&&y_shadow.CanFreeze)427 y_shadow.Freeze();428 X_ShadowGeometryCache=x_shadow;429 Y_ShadowGeometryCache=y_shadow;430 Brush x_b;431 Brush y_b;432 measuredata.SetShadowBrush(ShadowLightArc,433 ShadowColor,outx_b,outy_b);434 X_ShadowBrushCache=x_b;435 Y_ShadowBrushCache=y_b;436 }437 else438 {439 X_ShadowGeometryCache=null;440 Y_ShadowGeometryCache=null;441 X_ShadowBrushCache=null;442 Y_ShadowBrushCache=null;443 }444 returnarrangeSize;445 }446 447 protectedoverridevoidOnRender(DrawingContext drawingContext)448 {449 base.OnRender(drawingContext);450 if(Background!=null&&BackgroundGeometryCache!=null)451 {452 drawingContext.DrawGeometry(453 Background,null, BackgroundGeometryCache);454 }455 if(MeasureData.HasShadow)456 {457 Brush shadowb=ShadowBrush;458 if(shadowb!=null)459 {460 Geometry y_shadow=Y_ShadowGeometryCache;461 if(y_shadow!=null)462 {463 drawingContext.DrawGeometry(464 shadowb,null, y_shadow);465 }466 Geometry x_shadow=X_ShadowGeometryCache;467 if(x_shadow!=null)468 {469 drawingContext.DrawGeometry(470 shadowb,null, x_shadow);471 }472 }473 else474 {475 Brush x_b=X_ShadowBrushCache;476 Brush y_b=Y_ShadowBrushCache;477 if(x_b==null||y_b==null)478 {479 MeasureData.SetShadowBrush(ShadowLightArc,480 ShadowColor,outx_b,outy_b);481 X_ShadowBrushCache=x_b;482 Y_ShadowBrushCache=y_b;483 }484 Geometry x_shadow=X_ShadowGeometryCache;485 if(x_shadow!=null)486 {487 drawingContext.DrawGeometry(488 x_b,null, x_shadow);489 }490 Geometry y_shadow=Y_ShadowGeometryCache;491 if(y_shadow!=null)492 {493 drawingContext.DrawGeometry(494 y_b,null, y_shadow);495 }496 }497 }498 StreamGeometry border=BorderGeometryCache;499 if(border!=null)500 {501 drawingContext.DrawGeometry(502 BorderBrush,null, border);503 }504 505 506 }507 508 #endregion509 510 #region私有类511 512 privateclassShadowBorderMeasureData513 {514 515 internalShadowBorderMeasureData()516 {517 g_ShadowLightPosition=MeasureShadowLightPosition.None;518 g_Dy=0;519 g_Dy=0;520 p_ShadowBorderInnerSize=newSize();521 p_ShadowBorderOuterSize=newSize();522 p_BorderInnerSize=newSize();523 p_ChildSize=newSize();524 g_BorderCornerRadius=newCornerRadius();525 p_ShadowBorderOuterRect=newRect();526 p_ShadowBorderInnerRect=newRect();527 p_BorderInnerRect=newRect();528 p_ChildRect=newRect();529 g_LightArc=0;530 }531 532 privateMeasureShadowLightPosition g_ShadowLightPosition;/*阴影光源的位置*/533 534 privatedoubleg_Dx;/*X轴偏移量*/535 privatedoubleg_Dy;/*Y轴偏移量*/536 privatedoubleg_LightArc;/*光源角度*/537 privateSize p_ShadowBorderOuterSize;/*所有外围大小*/538 privateSize p_ShadowBorderInnerSize;/*除去阴影部份的外围大小*/539 privateSize p_BorderInnerSize;/*除去阴影部份和边框部份的外围大小*/540 privateSize p_ChildSize;/*子控件可用的大小*/541 privateCornerRadius g_BorderCornerRadius;/*边框倒角*/542 543 privateRect p_ShadowBorderOuterRect;/*所有外围区域*/544 privateRect p_ShadowBorderInnerRect;/*除去阴影部份的外围区域*/545 privateRect p_BorderInnerRect;/*除去阴影部份和边框部份的外围区域*/546 privateRect p_ChildRect;/*子控件可用的区域*/547 548 internalvoidMeasureData(549 Size constraint,550 doubleshadowwidth,551 doublelightarc,552 CornerRadius bordercornerradius,553 Thickness borderthickness,554 Thickness padding)555 {556 p_ShadowBorderOuterSize.Width=constraint.Width;557 p_ShadowBorderOuterSize.Height=constraint.Height;558 boolb=(shadowwidth>0&&lightarc>=0);559 g_BorderCornerRadius.TopLeft=bordercornerradius.TopLeft;560 g_BorderCornerRadius.TopRight=bordercornerradius.TopRight;561 g_BorderCornerRadius.BottomLeft=bordercornerradius.BottomLeft;562 g_BorderCornerRadius.BottomRight=bordercornerradius.BottomRight;563 if(b)564 {565 ResetDxDy(shadowwidth, lightarc);566 p_ShadowBorderInnerSize.Width=567 p_ShadowBorderOuterSize.Width-g_Dx;568 p_ShadowBorderInnerSize.Height=569 p_ShadowBorderOuterSize.Height-g_Dy;570 }571 else572 {573 g_ShadowLightPosition=MeasureShadowLightPosition.None;574 g_Dx=0;575 g_Dy=0;576 p_ShadowBorderInnerSize.Width=constraint.Width;577 p_ShadowBorderInnerSize.Height=constraint.Height;578 }579 p_BorderInnerSize.Width=580 p_ShadowBorderInnerSize.Width-581 borderthickness.Left-582 borderthickness.Right;583 p_BorderInnerSize.Height=584 p_ShadowBorderInnerSize.Height-585 borderthickness.Top-586 borderthickness.Bottom;587 p_ChildSize.Width=p_BorderInnerSize.Width-588 padding.Left-padding.Right;589 p_ChildSize.Height=p_BorderInnerSize.Height-590 padding.Top-padding.Bottom;591 }592 593 internalSize ChildSize594 {595 get{returnp_ChildSize; }596 }597 598 internalRect ChildRect599 {600 get{returnp_ChildRect; }601 }602 603 internalRect ShadowBorderOuterRect604 {605 get{returnp_ShadowBorderOuterRect; }606 }607 608 internalRect ShadowBorderInnerRect609 {610 get{returnp_ShadowBorderInnerRect; }611 }612 613 internalboolHasShadow614 {615 get{returng_ShadowLightPosition!=MeasureShadowLightPosition.None; }616 }617 618 internalvoidArrangeData(619 Size constraint,620 doubleshadowwidth,621 doublelightarc,622 CornerRadius bordercornerradius,623 Thickness borderthickness,624 Thickness padding)625 {626 MeasureData(constraint,627 shadowwidth, lightarc, bordercornerradius,628 borderthickness, padding);629 p_ShadowBorderOuterRect.Width=p_ShadowBorderOuterSize.Width;630 p_ShadowBorderOuterRect.Height=p_ShadowBorderOuterSize.Height;631 p_ShadowBorderInnerRect.Width=p_ShadowBorderInnerSize.Width;632 p_ShadowBorderInnerRect.Height=p_ShadowBorderInnerSize.Height;633 switch(g_ShadowLightPosition)634 {635 caseMeasureShadowLightPosition.None:636 caseMeasureShadowLightPosition.LeftTop:637 {638 p_ShadowBorderInnerRect.X=p_ShadowBorderOuterRect.X;639 p_ShadowBorderInnerRect.Y=p_ShadowBorderOuterRect.Y;640 break;641 }642 caseMeasureShadowLightPosition.RightTop:643 {644 p_ShadowBorderInnerRect.X=p_ShadowBorderOuterRect.X+g_Dx;645 p_ShadowBorderInnerRect.Y=p_ShadowBorderOuterRect.Y;646 break;647 }648 caseMeasureShadowLightPosition.RightBottom:649 {650 p_ShadowBorderInnerRect.X=p_ShadowBorderOuterRect.X+g_Dx;651 p_ShadowBorderInnerRect.Y=p_ShadowBorderOuterRect.Y+g_Dy;652 break;653 }654 caseMeasureShadowLightPosition.LeftBottom:655 {656 p_ShadowBorderInnerRect.X=p_ShadowBorderOuterRect.X;657 p_ShadowBorderInnerRect.Y=p_ShadowBorderOuterRect.Y+g_Dy;658 break;659 }660 }661 p_BorderInnerRect.Width=p_BorderInnerSize.Width;662 p_BorderInnerRect.Height=p_BorderInnerSize.Height;663 p_BorderInnerRect.X=p_ShadowBorderInnerRect.X+borderthickness.Left;664 p_BorderInnerRect.Y=p_ShadowBorderInnerRect.Y+borderthickness.Top;665 p_ChildRect.Width=p_ChildSize.Width;666 p_ChildRect.Height=p_ChildSize.Height;667 p_ChildRect.X=p_BorderInnerRect.X+padding.Left;668 p_ChildRect.Y=p_BorderInnerRect.Y+padding.Top;669 }670 671 internalboolHasBorder672 {673 get674 {675 return(p_ShadowBorderInnerSize.Width>676 p_BorderInnerSize.Width&&677 p_ShadowBorderInnerSize.Height>678 p_BorderInnerSize.Height);679 }680 }681 682 internalvoidSetBackgroundGeometry(StreamGeometryContext ctx)683 {684 SetBorderGeometry(ctx, p_BorderInnerRect);685 }686 687 internalvoidSetBorderGeometry(StreamGeometryContext ctx)688 {689 SetBorderGeometry(ctx, p_ShadowBorderInnerRect);690 SetBorderGeometry(ctx, p_BorderInnerRect);691 }692 693 internalvoidGetShadowGeometry(outGeometry x_shadow,outGeometry y_shadow)694 {695 Rect vx=newRect(696 p_ShadowBorderInnerRect.X,697 p_ShadowBorderInnerRect.Y,698 p_ShadowBorderInnerRect.Width,699 p_ShadowBorderInnerRect.Height);700 Point[] joinpoints=null;701 switch(g_ShadowLightPosition)702 {703 caseMeasureShadowLightPosition.LeftTop:704 {705 vx.X+=g_Dx;706 vx.Y+=g_Dy;707 joinpoints=GetBorderJoinPoints(p_ShadowBorderInnerRect,708 vx);709 break;710 }711 caseMeasureShadowLightPosition.RightTop:712 {713 vx.X-=g_Dx;714 vx.Y+=g_Dy;715 joinpoints=GetBorderJoinPoints(p_ShadowBorderInnerRect, vx);716 break;717 }718 caseMeasureShadowLightPosition.RightBottom:719 {720 vx.X-=g_Dx;721 vx.Y-=g_Dy;722 joinpoints=GetBorderJoinPoints(vx, p_ShadowBorderInnerRect);723 break;724 }725 caseMeasureShadowLightPosition.LeftBottom:726 {727 vx.X+=g_Dx;728 vx.Y-=g_Dy;729 joinpoints=GetBorderJoinPoints(vx, p_ShadowBorderInnerRect);730 break;731 }732 }733 StreamGeometry border1=newStreamGeometry();734 border1.FillRule=FillRule.EvenOdd;735 using(StreamGeometryContext context=border1.Open())736 {737 SetBorderGeometry(context, p_ShadowBorderInnerRect);738 }739 if(border1.CanFreeze)740 border1.Freeze();741 StreamGeometry border2=newStreamGeometry();742 border2.FillRule=FillRule.EvenOdd;743 using(StreamGeometryContext context=border2.Open())744 {745 SetBorderGeometry(context, vx);746 }747 if(border2.CanFreeze)748 border2.Freeze();749 PathGeometry p=Geometry.Combine(border1, border2, GeometryCombineMode.Union,750 null);751 if(!(g_LightArc==0||752 g_LightArc==90||753 g_LightArc==180||754 g_LightArc==270)755 &&joinpoints!=null)756 {757 StreamGeometry border3=newStreamGeometry();758 using(StreamGeometryContext context=border3.Open())759 {760 context.BeginFigure(joinpoints[0],true,true);761 context.LineTo(joinpoints[1],true,false);762 context.LineTo(joinpoints[2],true,false);763 context.LineTo(joinpoints[3],true,true);764 }765 if(border3.CanFreeze)766 border3.Freeze();767 p=Geometry.Combine(p, border3, GeometryCombineMode.Union,null);768 }769 p=Geometry.Combine(p, border1, GeometryCombineMode.Exclude,null);770 SplitGeometty(p,outx_shadow,outy_shadow);771 }772 internalvoidSetShadowBrush(773 doublelightarc, Color shadowcolor,outBrush x_brush,outBrush y_brush)774 {775 x_brush=null;776 y_brush=null;777 doublefgd=StandardHelpMethod.HjFgPoint;778 bytea=(byte)(255*fgd);779 Color startcolor=shadowcolor;780 Color fgdcolor=Color.FromArgb(781 a,shadowcolor.R,shadowcolor.G,shadowcolor.B);782 Color endcolor=Color.FromArgb(0,783 shadowcolor.R, shadowcolor.G, shadowcolor.B);784 Point x_startpoint=newPoint();785 Point x_endpoint=newPoint();786 Point y_startpoint=newPoint();787 Point y_endpoint=newPoint();788 MeasureShadowLightPosition pos=GetLightPosition(lightarc);789 switch(pos)790 {791 caseMeasureShadowLightPosition.LeftTop:792 {793 x_startpoint=newPoint();794 x_endpoint=newPoint(0,1);795 y_startpoint=newPoint();796 y_endpoint=newPoint(1,0);797 break;798 }799 caseMeasureShadowLightPosition.LeftBottom:800 {801 x_startpoint=newPoint(0,1);802 x_endpoint=newPoint(0,0);803 y_startpoint=newPoint(0,0);804 y_endpoint=newPoint(1,0);805 break;806 }807 caseMeasureShadowLightPosition.RightTop:808 {809 x_startpoint=newPoint();810 x_endpoint=newPoint(0,1);811 y_startpoint=newPoint(1,0);812 y_endpoint=newPoint(0,0);813 break;814 }815 caseMeasureShadowLightPosition.RightBottom:816 {817 x_startpoint=newPoint(0,1);818 x_endpoint=newPoint(0,0);819 y_startpoint=newPoint(1,0);820 y_endpoint=newPoint(0,0);821 break;822 }823 }824 GradientStopCollection stops=newGradientStopCollection();825 stops.Add(newGradientStop(startcolor,0));826 stops.Add(newGradientStop(fgdcolor, fgd));827 stops.Add(newGradientStop(endcolor,1));828 x_brush=newLinearGradientBrush(stops)829 {830 StartPoint=x_startpoint,831 EndPoint=x_endpoint832 };833 y_brush=newLinearGradientBrush(stops)834 {835 StartPoint=y_startpoint,836 EndPoint=y_endpoint837 };838 }839 840 privateMeasureShadowLightPosition GetLightPosition(doublelightarc)841 {842 doublearc=GetArc(lightarc);843 MeasureShadowLightPosition lp=MeasureShadowLightPosition.None;844 if(arc>=0&&arc<90)845 lp=MeasureShadowLightPosition.LeftTop;846 elseif(arc>=90&&arc<180)847 {848 lp=MeasureShadowLightPosition.RightTop;849 arc=180-arc;850 }851 elseif(arc>=180&&arc<270)852 {853 lp=MeasureShadowLightPosition.RightBottom;854 arc-=180;855 }856 elseif(arc>=270&&arc<360)857 lp=MeasureShadowLightPosition.LeftBottom;858 returnlp;859 }860 861 privatevoidSplitGeometty(PathGeometry shadow,outGeometry shadow_x,outGeometry shadow_y)862 {863 switch(g_ShadowLightPosition)864 {865 caseMeasureShadowLightPosition.RightBottom:866 {867 SplitGeometty_RightBottom(shadow,outshadow_x,outshadow_y);868 break;869 }870 caseMeasureShadowLightPosition.LeftTop:871 {872 SplitGeometty_LeftTop(shadow,outshadow_x,outshadow_y);873 break;874 }875 caseMeasureShadowLightPosition.LeftBottom:876 {877 SplitGeometty_LeftBottom(shadow,outshadow_x,outshadow_y);878 break;879 }880 caseMeasureShadowLightPosition.RightTop:881 {882 SplitGeometty_RightTop(shadow,outshadow_x,outshadow_y);883 break;884 }885 default:886 {887 shadow_x=null;888 shadow_y=null;889 break;890 }891 }892 }893 894 privatevoidSplitGeometty_LeftTop(PathGeometry shadow,outGeometry shadow_x,outGeometry shadow_y)895 {896 if(g_LightArc==0)897 {898 shadow_y=shadow;899 shadow_x=null;900 return;901 }902 doublearc=g_LightArc*(Math.PI/180);903 doubledy=p_ShadowBorderOuterRect.Width*(Math.Sin(arc)/Math.Cos(arc));904 Point xstartpoint=p_ShadowBorderOuterRect.BottomLeft+newVector(0,0-dy);905 StreamGeometry xg=newStreamGeometry();906 using(StreamGeometryContext context=xg.Open())907 {908 context.BeginFigure(xstartpoint,true,true);909 context.LineTo(p_ShadowBorderOuterRect.BottomLeft,true,false);910 context.LineTo(p_ShadowBorderOuterRect.BottomRight,true,false);911 }912 xg.Freeze();913 StreamGeometry yg=newStreamGeometry();914 using(StreamGeometryContext context=yg.Open())915 {916 context.BeginFigure(xstartpoint,true,true);917 if(!DoubleUtil.AreClose(xstartpoint,918 p_ShadowBorderOuterRect.TopLeft))919 {920 context.LineTo(p_ShadowBorderOuterRect.TopLeft,true,false);921 }922 context.LineTo(p_ShadowBorderOuterRect.TopRight,true,false);923 context.LineTo(p_ShadowBorderOuterRect.BottomRight,true,false);924 }925 yg.Freeze();926 shadow_x=Geometry.Combine(927 xg, shadow, GeometryCombineMode.Intersect,null);928 shadow_y=Geometry.Combine(929 yg, shadow, GeometryCombineMode.Intersect,null);930 }931 932 privatevoidSplitGeometty_RightBottom(PathGeometry shadow,outGeometry shadow_x,outGeometry shadow_y)933 {934 if(g_LightArc==180)935 {936 shadow_y=shadow;937 shadow_x=null;938 return;939 }940 doublearc=(g_LightArc-180)*(Math.PI/180);941 doubledy=(Math.Sin(arc)/Math.Cos(arc))*p_ShadowBorderOuterRect.Width;942 Point xstartpoint=p_ShadowBorderOuterRect.TopRight+newVector(0, dy);943 StreamGeometry xg=newStreamGeometry();944 using(StreamGeometryContext context=xg.Open())945 {946 context.BeginFigure(xstartpoint,true,true);947 context.LineTo(p_ShadowBorderOuterRect.TopRight,true,false);948 context.LineTo(p_ShadowBorderOuterRect.TopLeft,true,false);949 }950 xg.Freeze();951 StreamGeometry yg=newStreamGeometry();952 using(StreamGeometryContext context=yg.Open())953 {954 context.BeginFigure(xstartpoint,true,true);955 if(!DoubleUtil.AreClose(xstartpoint,956 p_ShadowBorderOuterRect.BottomRight))957 {958 context.LineTo(p_ShadowBorderOuterRect.BottomRight,true,false);959 }960 context.LineTo(p_ShadowBorderOuterRect.BottomLeft,true,false);961 context.LineTo(p_ShadowBorderOuterRect.TopLeft,true,false);962 }963 yg.Freeze();964 shadow_x=Geometry.Combine(965 xg, shadow, GeometryCombineMode.Intersect,null);966 shadow_y=Geometry.Combine(967 yg, shadow, GeometryCombineMode.Intersect,null);968 }969 970 privatevoidSplitGeometty_RightTop(PathGeometry shadow,outGeometry shadow_x,outGeometry shadow_y)971 {972 if(g_LightArc==90)973 {974 shadow_y=null;975 shadow_x=shadow;976 return;977 }978 doublearc=(180-g_LightArc)*(Math.PI/180);979 doubledy=p_ShadowBorderOuterRect.Width*(Math.Sin(arc)/Math.Cos(arc));980 Point xstartpoint=p_ShadowBorderOuterRect.BottomRight+newVector(0,0-dy);981 StreamGeometry xg=newStreamGeometry();982 using(StreamGeometryContext context=xg.Open())983 {984 context.BeginFigure(xstartpoint,true,true);985 context.LineTo(p_ShadowBorderOuterRect.BottomRight,true,false);986 context.LineTo(p_ShadowBorderOuterRect.BottomLeft,true,false);987 }988 xg.Freeze();989 StreamGeometry yg=newStreamGeometry();990 using(StreamGeometryContext context=yg.Open())991 {992 context.BeginFigure(xstartpoint,true,true);993 if(!DoubleUtil.AreClose(xstartpoint,994 p_ShadowBorderOuterRect.TopRight))995 {996 context.LineTo(p_ShadowBorderOuterRect.TopRight,true,false);997 }998 context.LineTo(p_ShadowBorderOuterRect.TopLeft,true,false);999 context.LineTo(p_ShadowBorderOuterRect.BottomLeft,true,false);1000 }1001 yg.Freeze();1002 shadow_x=Geometry.Combine(1003 xg, shadow, GeometryCombineMode.Intersect,null);1004 shadow_y=Geometry.Combine(1005 yg, shadow, GeometryCombineMode.Intersect,null);1006 }1007 1008 privatevoidSplitGeometty_LeftBottom(PathGeometry shadow,outGeometry shadow_x,outGeometry shadow_y)1009 {1010 if(g_LightArc==270)1011 {1012 shadow_y=null;1013 shadow_x=shadow;1014 return;1015 }1016 doublearc=(360-g_LightArc)*(Math.PI/180);1017 doubledy=(Math.Sin(arc)/Math.Cos(arc))*p_ShadowBorderOuterRect.Width;1018 Point xstartpoint=p_ShadowBorderOuterRect.TopLeft+newVector(0, dy);1019 StreamGeometry xg=newStreamGeometry();1020 using(StreamGeometryContext context=xg.Open())1021 {1022 context.BeginFigure(xstartpoint,true,true);1023 context.LineTo(p_ShadowBorderOuterRect.TopLeft,true,false);1024 context.LineTo(p_ShadowBorderOuterRect.TopRight,true,false);1025 }1026 xg.Freeze();1027 StreamGeometry yg=newStreamGeometry();1028 using(StreamGeometryContext context=yg.Open())1029 {1030 context.BeginFigure(xstartpoint,true,true);1031 if(!DoubleUtil.AreClose(xstartpoint,1032 p_ShadowBorderOuterRect.BottomLeft))1033 {1034 context.LineTo(p_ShadowBorderOuterRect.BottomLeft,true,false);1035 }1036 context.LineTo(p_ShadowBorderOuterRect.BottomRight,true,false);1037 context.LineTo(p_ShadowBorderOuterRect.TopRight,true,false);1038 }1039 yg.Freeze();1040 shadow_x=Geometry.Combine(1041 xg, shadow, GeometryCombineMode.Intersect,null);1042 shadow_y=Geometry.Combine(1043 yg, shadow, GeometryCombineMode.Intersect,null);1044 }1045 1046 privatedoubleGetArc(doublelarc)1047 {1048 doublearc=Math.Abs(larc);1049 while(arc>=360)1050 arc-=360;1051 returnarc;1052 }1053 1054 privatevoidResetDxDy(1055 doubleshadowwidth,1056 doublelightarc)1057 {1058 doublearc=GetArc(lightarc);1059 g_LightArc=arc;1060 if(arc>=0&&arc<90)1061 {1062 g_Dx=Math.Abs(shadowwidth*Math.Cos(arc*(Math.PI/180)));1063 if(DoubleUtil.IsZero(g_Dx))1064 g_Dx=0;1065 g_Dy=Math.Abs(shadowwidth*Math.Sin(arc*(Math.PI/180)));1066 if(DoubleUtil.IsZero(g_Dy))1067 g_Dy=0;1068 g_ShadowLightPosition=MeasureShadowLightPosition.LeftTop;1069 return;1070 }1071 if(arc>=90&&arc<180)1072 {1073 g_Dx=Math.Abs(shadowwidth*Math.Cos((180-arc)*(Math.PI/180)));1074 if(DoubleUtil.IsZero(g_Dx))1075 g_Dx=0;1076 g_Dy=Math.Abs(shadowwidth*Math.Sin((180-arc)*(Math.PI/180)));1077 if(DoubleUtil.IsZero(g_Dy))1078 g_Dy=0;1079 g_ShadowLightPosition=MeasureShadowLightPosition.RightTop;1080 return;1081 }1082 if(arc>=180&&arc<270)1083 {1084 g_Dx=Math.Abs(shadowwidth*Math.Cos((arc-180)*(Math.PI/180)));1085 if(DoubleUtil.IsZero(g_Dx))1086 g_Dx=0;1087 g_Dy=Math.Abs(shadowwidth*Math.Sin((arc-180)*(Math.PI/180)));1088 if(DoubleUtil.IsZero(g_Dy))1089 g_Dy=0;1090 g_ShadowLightPosition=MeasureShadowLightPosition.RightBottom;1091 return;1092 }1093 if(arc>=270&&arc<360)1094 {1095 g_Dx=Math.Abs(shadowwidth*Math.Cos((360-arc)*(Math.PI/180)));1096 if(DoubleUtil.IsZero(g_Dx))1097 g_Dx=0;1098 g_Dy=Math.Abs(shadowwidth*Math.Sin((360-arc)*(Math.PI/180)));1099 if(DoubleUtil.IsZero(g_Dy))1100 g_Dy=0;1101 g_ShadowLightPosition=MeasureShadowLightPosition.LeftBottom;1102 return;1103 }1104 }1105 1106 privatevoidAddJoinPoints(StreamGeometryContext ctx,Point[] points)1107 {1108 ctx.BeginFigure(points[0],true,false);1109 ctx.LineTo(points[1],true,false);1110 ctx.BeginFigure(points[2],true,false);1111 ctx.LineTo(points[3],true,false);1112 }1113 1114 privatevoidSetBorderGeometry(StreamGeometryContext ctx,Rect rect)1115 {1116 Point p1;1117 Point p2;1118 Point p3;1119 Point p4;1120 Point p5;1121 Point p6;1122 Point p7;1123 Point p8;1124 if(DoubleUtil.IsZero(g_BorderCornerRadius.TopLeft))1125 {1126 p1=p8=rect.Location;1127 }1128 else1129 {1130 p1=rect.Location+newVector(g_BorderCornerRadius.TopLeft,0);1131 p8=rect.Location+newVector(0, g_BorderCornerRadius.TopLeft);1132 }1133 if(DoubleUtil.IsZero(g_BorderCornerRadius.TopRight))1134 {1135 p2=p3=rect.TopRight;1136 }1137 else1138 {1139 p2=rect.TopRight+newVector(0-g_BorderCornerRadius.TopRight,0);1140 p3=rect.TopRight+newVector(0, g_BorderCornerRadius.TopRight);1141 }1142 if(DoubleUtil.IsZero(g_BorderCornerRadius.BottomRight))1143 {1144 p4=p5=rect.BottomRight;1145 }1146 else1147 {1148 p4=rect.BottomRight+newVector(0,0-g_BorderCornerRadius.BottomRight);1149 p5=rect.BottomRight+newVector(0-g_BorderCornerRadius.BottomRight,0);1150 }1151 if(DoubleUtil.IsZero(g_BorderCornerRadius.BottomLeft))1152 {1153 p6=p7=rect.BottomLeft;1154 }1155 else1156 {1157 p6=rect.BottomLeft+newVector(g_BorderCornerRadius.BottomLeft,0);1158 p7=rect.BottomLeft+newVector(0,0-g_BorderCornerRadius.BottomLeft);1159 }1160 if(p1.X>p2.X)1161 {1162 doublenum=(g_BorderCornerRadius.TopLeft/(g_BorderCornerRadius.TopLeft+g_BorderCornerRadius.TopRight))*rect.Width;1163 p1.X=num;1164 p2.X=num;1165 }1166 if(p3.Y>p4.Y)1167 {1168 doublenum2=(g_BorderCornerRadius.TopRight/(g_BorderCornerRadius.TopRight+g_BorderCornerRadius.BottomRight))*rect.Height;1169 p3.Y=num2;1170 p4.Y=num2;1171 }1172 if(p5.X180)1290 arc-=180;1291 doubleax=bj*Math.Cos((90-arc)*(Math.PI/180));1292 doubledx=bj-ax;1293 doubleay=bj*Math.Sin((90-arc)*(Math.PI/180));1294 doubledy=bj-ay;1295 returnnewVector(dx, dy);1296 }1297 1298 privateVector GetBorderJoinPointVector_RT_LB(doublebj)1299 {1300 doublearc;1301 if(g_LightArc>270)1302 {1303 arc=360-g_LightArc;1304 }1305 else1306 {1307 arc=360-(g_LightArc+180);1308 }1309 doubleax=bj*Math.Cos((90-arc)*(Math.PI/180));1310 doubledx=bj-ax;1311 doubleay=bj*Math.Sin((90-arc)*(Math.PI/180));1312 doubledy=bj-ay;1313 returnnewVector(dx, dy);1314 }1315 1316 }1317 1318 privateenumMeasureShadowLightPosition1319 {1320 None,1321 LeftTop,1322 RightTop,1323 RightBottom,1324 LeftBottom1325 }1326 #endregion1327 }1328 }1329

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值