private void Window_MouseMove(object sender, MouseEventArgs e)
{
NavBtnList.Clear();
Point mouseP = e.GetPosition(this);
VisualTreeHelper.HitTest(this, new HitTestFilterCallback(HitFilter),
new HitTestResultCallback(HitResult), new PointHitTestParameters(mouseP));
if (NavBtnList.Count > )
{
for (int k = ; 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(, , element.ActualWidth, element.ActualHeight));
context.Close();
}
//dpi可以自己设定
// 获取dpi方法:PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.ActualWidth,
(int)element.ActualHeight, , , 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();
}
}
//鼠标按下获得所有IconBtnFrame 存储在NavBtnList里
List<IconBtn> NavBtnList = new List<IconBtn>();
public HitTestFilterBehavior HitFilter(DependencyObject dObject)
{
if (dObject is IconBtn)
{
NavBtnList.Add(dObject as IconBtn);
}
return HitTestFilterBehavior.Continue;
}
public HitTestResultBehavior HitResult(HitTestResult result)
{
return HitTestResultBehavior.Continue;
}