How can I create a transparent System.Windows.Forms.Panel on top of two other already existing Panels then draw a line on my transparent panel?

拈花ヽ惹草 提交于 2019-12-24 06:39:00

问题


I'm writing a form in C# and have several panels. I need to draw a line between two of the panels. I've found online several ways to go about this, the most promising appears to be to create a third panel, make it transparent, place it on top of my original panels and draw the line here.

I'm not able to get the panel to be transparent, even if I set its BackColor and ForeColor properties to transparent (in code or in design view of VS).

Any ideas on how to make the panel itself transparent (or not Visible) but have the line I draw on it still visible on top of everything else?

Thanks in advance.


回答1:


No, it's transparent. See this by giving the form's BackgroundImage a value. You'll see it through the transparent panel. Of course, that's not the kind of transparency you want, you want stacking effects to work. There is no direct support for that.

If you want layers to work then don't use controls. Use the Paint event to draw. Now there's no problem, if you want transparency then just don't paint. Draw a line across an image simply by drawing the image first. This is also the rendering model of WPF.




回答2:


You can actually do this pretty easily as your own UserControl. Here's a code example:

Drawing on top of controls inside a panel (C# WinForms)

This is similar to what you were originally attempting to do, only instead of drawing a line on top of a transparent panel, this code creates an irregularly-shaped user control (which happens to be in the irregular shape of a line).



来源:https://stackoverflow.com/questions/4973847/how-can-i-create-a-transparent-system-windows-forms-panel-on-top-of-two-other-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!