2

I'm working on ImageButton, in which I paint every state(i've got several images for each state) of this button (like mouseOver, mouseDown etc.).

I've made control transparent using this code:

public ImageButton()
{
  InitializeComponent();

  this.SetStyle(ControlStyles.Opaque, true);
  this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
}

protected override CreateParams CreateParams
{
  get
  {
      CreateParams parms = base.CreateParams;
      parms.ExStyle |= 0x20;  
      return parms;
  }
}

But there's a problem, after few switches of state, corners becoming sharp and ugly, to solve this problem I need to clear background, but if my control is transparent then it's impossible.

I've tried this solution: Clearing the graphics of a transparent panel C# but it's slow and makes control flickering.

Do you have any ideas how to clear this background and keep transparency of control?

Community
  • 1
  • 1
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67
  • What kind of Transparency do you need? Just the parent background to show through? Or do you want transparency for other controls and/or other windows? – Scott Rippey Dec 09 '11 at 23:43

1 Answers1

1

Ok, I've solved this problem. I've worked around it by setting control as not transparent and I draw canvas which is under my control, as background of my ImageButton.

Solution (in Paint event):

//gets position of button and transforms it to point on whole screen
//(because in next step we'll get screenshot of whole window [with borders etc])
Point btnpos = this.Parent.PointToScreen(new Point(Location.X, Location.Y));

//now our point will be relative to the edges of form  
//[including borders, which we'll have on our bitmap]
if (this.Parent is Form)
{
      btnpos.X -= this.Parent.Left;
      btnpos.Y -= this.Parent.Top;
}
else
{
    btnpos.X = this.Left;
    btnpos.Y = this.Top;
}

//gets screenshot of whole form
Bitmap b = new Bitmap(this.Parent.Width, this.Parent.Height);
this.Parent.DrawToBitmap(b, new Rectangle(new Point(0, 0), this.Parent.Size));

//draws background (which simulates transparency)
e.Graphics.DrawImage(b,
                new Rectangle(new Point(0, 0), this.Size),
                new Rectangle(btnpos, this.Size),
                GraphicsUnit.Pixel);

//do whatever you want to draw your stuff

PS. It doesn't work in designtime.

Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67
  • I've noticed another problem. When button is on clear form (not for example on PictureBox) then bitmap contains him drawn and it doesn't work properly. How to repaint background first then my control (to have bitmap without my control)? – Wojciech Kulik Dec 10 '11 at 14:33