我知道您的问题是关于C#的,但由于VB.NET的相似性和易于转换,我将添加一个完整的VB版本,同时允许在移动控件时更新其背景。您已经有了答案,但这是为那些通过搜索引擎找到此帖子并想要VB版本或者只是需要在C#中使用它的人提供一个完整可转换的示例。
创建一个新的自定义控件类,并将以下内容粘贴到其中...覆盖默认的类内容:
自定义控件类:
Public Class TransparentPictureBox
Private WithEvents refresher As Timer
Private _image As Image = Nothing
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
refresher = New Timer()
'refresher.Tick += New EventHandler(AddressOf Me.TimerOnTick)
refresher.Interval = 50
refresher.Start()
End Sub
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20
Return cp
End Get
End Property
Protected Overrides Sub OnMove(ByVal e As EventArgs)
MyBase.OnMove(e)
MyBase.RecreateHandle()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
'Add your custom paint code here
If _image IsNot Nothing Then
e.Graphics.DrawImage(_image, CInt(Width / 2) - CInt(_image.Width / 2), CInt(Height / 2) - CInt(_image.Height / 2))
End If
End Sub
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
' Paint background with underlying graphics from other controls
MyBase.OnPaintBackground(e)
Dim g As Graphics = e.Graphics
If Parent IsNot Nothing Then
' Take each control in turn
Dim index As Integer = Parent.Controls.GetChildIndex(Me)
For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
Dim c As Control = Parent.Controls(i)
' Check it's visible and overlaps this control
If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible Then
' Load appearance of underlying control and redraw it on this background
Dim bmp As New Bitmap(c.Width, c.Height, g)
c.DrawToBitmap(bmp, c.ClientRectangle)
g.TranslateTransform(c.Left - Left, c.Top - Top)
g.DrawImageUnscaled(bmp, Point.Empty)
g.TranslateTransform(Left - c.Left, Top - c.Top)
bmp.Dispose()
End If
Next
End If
End Sub
Public Property Image() As Image
Get
Return _image
End Get
Set(value As Image)
_image = value
MyBase.RecreateHandle()
End Set
End Property
Private Sub refresher_Tick(sender As Object, e As System.EventArgs) Handles refresher.Tick
MyBase.RecreateHandle()
refresher.Stop()
End Sub
End Class
保存类后,清理项目并重新构建。新控件应该出现为新的工具项。找到它,并将其拖到您的表单中。
我曾经遇到过这个控件的问题... 当我尝试加载一个动画的 "Loading" .gif 图像时发生。
图像不会动画,而且当你隐藏控件,然后再次显示它时,还会有显示问题。
解决这些问题,你就会有一个完美的自定义控件类。 :)
编辑:
我不知道以下内容是否适用于 C# 的 IDE,但这是我尝试转换的结果:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class TransparentPictureBox
{
private Timer withEventsField_refresher;
private Timer refresher {
get { return withEventsField_refresher; }
set {
if (withEventsField_refresher != null) {
withEventsField_refresher.Tick -= refresher_Tick;
}
withEventsField_refresher = value;
if (withEventsField_refresher != null) {
withEventsField_refresher.Tick += refresher_Tick;
}
}
}
private Image _image = null;
public TransparentPictureBox()
{
InitializeComponent();
refresher = new Timer();
refresher.Interval = 50;
refresher.Start();
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | 0x20;
return cp;
}
}
protected override void OnMove(EventArgs e)
{
base.OnMove(e);
base.RecreateHandle();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
if (_image != null) {
e.Graphics.DrawImage(_image, Convert.ToInt32(Width / 2) - Convert.ToInt32(_image.Width / 2), Convert.ToInt32(Height / 2) - Convert.ToInt32(_image.Height / 2));
}
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;
if (Parent != null) {
int index = Parent.Controls.GetChildIndex(this);
for (int i = Parent.Controls.Count - 1; i >= index + 1; i += -1) {
Control c = Parent.Controls(i);
if (c.Bounds.IntersectsWith(Bounds) && c.Visible) {
Bitmap bmp = new Bitmap(c.Width, c.Height, g);
c.DrawToBitmap(bmp, c.ClientRectangle);
g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
bmp.Dispose();
}
}
}
}
public Image Image {
get { return _image; }
set {
_image = value;
base.RecreateHandle();
}
}
private void refresher_Tick(object sender, System.EventArgs e)
{
base.RecreateHandle();
refresher.Stop();
}
}
试一下,自己看看吧 :P
ps:我不是大师,所以预计在C#和VB.NET版本中会出现各种错误。lol