(二十二)c#Winform自定义控件-半透明窗体

官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

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 点个星 星吧

posted @ 2019-08-16 15:09 冰封一夏 阅读(...) 评论(...) 编辑 收藏
HZHControls控件库官网:http://hzhcontrols.com