技巧解析:主要用到了两个系统API函数 SetWindowLong和GetWindowLong
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.Text; 8 using System.Windows.Forms; 9 using System.Runtime.InteropServices; 10 11 namespace MouseThroughForm 12 { 13 public partial class Frm_Main : Form 14 { 15 public Frm_Main() 16 { 17 InitializeComponent(); 18 } 19 20 private const uint WS_EX_LAYERED = 0x80000; 21 private const int WS_EX_TRANSPARENT = 0x20; 22 private const int GWL_EXSTYLE = (-20); 23 private string Var_genre = "";//记录当前操作的类型 24 25 #region 在窗口结构中为指定的窗口设置信息 26 /// <summary> 27 /// 在窗口结构中为指定的窗口设置信息 28 /// </summary> 29 /// <param name="hwnd">欲为其取得信息的窗口的句柄</param> 30 /// <param name="nIndex">欲取回的信息</param> 31 /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param> 32 /// <returns></returns> 33 [DllImport("user32", EntryPoint = "SetWindowLong")] 34 private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong); 35 #endregion 36 37 #region 从指定窗口的结构中取得信息 38 /// <summary> 39 /// 从指定窗口的结构中取得信息 40 /// </summary> 41 /// <param name="hwnd">欲为其获取信息的窗口的句柄</param> 42 /// <param name="nIndex">欲取回的信息</param> 43 /// <returns></returns> 44 [DllImport("user32", EntryPoint = "GetWindowLong")] 45 private static extern uint GetWindowLong(IntPtr hwnd, int nIndex); 46 #endregion 47 48 #region 使窗口有鼠标穿透功能 49 /// <summary> 50 /// 使窗口有鼠标穿透功能 51 /// </summary> 52 private void CanPenetrate() 53 { 54 uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE); 55 uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED); 56 } 57 #endregion 58 59 private void Frm_Main_Load(object sender, EventArgs e) 60 { 61 this.ShowInTaskbar = false;//窗体不出现在Windows任务栏中 62 CanPenetrate(); 63 this.TopMost = true;//使窗体始终在其它窗体之上 64 } 65 66 #region 设置颜色和透明度的状态 67 /// <summary> 68 /// 设置颜色和透明度的状态 69 /// </summary> 70 private void SetEstate(Form Frm, object sender) 71 { 72 Var_genre = ((ToolStripMenuItem)sender).Name; 73 string Tem_Str = Var_genre; 74 if (Var_genre.IndexOf(‘_‘) >= 0) 75 { 76 Var_genre = Tem_Str.Substring(0, Tem_Str.IndexOf(‘_‘)); 77 } 78 79 switch (Var_genre) 80 { 81 case "ToolColor": 82 { 83 Color Tem_Color=Color.Gainsboro; 84 switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString())) 85 { 86 case 1: Tem_Color = Color.Gainsboro; break; 87 case 2: Tem_Color = Color.DarkOrchid; break; 88 case 3: Tem_Color = Color.RoyalBlue; break; 89 case 4: Tem_Color = Color.Gold; break; 90 case 5: Tem_Color = Color.LightGreen; break; 91 } 92 Frm.BackColor = Tem_Color; 93 break; 94 } 95 case "ToolClarity": 96 { 97 double Tem_Double = 0.0; 98 switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString())) 99 { 100 case 1: Tem_Double = 0.1; break; 101 case 2: Tem_Double = 0.2; break; 102 case 3: Tem_Double = 0.3; break; 103 case 4: Tem_Double = 0.4; break; 104 case 5: Tem_Double = 0.5; break; 105 case 6: Tem_Double = 0.6; break; 106 case 7: Tem_Double = 0.7; break; 107 case 8: Tem_Double = 0.8; break; 108 case 9: Tem_Double = 0.9; break; 109 110 } 111 Frm.Opacity = Tem_Double; 112 break; 113 } 114 case "ToolAcquiescence": 115 { 116 Frm.BackColor = Color.Gainsboro; 117 Frm.Opacity = 0.6; 118 break; 119 } 120 case "ToolClose": 121 { 122 Close(); 123 break; 124 } 125 126 } 127 } 128 #endregion 129 130 private void ToolColor_Glass_Click(object sender, EventArgs e) 131 { 132 SetEstate(this, sender); 133 } 134 } 135 }
时间: 2024-12-25 16:28:14