1. 程式人生 > >C# Winform 實現自定義半透明loading載入遮罩層

C# Winform 實現自定義半透明loading載入遮罩層

在網頁中通過div+css實現半透明效果不難,今天我們看看一種在winfrom中實現的方法:

效果圖如下,正常時:

C# Winform 實現自定義半透明遮罩層 - 1

顯示遮罩層時:

C# Winform 實現自定義半透明遮罩層 - 2

自定義遮罩層控制元件的原始碼如下:

1 using System;
2 using System.Drawing;
3 using System.Windows.Forms;
4 using System.ComponentModel;
5
6 namespace MyOpaqueLayer
7 {
8 /// <summary>
9 /// 自定義控制元件:半透明控制元件
10 /// </summary>
11 /*
12 * [ToolboxBitmap(typeof(MyOpaqueLayer))]
13 * 用於指定當把你做好的自定義控制元件新增到工具欄時,工具欄顯示的圖示。
14 * 正確寫法應該是
15 * [ToolboxBitmap(typeof(XXXXControl),"xxx.bmp")]
16 * 其中XXXXControl是你的自定義控制元件,"xxx.bmp"是你要用的圖示名稱。
17 */
18 [ToolboxBitmap(typeof(MyOpaqueLayer))]
19 public class MyOpaqueLayer : System
.Windows.Forms.Control
20 {
21 private bool _transparentBG = true;//是否使用透明
22 private int _alpha = 125;//設定透明度
23
24 private System.ComponentModel.Container components = new System.ComponentModel.Container();
25
26 public MyOpaqueLayer()
27 : this(125, true)
28 {
29 }
30
31 public MyOpaqueLayer
(int Alpha, bool IsShowLoadingImage)
32 {
33 SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
34 base.CreateControl();
35
36 this._alpha = Alpha;
37 if (IsShowLoadingImage)
38 {
39 PictureBox pictureBox_Loading = new PictureBox();
40 pictureBox_Loading.BackColor = System.Drawing.Color.White;
41 pictureBox_Loading.Image = 載入中.Properties.Resources.loading;
42 pictureBox_Loading.Name = "pictureBox_Loading";
43 pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
44 pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
45 Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中
46 pictureBox_Loading.Location = Location;
47 pictureBox_Loading.Anchor = AnchorStyles.None;
48 this.Controls.Add(pictureBox_Loading);
49 }
50 }
51
52
53 protected override void Dispose(bool disposing)
54 {
55 if (disposing)
56 {
57 if (!((components == null)))
58 {
59 components.Dispose();
60 }
61 }
62 base.Dispose(disposing);
63 }
64
65 /// <summary>
66 /// 自定義繪製窗體
67 /// </summary>
68 /// <param name="e"></param>
69 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
70 {
71 float vlblControlWidth;
72 float vlblControlHeight;
73
74 Pen labelBorderPen;
75 SolidBrush labelBackColorBrush;
76
77 if (_transparentBG)
78 {
79 Color drawColor = Color.FromArgb(this._alpha, this.BackColor);
80 labelBorderPen = new Pen(drawColor, 0);
81 labelBackColorBrush = new SolidBrush(drawColor);
82 }
83 else
84 {
85 labelBorderPen = new Pen(this.BackColor, 0);
86 labelBackColorBrush = new SolidBrush(this.BackColor);
87 }
88 base.OnPaint(e);
89 vlblControlWidth = this.Size.Width;
90 vlblControlHeight = this.Size.Height;
91 e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
92 e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
93 }
94
95
96 protected override CreateParams CreateParams//v1.10
97 {
98 get
99 {
100 CreateParams cp = base.CreateParams;
101 cp.ExStyle |= 0x00000020; //0x20; // 開啟 WS_EX_TRANSPARENT,使控制元件支援透明
102 return cp;
103 }
104 }
105
106 /*
107 * [Category("myOpaqueLayer"), Description("是否使用透明,預設為True")]
108 * 一般用於說明你自定義控制元件的屬性(Property)。
109 * Category用於說明該屬性屬於哪個分類,Description自然就是該屬性的含義解釋。
110 */
111 [Category("MyOpaqueLayer"), Description("是否使用透明,預設為True")]
112 public bool TransparentBG
113 {
114 get
115 {
116 return _transparentBG;
117 }
118 set
119 {
120 _transparentBG = value;
121 this.Invalidate();
122 }
123 }
124
125 [Category("MyOpaqueLayer"), Description("設定透明度")]
126 public int Alpha
127 {
128 get
129 {
130 return _alpha;
131 }
132 set
133 {
134 _alpha = value;
135 this.Invalidate();
136 }
137 }
138 }
139 }

OpaqueCommand的方法:ShowOpaqueLayer(顯示遮罩層)和HideOpaqueLayer(隱藏遮罩層)

1 using System;
2 using System.Windows.Forms;
3
4 namespace 載入中
5 {
6 class OpaqueCommand
7 {
8 private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;//半透明蒙板層
9
10 /// <summary>
11 /// 顯示遮罩層
12 /// </summary>
13 /// <param name="control">控制元件</param>
14 /// <param name="alpha">透明度</param>
15 /// <param name="isShowLoadingImage">是否顯示圖示</param>
16 public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage)
17 {
18 try
19 {
20 if (this.m_OpaqueLayer == null)
21 {
22 this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(alpha, isShowLoadingImage);
23 control.Controls.

相關推薦

C# Winform 實現定義透明loading載入

在網頁中通過div+css實現半透明效果不難,今天我們看看一種在winfrom中實現的方法: 效果圖如下,正常時: 顯示遮罩層時: 自定義遮罩層控制元件的原始碼如下: 1 using System; 2 using System.Drawing

C#實現Winform定義透明

using System; using System.Drawing; using System.Windows.Forms; using System.ComponentModel; namespace MyOpaqueLayer { /// <summary> /// 自

MFC + CxImage 實現透明按鈕

processor 專用 win dword ssa ont false set 技術 btn.h [cpp] view plain copy #pragma once // CBtn #include "ximage

c#(winform)中定義ListItem類方便ComboBox添加Item項

urn left over string his 定義 return box item 1.定義ListItem類 public class ListItem { private string _key = string.Empty;

C# Winform定義篩選及帶統計行的Datagridview控制元件

網上分享有很多種自制DGV控制元件,都有不小的缺陷。 沒辦法,按需求自己定製了一個。 一、過濾方面類似於Excel的篩選功能。支援右鍵選單篩選,同時也支援在文字框輸入文字按焦點列進行篩選;  二、統計行我採用的是雙Datagridview方案。在構建控制元件時加入一個Dock為Bottom的子Datagr

c# winform定義控制元件新增事件

1)使用者控制元件UserControl1.cs using System; using System.Collections.Generic; using System.ComponentModel

C# WinForm 使用者定義控制元件閃爍的問題

    使用WinForm開發,當使用了大量的使用者自定義控制元件UserControl時,介面拖動或切換時就會出現閃爍的問題。解決方法主要就是重寫UserControl和Form的CreatePar

C# Winform實現炫酷的透明動畫介面(轉載)

http://www.cnblogs.com/Joetao/articles/4631868.html 做過.NET Winform窗體美化的人應該都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以實現窗體的任意透明,效

Thinkphp5.0.18最高效的實現定義類的自動載入方式

今天看了下,tp5的原始碼。發現自定義類庫的載入方式 是有順序的。分別是按對映方式 psr4 psr0,其中最高效的,順序最前的當然是對映方式。 我貼出原始碼,大家看下。 private static function findFile($class) { //echo

thinkPHP3.2.3實現定義類的自動載入

實現類的自動載入有兩種方式: 1.新建一個配置檔案為 也可以自己命名,如果自己命名的話要在config.php中加入配置檔案載入的語句: 'LOAD_EXT_CONFIG' => 'xxx', 然後在裡面設定具體的對映: return array( 'C

css-實現滑鼠移至圖片上顯示

1、將遮罩層html程式碼與圖片放在一個div 我是放在 .img_div裡。 <div class="img_div"> <img src="./images/paella-dish.jpg"> <a href="#

C# 實現定義的USB設備與上位機進行通信(上位機部分)

lob filename 參考 EDA 文件 inpu sha red file   因為以前沒用過USB,對USB也不了解,於是上網查了很多資料,不過網上的資料都是零零散散,不清不楚的,於是我自己總結了一下,下面幾個鏈接是網上這麽多零散資料裏,我覺得比較有參考意義的。  

UpdateLayeredWindow實現定義透明視窗

UpdateLayeredWindow 你是不是很想要一個很漂亮的半透明或是區域性透明的視窗呢,那就仔細看看原始碼吧。 關於updatelayeredwindow的中文介紹,看完還是像我一樣半知不解的就直接看原始碼吧。 函式可以實現的功能:實現帶透明(

C++資料結構與STL--雙向迴圈連結串列(實現定義iterator類)

class dLinkList {private:node<T> *head;  //頭節點size_t length; //連結串列長度void dInsert(node<T> *curr,T val)  //插入的輔助函式,把新節點插入curr前 {node<T>* t

Object-C中對定義實現協議

如果嘗試使用自定義類(例如,人類(person類)、地址簿類(myBook類)、分數類(Fraction類))中的copy方法,如 myBook = [myBook mutableCopy]; person = [Person copy];等類似的操作,將會收到一條異

通過c# 實現定義屬性改變觸發定義事件 ,理解定義事件及其觸發過程

以下說明可解釋自定義的事件的自定義觸發過程: 直接上程式碼,內含說明(介面是兩個文字框textbox1,textbox2,和一個button1,介面的Load事件,button的click事件) Form1 類(呼叫者端) using System; using

c# 實現定義事件訪問器 和 實現介面事件

event EventHandler IDrawingObject.OnDraw { add { lock (PreDrawEvent) {

WinForm輕鬆實現定義分頁

以前都是做web開發,最近接觸了下WinForm,發現WinForm分頁控制元件好像都沒有,網上搜索了一下,發現有很多網友寫的分頁控制元件,分頁效果應該都能實現吧,只是其風格都不是很符合我想要的。做web的時候,我習慣了Extjs的Grid分頁效果,所以也想在WinFor

C++資料結構——二叉搜尋樹(實現定義迭代器)

#ifndef BS_Tree_H #define BS_Tree_H #include"node.h"  #include<iostream> #include<queue> using namespace std; template<typename T>class

(七十三)c#Winform定義控制元件-資源載入窗體

前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制元件,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.