1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace TransparentPanelTest
  10. {
  11. public class TransparentPanel : Control
  12. {
  13. private Color _borderColor;
  14. private int _borderWidth = 1;
  15. private DashStyle _borderStyle = DashStyle.Solid;
  16. private int _opacity = 125;
  17.  
  18. public TransparentPanel()
  19. {
  20.  
  21. }
  22.  
  23. #region Property
  24. [Category("Custom"), Description("Border Color")]
  25. public Color BorderColor
  26. {
  27. set { _borderColor = value; }
  28. get { return _borderColor; }
  29. }
  30.  
  31. [Category("Custom"), Description("Border Width"), DefaultValue(1)]
  32. public int BorderWidth
  33. {
  34. set
  35. {
  36. if (value < 0) value = 0;
  37. _borderWidth = value;
  38. }
  39. get { return _borderWidth; }
  40. }
  41.  
  42. [Category("Custom"), Description("Border Style"), DefaultValue(DashStyle.Solid)]
  43. public DashStyle BorderStyle
  44. {
  45. set { this._borderStyle = value; this.Invalidate(); }
  46. get { return this._borderStyle; }
  47. }
  48.  
  49. [Bindable(true), Category("Custom"), DefaultValue(125), Description("背景的透明度. 有效值0-255")]
  50. public int Opacity
  51. {
  52. get { return _opacity; }
  53. set
  54. {
  55. if (value > 255) value = 255;
  56. else if (value < 0) value = 0;
  57. _opacity = value;
  58. this.Invalidate();
  59. }
  60. }
  61. #endregion
  62.  
  63. protected override void OnPaintBackground(PaintEventArgs e)
  64. {
  65. //do not allow the background to be painted
  66. }
  67.  
  68. protected override CreateParams CreateParams
  69. {
  70. get
  71. {
  72. CreateParams cp = base.CreateParams;
  73. cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
  74. return cp;
  75. }
  76. }
  77.  
  78. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  79. {
  80. if (this._opacity > 0)
  81. {
  82. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(this._opacity, this.BackColor)),
  83. this.ClientRectangle);
  84. }
  85. if (this._borderWidth > 0)
  86. {
  87. Pen pen = new Pen(this._borderColor, this._borderWidth);
  88. pen.DashStyle = this._borderStyle;
  89. e.Graphics.DrawRectangle(pen, e.ClipRectangle.Left, e.ClipRectangle.Top, this.Width - 1, this.Height - 1);
  90. pen.Dispose();
  91. }
  92. }
  93.  
  94. }
  95. }

c# 可以设置透明度的 Panel 组件的更多相关文章

  1. easyUI panel组件

    easyUI panel组件: 属性的使用: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. Javascript - ExtJs - Ext.form.Panel组件

    FormPanel组件(Ext.form.FormPanel) logogram:Ext.form.Panel | xtype:form Ext.form.Panel.配置 frame }//旗下所有 ...

  3. android 控件设置透明度

    问题:java文件中引用组件设置透明度:mGuideLayout.getBackground().setAlpha(125); 一直报null 修改办法:对应的布局文件中添加 android:back ...

  4. css中怎么设置透明度的问题

    小伙伴们是不是在找怎么样去设置页面的透明度的方法呢...别找了,我这儿就有,而且肯定够用了. 我自己会用到的就有两种,可以和大家分享一下. 1.用opcity的方法去设置透明度.代码如下: .div ...

  5. IOS 设置透明度导致底层View始终可见

    正确效果如图:下面的toolBar应该看不见它下面的View, 避免方法:[self.view  addSubView:aSubView];这个aSubView一定不能设置不透明度,应将其设置为Cle ...

  6. cocos2dx对所有子节点设置透明度

    看到cocos2dx2.2.5发布了,修复了输入框的bug,于是我们的项目也升级到了2.2.5, 升级过程还是比较顺利,没想到后来发现设置透明度无效了. 经过调试发现要调用一下setCascadeOp ...

  7. java 图片加水印,设置透明度。说明非常具体

    package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...

  8. sublime text3使用插件SublimeTextTrans设置透明度

    推荐一款在windows上设置sublime2和sublime3背景透明度的插件:SublimeTextTrans, 下载地址:https://github.com/vhanla/SublimeTex ...

  9. (求助)对某一颜色,设置透明度 alpha 后,其他使用该颜色的地方 受到影响!!!!原因未知

    对某一颜色,设置透明度 alpha 后,其他使用该颜色的地方 受到影响!!!!原因未知,有谁碰到过这样的问题?????? 测试了以下三款手机,结果如下: 1.android 4.4.2: 不受影响 2 ...

随机推荐

  1. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...

  2. nagios安装配置

     http://www.codeweblog.com/nagios%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/ 上线的服务器有时会被人攻击,导致服务不可用,今天安装配置了 ...

  3. Notes of Linked Data concept and application - TODO

    Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...

  4. CollectionsAPI

    鉴于总用,总结一下: 类别 方法 查找 binarySearch. 返回 enumeration.checkedSortedMap 判断 max.min 移动 reverse(List).revers ...

  5. jce

    jdk8:jce 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html jdk ...

  6. Android——BaseAdapter相关

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  7. 作业:用HTML制作邮箱登陆界面

    <body leftmargin="200" rightmargin="200"> <font size="45" > ...

  8. 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke 解决办法

    增加IsHandleCreated 判断 if (this.IsHandleCreated) { this.Invoke(new EventHandler(delegate { ...... })); ...

  9. Linux系统编程@多线程编程(一)

    多线程编程 涉及操作系统原理概念 时间片 进程状态 上下文: 对进程来说,就是进程的执行环境,具体就是各个变量和数据,包括所有的寄存器变量.打开的文件.内存信息等. 进程的写时复制:由于一般 fork ...

  10. phpstorm用正则删除PHP代码空行小技巧

    有很多小伙伴会遇到代码空行特别多,但是一行一行删除肯定很烦躁,这时候就需要用到批量删除空行. 怎么批量删除空行呢? 我的办法是用正则把所有空行找到,然后一键全部替换. 首先把Match Case和Re ...