Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to make DiagramView background transparent? (Read 945 times)
koichi_satoh
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 25
Joined: Nov 30th, 2023
How to make DiagramView background transparent?
Nov 30th, 2023 at 4:23am
Print Post  
I would like to make the background of DiagramView transparent. Is it possible to achieve this using properties?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3377
Joined: Oct 19th, 2005
Re: How to make DiagramView background transparent?
Reply #1 - Nov 30th, 2023 at 6:42am
Print Post  
Hi,

If you need overlapped background controls to see through, you will have to override diagram view's drawing -

https://stackoverflow.com/questions/592538/how-to-create-a-transparent-control-w...
http://componentfactory.blogspot.com/2005/06/net2-transparent-controls.html

Code
Select All
class DiagramViewEx : DiagramView
{
    public DiagramViewEx()
    {
        Diagram.BackBrush = new SolidBrush(Color.Transparent);
    }

    void PaintParentBackground(PaintEventArgs e)
    {
        if (Parent != null)
        {
            var rect = new Rectangle(Left, Top, Width, Height);

            e.Graphics.TranslateTransform(-rect.X, -rect.Y);

            try
            {
                using (var pea = new PaintEventArgs(e.Graphics, rect))
                {
                    pea.Graphics.SetClip(rect);
                    InvokePaintBackground(Parent, pea);
                    InvokePaint(Parent, pea);
                }
            }
            finally
            {
                e.Graphics.TranslateTransform(rect.X, rect.Y);
            }

            var behind = new Bitmap(Parent.Width, Parent.Height);
            foreach (Control c in Parent.Controls)
                if (c.Bounds.IntersectsWith(this.Bounds) && c != this)
                    c.DrawToBitmap(behind, c.Bounds);
            e.Graphics.DrawImage(behind, -Left, -Top);
            behind.Dispose();
        }
        else
        {
            e.Graphics.FillRectangle(
                SystemBrushes.Control, ClientRectangle);
        }
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        PaintParentBackground(pe);
        base.OnPaint(pe);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if (Diagram.Interaction != null)
        {
            Diagram.Invalidate();
            Refresh();
        }
    }
} 





Diagram's back-buffer optimization during interaction does not work well with transparent background, hence the refresh from OnMouseMove override too.

Regards,
Slavcho
Mindfusion
« Last Edit: Nov 30th, 2023 at 9:01am by Slavcho »  
Back to top
 
IP Logged
 
koichi_satoh
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 25
Joined: Nov 30th, 2023
Re: How to make DiagramView background transparent?
Reply #2 - Dec 1st, 2023 at 12:28am
Print Post  
I was able to do it using the method you taught me.
thank you very much.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint