前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 star 支持一下吧
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
準備工作
這個窗體繼承子基類窗體FrmBase,如果你對FrmBase還不瞭解,請移步 (十七)c#Winform自定義控制項-基類窗體 查看
開始
添加一個Form,命名FrmTransparent,繼承自FrmBase
代碼不多,直接上完整代碼了
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:FrmTransparent.cs 3 // 創建日期:2019-08-15 16:05:00 4 // 功能描述:FrmTransparent 5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using System.Collections.Generic; 8 using System.ComponentModel; 9 using System.Data; 10 using System.Drawing; 11 using System.Linq; 12 using System.Reflection; 13 using System.Runtime.InteropServices; 14 using System.Text; 15 using System.Windows.Forms; 16 17 namespace HZH_Controls.Forms 18 { 19 public partial class FrmTransparent : FrmBase 20 { 21 private const int WM_ACTIVATE = 6; 22 23 private const int WM_ACTIVATEAPP = 28; 24 25 private const int WM_NCACTIVATE = 134; 26 27 private const int WA_INACTIVE = 0; 28 29 private const int WM_MOUSEACTIVATE = 33; 30 31 private const int MA_NOACTIVATE = 3; 32 33 public FrmBase frmchild 34 { 35 get; 36 set; 37 } 38 public FrmTransparent() 39 { 40 InitializeComponent(); 41 42 base.SetStyle(ControlStyles.UserPaint, true); 43 base.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 44 base.SetStyle(ControlStyles.DoubleBuffer, true); 45 46 MethodInfo method = base.GetType().GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod); 47 method.Invoke(this, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, new object[] 48 { 49 ControlStyles.Selectable, 50 false 51 }, Application.CurrentCulture); 52 } 53 54 protected override void OnLoad(EventArgs e) 55 { 56 base.OnLoad(e); 57 base.ShowInTaskbar = false; 58 base.ShowIcon = true; 59 } 60 [DllImport("user32.dll")] 61 private static extern IntPtr SetActiveWindow(IntPtr handle); 62 63 protected override void WndProc(ref Message m) 64 { 65 if (m.Msg == 33) 66 { 67 m.Result = new IntPtr(3); 68 } 69 else 70 { 71 if (m.Msg == 134) 72 { 73 if (((int)m.WParam & 65535) != 0) 74 { 75 if (m.LParam != IntPtr.Zero) 76 { 77 FrmTransparent.SetActiveWindow(m.LParam); 78 } 79 else 80 { 81 FrmTransparent.SetActiveWindow(IntPtr.Zero); 82 } 83 } 84 } 85 else if (m.Msg == 2000) 86 { 87 } 88 base.WndProc(ref m); 89 } 90 } 91 92 private void FrmTransparent_Load(object sender, EventArgs e) 93 { 94 if (frmchild != null) 95 { 96 frmchild.IsShowMaskDialog = false; 97 var dia = frmchild.ShowDialog(this); 98 this.DialogResult = dia; 99 } 100 } 101 } 102 }View Code
1 namespace HZH_Controls.Forms 2 { 3 partial class FrmTransparent 4 { 5 /// <summary> 6 /// Required designer variable. 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// Clean up any resources being used. 12 /// </summary> 13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows Form Designer generated code 24 25 /// <summary> 26 /// Required method for Designer support - do not modify 27 /// the contents of this method with the code editor. 28 /// </summary> 29 private void InitializeComponent() 30 { 31 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmTransparent)); 32 this.SuspendLayout(); 33 // 34 // FrmTransparent 35 // 36 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 37 this.BackColor = System.Drawing.Color.Black; 38 this.ClientSize = new System.Drawing.Size(284, 262); 39 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 40 this.Name = "FrmTransparent"; 41 this.Opacity = 0.5D; 42 this.ShowIcon = false; 43 this.ShowInTaskbar = false; 44 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; 45 this.Text = "FrmTransparent"; 46 this.Load += new System.EventHandler(this.FrmTransparent_Load); 47 this.ResumeLayout(false); 48 49 } 50 51 #endregion 52 } 53 }View Code
主要就是構造函數和load事件裡面的那幾句話
用處及效果
用途:一般用在蒙版,比如彈出視窗的時候,顯示一個半透明蒙版
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧