using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ryControls { /// /// 透明图片控件 /// [ToolboxBitmap(typeof(ZhLoading))] public partial class ZhLoading : Control { private bool _isTransparent = true;//是否透明 /// /// 是否使用透明,默认为True /// [Category("XX控件"), Description("是否使用透明,默认为True")] public bool IsTransparent { get { return _isTransparent; } set { _isTransparent = value; } } private int _alpha = 125;//设置透明度 /// /// 设置透明度 /// [Category("XX控件"), Description("设置透明度")] public int Alpha { get { return _alpha; } set { _alpha = value; } } private Control _byControl; /// /// 要绑定的控件 /// [Category("XX控件"), Description("要绑定的控件")] public Control ByControl { get { return _byControl; } set { _byControl = value; if (_byControl != null) { _byControl.Controls.Add(this); } } } /// /// 透明图片控件 /// public ZhLoading() : this(125, false,null) { InitializeComponent(); } /// /// 图片 /// public Image LoadingImage { get { return pic.Image; } set { pic.Image = value;pic.Visible = value != null; } } PictureBox pic = new PictureBox(); /// /// 初始化加载控件 /// /// 透明度 /// 是否显示加载图片 /// 图片 public ZhLoading(int Alpha, bool IsShowLoadingImage, Image img) { SetStyle(ControlStyles.Opaque, true);//设置背景透明 base.CreateControl(); _alpha = Alpha; pic.BackColor = Color.Transparent; pic.Image = img; pic.Size = new Size(48, 48); pic.SizeMode = PictureBoxSizeMode.AutoSize; pic.Location = new Point(this.Location.X + (this.Width - pic.Width) / 2, this.Location.Y + (this.Height - pic.Height) / 2);//居中 pic.Anchor = AnchorStyles.None; this.Controls.Add(pic); pic.Visible = IsShowLoadingImage; this.Visible = false; } /// /// /// protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; // 开启 WS_EX_TRANSPARENT,使控件支持透明 return cp; } } /// /// /// /// protected override void OnPaint(PaintEventArgs pe) { Pen labelBorderPen; SolidBrush labelBackColorBrush; if (_isTransparent) { Color cl = Color.FromArgb(_alpha, this.BackColor); labelBorderPen = new Pen(cl, 0); labelBackColorBrush = new SolidBrush(cl); } else { labelBorderPen = new Pen(this.BackColor, 0); labelBackColorBrush = new SolidBrush(this.BackColor); } base.OnPaint(pe); pe.Graphics.DrawRectangle(labelBorderPen, 0, 0, this.Width, this.Height); pe.Graphics.FillRectangle(labelBackColorBrush, 0, 0, this.Width, this.Height); } /// /// 显示加载 /// public void ShowLoad() { pic.Size = new Size(48, 48); pic.SizeMode = PictureBoxSizeMode.AutoSize; pic.Location = new Point(this.Location.X + (this.Width - pic.Width) / 2, this.Location.Y + (this.Height - pic.Height) / 2);//居中 this.BringToFront(); this.Dock = DockStyle.Fill; this.Visible = true; } /// /// 隐藏加载 /// public void HideLoad() { this.Visible = false; } } }