TestCrossForm.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. namespace Y.Test.Views
  11. {
  12. public partial class TestCrossForm : Form
  13. {
  14. #region 窗体穿透
  15. private const uint WS_EX_LAYERED = 0x80000;
  16. private const int WS_EX_TRANSPARENT = 0x20;
  17. private const int GWL_STYLE = (-16);
  18. private const int GWL_EXSTYLE = (-20);
  19. private const int LWA_ALPHA = 0;
  20. [DllImport("user32", EntryPoint = "SetWindowLong")]
  21. private static extern uint SetWindowLong(
  22. IntPtr hwnd,
  23. int nIndex,
  24. uint dwNewLong
  25. );
  26. [DllImport("user32", EntryPoint = "GetWindowLong")]
  27. private static extern uint GetWindowLong(
  28. IntPtr hwnd,
  29. int nIndex
  30. );
  31. [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
  32. private static extern int SetLayeredWindowAttributes(
  33. IntPtr hwnd,
  34. int crKey,
  35. int bAlpha,
  36. int dwFlags
  37. );
  38. /// <summary>
  39. /// 设置窗体具有鼠标穿透效果
  40. /// </summary>
  41. public void SetPenetrate()
  42. {
  43. this.TopMost = true;
  44. SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
  45. SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
  46. }
  47. #endregion
  48. public TestCrossForm()
  49. {
  50. InitializeComponent();
  51. }
  52. private void TestCrossForm_Load(object sender, EventArgs e)
  53. {
  54. SetPenetrate();
  55. }
  56. }
  57. }