C#中的不规则形状控件 [英] Irregular shaped controls in C#

查看:236
本文介绍了C#中的不规则形状控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试创建一个用户控件,您可以从包含风琴绘制的图片中选择任何风琴。我已将它们逐个拆分,将它们放在旁边。我使用图片框将它们紧密放置,但每个区域与其他区域重叠,而且空白区域也会产生问题。



那么如何只用可点击的图片就可以画出每张图片。



图片是不透明的,在视觉上我没有任何问题,问题是有用的。



谢谢你好



编辑:提问。



[ ^ ] 以下是我正在处理的问题 [ ^ ]



如图所示,有8个不同的部分(假设右侧和左侧合并)。正如你所看到的,它们彼此接近,其中一些甚至适合其他人之间的空白区域。



我的目标是,例如,如果我点击了Pectorals(图中的红色区域),它将变为彩色版本,并会运行一堆其他代码。



问题是,默认情况下,当我们使用PictureBox添加任何图片时,它将从该边界开始围绕该图片创建一个矩形。因此,如果我将两张图片(如图所示)放在一起,那么一个空区域会阻止我点击另一张图片。



它也会引发错误的Object的ClickEvent,因为这个问题。



我正在尝试设置提升事件区域,我假设我们将图形区域称为图像存在的位置。我可以用一个循环来收集像素的位置,该循环确定该图片的哪个坐标具有颜色(意味着它是图片的一部分,我想要点击的区域)但我不能用该数据限制该区域。 />


我想要实现的一个例子:https://www.youtube.com/watch?v = K_JzL4kzCoE



使用哪个控件来解决自定义控件或其他控件的重写形式的问题并不重要。

Hello, I'm trying to create a user-control that you can select any organ from picture(s) that contains organ draws. I already split them into one by one to place them next to next each other. I use picturebox to place them closely but each ones areas overlapping with others and also empty areas causes problems.

So How can i draw each picture with only clickable on picture boundrys.

Pictures are opaque and visually I don't have any problem, Problem is functional.

Thank you

To make question clear.

[^]Here is what I'm dealing with[^]

As you can see in the picture, there are 8 different parts (assuming the right and left sides are combined). As you can see, they are close to each other, and some of them are even fit in the empty space between others.

My goal is, for example, if I clicked on Pectorals (red zone in the figure), it will change to the colored version of it, and a bunch of other code will run.

The problem is, by default when we add any Picture with a PictureBox, it will create a rectangle around that picture starting from the boundaries of it. So if I place two pictures (as in the figure) close together, one's empty zone prevents me from clicking the other's.

Also it raises the wrong Object's ClickEvent because of this problem.

I'm trying to set "Raise Event Region" that I assume we called it Graphic Region just where the picture exists. I can collect positions of pixels with a loop which determines which coordinates of that picture has "Color" (meaning it is part of the picture, the area that I want clickable) but I can't limit that area with that data.

An example of what I'm trying to achieve: https://www.youtube.com/watch?v=K_JzL4kzCoE

It doesn't matter which control will be used for solve the issue wheter custom control or another control's rewritten form.

推荐答案

构造PictureBox控制具有可点击的不规则形状,并且不会遮挡其他控件背后:



1.您需要清理位图(最好的是一个带有透明度的24位.png文件,其中形状轮廓周围有一个清晰的区域。



2.然后你可以从这个位图创建一个GraphicPath;这里有一个代码示例,说明如何在CP上执行此操作:[ ^ ]。



3.使用上面的链接代码,然后设置PictureBox的Region基于图形路径,如下所示:
To construct a PictureBox Control with an irregular shape that is clickable and that does not obscure other Controls "behind" it:

1. you need to have a cleaned-up bitmap (best is a 24 bit .png file with transparency) where there is a clear area around the outline of the shape.

2. you can then create a GraphicPath from this bitmap; there's an example of code showing how to do that here on CP: [^].

3. using the linked to code above, you then set the Region of the PictureBox based on the graphics path, like this:
GraphicsPath g1 = CalculateControlGraphicsPath(pbLung.Image as Bitmap);
GraphicsPath g2 = CalculateControlGraphicsPath(pbClavicle.Image as Bitmap);

pbLung.Region = new Region(g1);
pbClavicle.Region = new Region(g2);

但是,如果你正在谈论生成形状遵循的形状的用户控件在位图中内容区域的可见轮廓中,除非要在位图中定义形状的区域在轮廓和围绕它的区域之间有明显的区别,否则您将难以获得高质量的结果。



在大多数情况下,从位图创建控件/表格形状的视觉效果并不令人满意。



最佳结果将是在没有曲线,圆弧等定义所需形状时发生

However, if you are talking about producing user-controls whose shapes follow the visible outline of an area of content in a bitmap, you are going to struggle to get quality results unless the area you want to define the shape in the bitmap has a very clear distinction between the outline and the area that surrounds it.

In most cases the visual results of creating Control/Form shapes from bitmaps are not satisfying.

Best results will occur when the shape required is defined without curves, arcs, etc.


来自这里 [ ^ ]可能会对您有所帮助。
Answers from here[^] might help you.


这篇关于C#中的不规则形状控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆