WPF 光标位于部分透明图像上
我有一个包含透明区域的 png,并将其设置为图像标签。
当光标位于图像的不透明部分上方时,如何将光标设置为手?
谢谢
I have a png that contains transparent regions and I set it to the image tag.
How am I able to set the cursor to a hand when it is over an opaque part of the image?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您需要查看位图本身。 WPF 的点击测试机制认为使用“透明”画笔绘制的任何像素即使不可见,仍然可单击。这通常是一件好事,但会妨碍您尝试做的事情。由于 .png 使用透明画笔进行绘制,因此在进行命中测试时,整个 .png 被视为已绘制。
您需要在
MouseMove
事件处理程序中执行以下操作:HitTestResult
,检查它是否是Image
,如果是,则检查鼠标下方是否有透明像素Cursor
值要确定鼠标是否位于图像的透明像素上:
e.GetPosition( image)
)To do this you will need to look at the bitmap itself. WPF's hit testing mechanism considers any pixel painted ith a "transparent" brush to still be clickable even though invisible. This is normally a good thing but gets in the way of what you're trying to do. Because a .png paints with the transparent brush, the entire .png is considerd as painted when doing hit testing.
What you need to do in your
MouseMove
event handler is:HitTestResult
you get back, check to see if it is anImage
and if so, whether a transparent pixel is under the mouseCursor
value based on what the mouse is overTo determine whether a the mouse is over a transparent pixel of an image:
e.GetPosition(image)
)BitmapSource.CopyPixels
to copy a 1-pixel rectangle into an array (ie. only the single pixel the mouse is over)