4

それで、しばらく運が悪かったので、私は最終的に、透明なコントロールを互いに表示させる方法について尋ねることにしました。

ここに画像の説明を入力してください

写真にあるように、私は2つの透明な画像ボックスを持っていますが、それらは背景を非常によく示していますが、画像で見ることができるように選択した画像ボックスに関しては、フォームの背景画像のみをレンダリングし、下の他の画像ボックスはレンダリングしませんそれ。適切なレンダリングがないためにwinformsに一般的な状況があることは知っていますが、問題は次のとおりです。

このレンダリングの不具合を回避する方法はありますか?透明なコントロールを相互にレンダリングさせる方法はありますか?

これが答えです:C#WinFormsを使用した透明な画像

4

1 に答える 1

3

コントロールの透明度は、その親コン​​トロールによって異なります。ただし、親画像の画像ボックスの代わりにカスタムコンテナコントロールを使用できます。このコードは使いやすいかもしれません。

    using System;
using System.Windows.Forms;
using System.Drawing;

public class TransparentControl : Control
{
    private readonly Timer refresher;
    private Image _image;

    public TransparentControl()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.Transparent;
        refresher = new Timer();
        refresher.Tick += TimerOnTick;
        refresher.Interval = 50;
        refresher.Enabled = true;
        refresher.Start();
    }

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

    protected override void OnMove(EventArgs e)
    {
        RecreateHandle();
    }


    protected override void OnPaint(PaintEventArgs e)
    {
        if (_image != null)
        {
            e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
       //Do not paint background
    }

    //Hack
    public void Redraw()
    {
        RecreateHandle();
    }

    private void TimerOnTick(object source, EventArgs e)
    {
        RecreateHandle();
        refresher.Stop();
    }

    public Image Image
    {
        get
        {
            return _image;
        }
        set
        {
            _image = value;
            RecreateHandle();
        }
    }
}
于 2013-02-16T19:23:09.733 に答える