I need an irregularly shaped button in WPF. I am doing it in this way using XAML:
<Button Name="toggleButton" Click="toggleButton_Click" Canvas.Left="177" Canvas.Top="0"> <Button.Template> <ControlTemplate> <Image Source="ball.png" /> </ControlTemplate> </Button.Template> </Button>
我的ball.png图像是与它周围的透明区域,一个球一个PNG图像。该按钮可显示正常,但Click事件处理程序,即使当我短声上的图像的透明部分执行。
My ball.png image is a PNG image with a ball with transparent area around it. The button displays correctly, but Click event handler is executed even when I clik on the transparent part of the image.
有没有办法使用透明的PNG创建不规则的按钮?
Is there any way to create irregular buttons using transparent PNGs?
谢谢,米哈尔
您可以创建一个类继承自图像和覆盖HitTestCore因此,它不响应命中测试在图像的透明部分,然后用一个简单的形象该类在你的模板。
You can create a class that inherits from Image and overrides HitTestCore so that it does not respond to hit testing over the transparent parts of an image, and then use that class in your template instead of a plain Image.
下面是一个例子,虽然code键检查透明像素不是很鲁棒,因为它使关于图像源和像素格式一些假设。如果你已经有code,以检查透明像素,那么你应该插在代替。
Here is an example, although the code to check for transparent pixels isn't very robust since it makes some assumptions about the image source and pixel format. If you already have code to check for transparent pixels then you should plug that in instead.
public class TransparentImage : Image { protected override HitTestResult HitTestCore( PointHitTestParameters hitTestParameters) { // Get value of current pixel var source = (BitmapSource)Source; var x = (int)(hitTestParameters.HitPoint.X / ActualWidth * source.PixelWidth); var y = (int)(hitTestParameters.HitPoint.Y / ActualHeight * source.PixelHeight); var pixels = new byte[4]; source.CopyPixels(new Int32Rect(x, y, 1, 1), pixels, 4, 0); // Check alpha channel if (pixels[3] < 10) { return new PointHitTestResult(this, hitTestParameters.HitPoint); } else { return null; } } protected override GeometryHitTestResult HitTestCore( GeometryHitTestParameters hitTestParameters) { // Do something similar here, possibly checking every pixel within // the hitTestParameters.HitGeometry.Bounds rectangle return base.HitTestCore(hitTestParameters); } }
相关推荐
金翔龙(2023最新金翔龙百科介绍)
在 cocos2d 引擎中实现二维码阅读器阅读器、引擎、二维码、cocos2
Android和Facebook的联系人选择器发卡行联系人、选择器、发卡行、
&QUOT; Android的库更新“月蚀的任务随机坚持我月食开始月食、任务
如何保护web服务,这样只有我的Android应用程序可以使用我的web
幼师的工资一般是多少 薪资待遇好吗_ui设计师工资一般多少 薪资待
四年级上册语文教学计划_人教版四年级上册语文教学计划
身份证到期换证流程是什么_身份证到期换证流程 身份证到期换证方
有点高血压怎么办啊?_高血压的偏方
仰卧起坐为什么头晕啊?_仰卧起坐后头晕怎么办?
番茄酱虾的做法_西红柿炒虾的做法,这些方法你会了吗?
拉伸运动有那些呢_跳绳减肥拉伸运动更重要 防止小腿变粗
马拉糕的做法大全_麻婆豆腐的做法大全及菜品功效
石榴健胃片(大得力)的说明书_感冒软胶囊(大深林)的说明书
怀孕后能天天喝酸奶吗?_孕妇能天天喝酸奶好吗
呼吸急促是什么问题呢?_小儿呼吸急促是怎么回事?
看准再摸!舒服有益健康_男性常常搓搓这些部位竟有益健康!
睡觉起来胳膊酸痛怎么回事_经常出现胳膊酸痛怎么回事
基因睾酮是壮阳药吗_基因睾酮是真的吗
高血脂应该怎么检查_高血脂应该如何检查?
最新文章
柳叶刀·数字医疗(期刊)
异位性皮炎(2023最新异位性皮炎百科介绍)
百度医生(百度公司打造的全国性医患双选平台)
写你太难(写你太难)
ML15Cr(ML15Cr)
条纹双线鳚(条纹双线鳚)
翻天红(翻天红)
部分索引得到部分位置返回0部分、索引、位置
制作老鼠笼方法 老鼠笼制作方法如下_灯笼制作的方法 灯笼如何制作
南瓜葱花饼的做法_南瓜发面葱花饼的做法
新生儿脊椎问题_脊椎结核的偏方
肥胖症的危害有哪些方面_肥胖症的危害是什么
夫妻吵架啦!四招就和好_四招辨别纯鲜牛奶
男人为何喜欢征服别人的女人_男人出轨喜欢找哪样的女人做情人?
珍珠鲍鱼的家常做法_冷冻鲍鱼的家常做法
巩膜炎吃什么好?巩膜炎饮食_筋膜炎吃什么药治疗效果好
心脏急救常识有哪些?_家庭心脏病急救常识有哪些
我有一个梦想的演讲稿600字8篇(2023年小学生我有一个梦想演讲稿8
银行面试简单自我介绍范文8篇(银行面试自我介绍范文最新3篇)
教师入职发言稿(教师入职发言稿1000字)