Png圖片的透明部分穿透測試
阿新 • • 發佈:2018-09-22
自己 open 鼠標 tree ++ lee ctu lis tar 原文:Png圖片的透明部分穿透測試
private void Window_MouseMove(object sender, MouseEventArgs e)
{
NavBtnList.Clear(); Point mouseP = e.GetPosition(this); VisualTreeHelper.HitTest(this, new HitTestFilterCallback(HitFilter), new HitTestResultCallback(HitResult), newPointHitTestParameters(mouseP)); if (NavBtnList.Count > 0) { for (int k = 0; k < NavBtnList.Count; k++) { IconBtn element = NavBtnList[k] as IconBtn; DrawingVisual drawingVisual= new DrawingVisual(); using (DrawingContext context = drawingVisual.RenderOpen()) { VisualBrush brush = new VisualBrush(element) { Stretch = Stretch.None }; context.DrawRectangle(brush, null,new Rect(0, 0, element.ActualWidth, element.ActualHeight)); context.Close(); } //dpi可以自己設定 // 獲取dpi方法:PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32); bitmap.Render(drawingVisual); Point p = this.TranslatePoint(mouseP, element); //全局坐標轉為局部坐標point //獲取鼠標下像素顏色值 var c = getImagePointColor((bitmap as BitmapSource), (int)p.X, (int)p.Y); if (c != Convert16ToColor("#00000000")) { if (element.IsEnabled) { } break; } bitmap.Clear(); }
}
1 //鼠標按下獲得所有IconBtnFrame 存儲在NavBtnList裏 2 List<IconBtn> NavBtnList = new List<IconBtn>(); 3 4 public HitTestFilterBehavior HitFilter(DependencyObject dObject) 5 { 6 if (dObject is IconBtn) 7 { 8 NavBtnList.Add(dObject as IconBtn); 9 } 10 return HitTestFilterBehavior.Continue; 11 } 12 13 public HitTestResultBehavior HitResult(HitTestResult result) 14 { 15 return HitTestResultBehavior.Continue; 16 }
Png圖片的透明部分穿透測試