FrmTransparent.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="FrmTransparent.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Data;
  20. using System.Drawing;
  21. using System.Linq;
  22. using System.Reflection;
  23. using System.Runtime.InteropServices;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. namespace HZH_Controls.Forms
  27. {
  28. /// <summary>
  29. /// Class FrmTransparent.
  30. /// Implements the <see cref="HZH_Controls.Forms.FrmBase" />
  31. /// </summary>
  32. /// <seealso cref="HZH_Controls.Forms.FrmBase" />
  33. public partial class FrmTransparent : FrmBase
  34. {
  35. /// <summary>
  36. /// The wm activate
  37. /// </summary>
  38. private const int WM_ACTIVATE = 6;
  39. /// <summary>
  40. /// The wm activateapp
  41. /// </summary>
  42. private const int WM_ACTIVATEAPP = 28;
  43. /// <summary>
  44. /// The wm ncactivate
  45. /// </summary>
  46. private const int WM_NCACTIVATE = 134;
  47. /// <summary>
  48. /// The wa inactive
  49. /// </summary>
  50. private const int WA_INACTIVE = 0;
  51. /// <summary>
  52. /// The wm mouseactivate
  53. /// </summary>
  54. private const int WM_MOUSEACTIVATE = 33;
  55. /// <summary>
  56. /// The ma noactivate
  57. /// </summary>
  58. private const int MA_NOACTIVATE = 3;
  59. /// <summary>
  60. /// Gets the create parameters.
  61. /// </summary>
  62. /// <value>The create parameters.</value>
  63. protected override CreateParams CreateParams
  64. {
  65. get
  66. {
  67. CreateParams cp = base.CreateParams;
  68. cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
  69. return cp;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets or sets the frmchild.
  74. /// </summary>
  75. /// <value>The frmchild.</value>
  76. public FrmBase frmchild
  77. {
  78. get;
  79. set;
  80. }
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="FrmTransparent" /> class.
  83. /// </summary>
  84. public FrmTransparent()
  85. {
  86. InitializeComponent();
  87. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  88. this.SetStyle(ControlStyles.DoubleBuffer, true);
  89. this.SetStyle(ControlStyles.ResizeRedraw, true);
  90. this.SetStyle(ControlStyles.Selectable, true);
  91. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  92. this.SetStyle(ControlStyles.UserPaint, true);
  93. MethodInfo method = base.GetType().GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod);
  94. method.Invoke(this, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, new object[]
  95. {
  96. ControlStyles.Selectable,
  97. false
  98. }, Application.CurrentCulture);
  99. }
  100. /// <summary>
  101. /// 引发 <see cref="E:System.Windows.Forms.Form.Load" /> 事件。
  102. /// </summary>
  103. /// <param name="e">一个包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  104. protected override void OnLoad(EventArgs e)
  105. {
  106. base.OnLoad(e);
  107. base.ShowInTaskbar = false;
  108. base.ShowIcon = true;
  109. }
  110. /// <summary>
  111. /// Sets the active window.
  112. /// </summary>
  113. /// <param name="handle">The handle.</param>
  114. /// <returns>IntPtr.</returns>
  115. [DllImport("user32.dll")]
  116. private static extern IntPtr SetActiveWindow(IntPtr handle);
  117. /// <summary>
  118. /// WNDs the proc.
  119. /// </summary>
  120. /// <param name="m">要处理的 Windows <see cref="T:System.Windows.Forms.Message" />。</param>
  121. protected override void WndProc(ref Message m)
  122. {
  123. if (m.Msg == 33)
  124. {
  125. m.Result = new IntPtr(3);
  126. }
  127. else
  128. {
  129. if (m.Msg == 134)
  130. {
  131. if (((int)m.WParam & 65535) != 0)
  132. {
  133. if (m.LParam != IntPtr.Zero)
  134. {
  135. FrmTransparent.SetActiveWindow(m.LParam);
  136. }
  137. else
  138. {
  139. FrmTransparent.SetActiveWindow(IntPtr.Zero);
  140. }
  141. }
  142. }
  143. else if (m.Msg == 2000)
  144. {
  145. }
  146. base.WndProc(ref m);
  147. }
  148. }
  149. /// <summary>
  150. /// Handles the Load event of the FrmTransparent control.
  151. /// </summary>
  152. /// <param name="sender">The source of the event.</param>
  153. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  154. private void FrmTransparent_Load(object sender, EventArgs e)
  155. {
  156. if (frmchild != null)
  157. {
  158. frmchild.IsShowMaskDialog = false;
  159. var dia = frmchild.ShowDialog(this);
  160. this.DialogResult = dia;
  161. }
  162. }
  163. }
  164. }