Transparent and Semi Transparent panels?

M

mrabie

Hi,

I was wondering if someone can help me in finding a way to create a
semi-transparent or transparent panel in netcf? For example if i have
a background image on my main form and i want to add a panel with a
button over my form, so the panel would be semi -transparent showing
the form's background image?

Thanks
 
C

Christian Resma Helle

What you can do is to draw the part of the form's background image on the
control to give a so called "transparent" effect. I think you can draw on
the Panel control in .NETCF 3.5. Otherwise you'll have to can you're own
custom Panel control by creating a class that inherits from
ScrollableControl.
 
C

Christian Resma Helle

Otherwise you'll have to create you're own
custom Panel control by creating a class that inherits from
ScrollableControl...

Sorry for the typo
 
M

mrabie

Otherwise you'll have to create you're own
custom Panel control by creating a class that inherits from
ScrollableControl...

Sorry for the typo

--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com





- Show quoted text -

Hi
thanks for the info, what if i want to create my own custom class,
where do i start looking to know how can i override the OnPaint class
to paint with a semi or transparent color?
 
C

Christian Resma Helle

I don't really know of any article on the net that describes. but I took the
time to write some small code for a TransparentPanel control in .NETCF 3.5

What I did was to create an interface called IFormBackground that has a
property exposing the BackgroundImage of a form. I then created a custom
control that inherits from Panel, where I overridden the OnPaintBackground
of this control. In my OnPaintBackground I get the Parent control as
IFormBackground, if I get a NULL back then it means that the parent has no
background image coz it doesn't implement IFormBackground. Once I get my
IFormBackground instance, I draw part of the background image that my
control is on top of.

This would give you a so called transparent control effect. Here's the code
in C#

------------------
CODE START
------------------

namespace TransparentPanelSample
{
interface IFormBackground
{
Image BackgroundImage { get; }
}

class TransparentPanel : Panel
{
protected override void OnPaintBackground(PaintEventArgs e)
{
IFormBackground form = Parent as IFormBackground;
if (form == null) {
base.OnPaintBackground(e);
return;
}

e.Graphics.DrawImage(
form.BackgroundImage,
0,
0,
Bounds,
GraphicsUnit.Pixel);
}
}

class MainForm : Form, IFormBackground
{
Bitmap bmp;
TransparentPanel panel;
Button button;

public MainForm()
{
bmp = new Bitmap(
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"TransparentPanelSample.background.jpg"));

panel = new TransparentPanel();
panel.Bounds = new Rectangle(20, 20, 200, 280);
panel.Visible = true;
Controls.Add(panel);

button = new Button();
button.Text = "Button1";
button.Bounds = new Rectangle(50, 50, 70, 30);
button.Visible = true;
panel.Controls.Add(button);
}

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}

public Image BackgroundImage
{
get { return bmp; }
}
}
}

---------------
CODE END
---------------

I hope this helps...

--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com


Otherwise you'll have to create you're own
custom Panel control by creating a class that inherits from
ScrollableControl...

Sorry for the typo

--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com

message



- Show quoted text -

Hi
thanks for the info, what if i want to create my own custom class,
where do i start looking to know how can i override the OnPaint class
to paint with a semi or transparent color?
 
M

mrabie

I don't really know of any article on the net that describes. but I took the
time to write some small code for a TransparentPanel control in .NETCF 3.5

What I did was to create an interface called IFormBackground that has a
property exposing the BackgroundImage of a form. I then created a custom
control that inherits from Panel, where I overridden the OnPaintBackground
of this control. In my OnPaintBackground I get the Parent control as
IFormBackground, if I get a NULL back then it means that the parent has no
background image coz it doesn't implement IFormBackground. Once I get my
IFormBackground instance, I draw part of the background image that my
control is on top of.

This would give you a so called transparent control effect. Here's the code
in C#

------------------
CODE START
------------------

namespace TransparentPanelSample
{
  interface IFormBackground
  {
    Image BackgroundImage { get; }
  }

  class TransparentPanel : Panel
  {
    protected override void OnPaintBackground(PaintEventArgs e)
    {
      IFormBackground form = Parent as IFormBackground;
      if (form == null) {
        base.OnPaintBackground(e);
        return;
      }

      e.Graphics.DrawImage(
        form.BackgroundImage,
        0,
        0,
        Bounds,
        GraphicsUnit.Pixel);
    }
  }

  class MainForm : Form, IFormBackground
  {
    Bitmap bmp;
    TransparentPanel panel;
    Button button;

    public MainForm()
    {
      bmp = new Bitmap(
        Assembly.GetExecutingAssembly().GetManifestResourceStream(
        "TransparentPanelSample.background.jpg"));

      panel = new TransparentPanel();
      panel.Bounds = new Rectangle(20, 20, 200, 280);
      panel.Visible = true;
      Controls.Add(panel);

      button = new Button();
      button.Text = "Button1";
      button.Bounds = new Rectangle(50, 50, 70, 30);
      button.Visible = true;
      panel.Controls.Add(button);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      e.Graphics.DrawImage(bmp, 0, 0);
    }

    public Image BackgroundImage
    {
      get { return bmp; }
    }
  }

}

---------------
CODE END
---------------

I hope this helps...

--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com







Hi
thanks for the info, what if i want to create my own custom class,
where do i start looking to know how can i override the OnPaint class
to paint with a semi or transparent color?- Hide quoted text -

- Show quoted text -

Hey Christian

thank you so much for the code sample it was really helpful i really
appreciate it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top