首页 > 代码库 > winform 遮罩层
winform 遮罩层
代码:
using System;using System.Drawing;using System.Windows.Forms;using System.ComponentModel;namespace MyOpaqueLayer{ /// <summary> /// 自定义控件:半透明控件 /// </summary> /* * [ToolboxBitmap(typeof(MyOpaqueLayer))] * 用于指定当把你做好的自定义控件添加到工具栏时,工具栏显示的图标。 * 正确写法应该是 * [ToolboxBitmap(typeof(XXXXControl),"xxx.bmp")] * 其中XXXXControl是你的自定义控件,"xxx.bmp"是你要用的图标名称。 */ [ToolboxBitmap(typeof(MyOpaqueLayer))] public class MyOpaqueLayer : System.Windows.Forms.Control { private bool _transparentBG = true;//是否使用透明 private int _alpha = 125;//设置透明度 private System.ComponentModel.Container components = new System.ComponentModel.Container(); public MyOpaqueLayer() : this(125, true) { } public MyOpaqueLayer(int Alpha, bool IsShowLoadingImage) { SetStyle(System.Windows.Forms.ControlStyles.Opaque, true); base.CreateControl(); this._alpha = Alpha; if (IsShowLoadingImage) { //PictureBox pictureBox_Loading = new PictureBox(); //pictureBox_Loading.BackColor = System.Drawing.Color.White; //pictureBox_Loading.Image = WindowsFormsApplication1.Properties.Resources.loading; //pictureBox_Loading.Name = "pictureBox_Loading"; //pictureBox_Loading.Size = new System.Drawing.Size(48, 48); //pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; //Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中 //pictureBox_Loading.Location = Location; //pictureBox_Loading.Anchor = AnchorStyles.None; //this.Controls.Add(pictureBox_Loading); } } protected override void Dispose(bool disposing) { if (disposing) { if (!((components == null))) { components.Dispose(); } } base.Dispose(disposing); } /// <summary> /// 自定义绘制窗体 /// </summary> /// <param name="e"></param> protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { float vlblControlWidth; float vlblControlHeight; Pen labelBorderPen; SolidBrush labelBackColorBrush; if (_transparentBG) { Color drawColor = Color.FromArgb(this._alpha, this.BackColor); labelBorderPen = new Pen(drawColor, 0); labelBackColorBrush = new SolidBrush(drawColor); } else { labelBorderPen = new Pen(this.BackColor, 0); labelBackColorBrush = new SolidBrush(this.BackColor); } base.OnPaint(e); vlblControlWidth = this.Size.Width; vlblControlHeight = this.Size.Height; e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight); e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight); } protected override CreateParams CreateParams//v1.10 { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; //0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明 return cp; } } /* * [Category("myOpaqueLayer"), Description("是否使用透明,默认为True")] * 一般用于说明你自定义控件的属性(Property)。 * Category用于说明该属性属于哪个分类,Description自然就是该属性的含义解释。 */ [Category("MyOpaqueLayer"), Description("是否使用透明,默认为True")] public bool TransparentBG { get { return _transparentBG; } set { _transparentBG = value; this.Invalidate(); } } [Category("MyOpaqueLayer"), Description("设置透明度")] public int Alpha { get { return _alpha; } set { _alpha = value; this.Invalidate(); } } }}
调用:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); hide(); } private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null; private void show() { if (this.m_OpaqueLayer == null) { this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(125, false); this.Controls.Add(this.m_OpaqueLayer); this.m_OpaqueLayer.Dock = DockStyle.Fill; this.m_OpaqueLayer.BringToFront(); } this.m_OpaqueLayer.Enabled = true; this.m_OpaqueLayer.Visible = true; //panel1在悬浮层上显示 panel1.Parent = m_OpaqueLayer; panel1.Show(); } private void hide() { this.richTextBox1.Parent = this; panel1.Hide(); if (this.m_OpaqueLayer != null) { this.m_OpaqueLayer.Visible = false; this.m_OpaqueLayer.Enabled = false; } } private void button1_Click(object sender, EventArgs e) { string text = this.richTextBox1.Text.ToString().Trim(); show(); } private void button2_Click(object sender, EventArgs e) { hide(); } }}
http://www.cnblogs.com/JuneZhang/archive/2012/07/06/2579215.html
winform 遮罩层
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。