透明的用户控件背景清晰 [英] Clear background of transparent user control

查看:77
本文介绍了透明的用户控件背景清晰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ImageButton,在其中我绘制该按钮的每个状态(每个状态(每个状态都有几张图像))(例如mouseOver,mouseDown等).

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.

我已经尝试过以下解决方案:清除透明面板C# 但是它很慢,并且会使控件闪烁.

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?

推荐答案

好的,我已经解决了这个问题. 我通过将控件设置为不透明来解决此问题,并绘制了在我控制下的画布作为ImageButton的背景.

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.

解决方案(在Paint事件中):

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.在设计时无效.

PS. It doesn't work in designtime.

这篇关于透明的用户控件背景清晰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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