Frm_Main.cs

View Code
  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 }

Frm_Main.designer.cs

View Code
  1 namespace MouseThroughForm
2 {
3 partial class Frm_Main
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 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 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.components = new System.ComponentModel.Container();
32 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Frm_Main));
33 this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
34 this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
35 this.ToolColor = new System.Windows.Forms.ToolStripMenuItem();
36 this.ToolColor_Gainsboro = new System.Windows.Forms.ToolStripMenuItem();
37 this.ToolColor_DarkOrchid = new System.Windows.Forms.ToolStripMenuItem();
38 this.ToolColor_RoyalBlue = new System.Windows.Forms.ToolStripMenuItem();
39 this.ToolColor_Gold = new System.Windows.Forms.ToolStripMenuItem();
40 this.ToolColor_LightGreen = new System.Windows.Forms.ToolStripMenuItem();
41 this.ToolClarity = new System.Windows.Forms.ToolStripMenuItem();
42 this.ToolClarity_10 = new System.Windows.Forms.ToolStripMenuItem();
43 this.ToolClarity_20 = new System.Windows.Forms.ToolStripMenuItem();
44 this.ToolClarity_30 = new System.Windows.Forms.ToolStripMenuItem();
45 this.ToolClarity_40 = new System.Windows.Forms.ToolStripMenuItem();
46 this.ToolClarity_50 = new System.Windows.Forms.ToolStripMenuItem();
47 this.ToolClarity_60 = new System.Windows.Forms.ToolStripMenuItem();
48 this.ToolClarity_70 = new System.Windows.Forms.ToolStripMenuItem();
49 this.ToolClarity_80 = new System.Windows.Forms.ToolStripMenuItem();
50 this.ToolClarity_90 = new System.Windows.Forms.ToolStripMenuItem();
51 this.ToolAcquiescence = new System.Windows.Forms.ToolStripMenuItem();
52 this.ToolClose = new System.Windows.Forms.ToolStripMenuItem();
53 this.contextMenuStrip1.SuspendLayout();
54 this.SuspendLayout();
55 //
56 // notifyIcon1
57 //
58 this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
59 this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
60 this.notifyIcon1.Text = "notifyIcon1";
61 this.notifyIcon1.Visible = true;
62 //
63 // contextMenuStrip1
64 //
65 this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
66 this.ToolColor,
67 this.ToolClarity,
68 this.ToolAcquiescence,
69 this.ToolClose});
70 this.contextMenuStrip1.Name = "contextMenuStrip1";
71 this.contextMenuStrip1.Size = new System.Drawing.Size(137, 92);
72 //
73 // ToolColor
74 //
75 this.ToolColor.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
76 this.ToolColor_Gainsboro,
77 this.ToolColor_DarkOrchid,
78 this.ToolColor_RoyalBlue,
79 this.ToolColor_Gold,
80 this.ToolColor_LightGreen});
81 this.ToolColor.Name = "ToolColor";
82 this.ToolColor.Size = new System.Drawing.Size(136, 22);
83 this.ToolColor.Text = "颜色设置";
84 //
85 // ToolColor_Gainsboro
86 //
87 this.ToolColor_Gainsboro.Name = "ToolColor_Gainsboro";
88 this.ToolColor_Gainsboro.Size = new System.Drawing.Size(100, 22);
89 this.ToolColor_Gainsboro.Tag = "1";
90 this.ToolColor_Gainsboro.Text = "玻璃";
91 this.ToolColor_Gainsboro.Click += new System.EventHandler(this.ToolColor_Glass_Click);
92 //
93 // ToolColor_DarkOrchid
94 //
95 this.ToolColor_DarkOrchid.Name = "ToolColor_DarkOrchid";
96 this.ToolColor_DarkOrchid.Size = new System.Drawing.Size(100, 22);
97 this.ToolColor_DarkOrchid.Tag = "2";
98 this.ToolColor_DarkOrchid.Text = "炫紫";
99 this.ToolColor_DarkOrchid.Click += new System.EventHandler(this.ToolColor_Glass_Click);
100 //
101 // ToolColor_RoyalBlue
102 //
103 this.ToolColor_RoyalBlue.Name = "ToolColor_RoyalBlue";
104 this.ToolColor_RoyalBlue.Size = new System.Drawing.Size(100, 22);
105 this.ToolColor_RoyalBlue.Tag = "3";
106 this.ToolColor_RoyalBlue.Text = "海洋";
107 this.ToolColor_RoyalBlue.Click += new System.EventHandler(this.ToolColor_Glass_Click);
108 //
109 // ToolColor_Gold
110 //
111 this.ToolColor_Gold.Name = "ToolColor_Gold";
112 this.ToolColor_Gold.Size = new System.Drawing.Size(100, 22);
113 this.ToolColor_Gold.Tag = "4";
114 this.ToolColor_Gold.Text = "金属";
115 this.ToolColor_Gold.Click += new System.EventHandler(this.ToolColor_Glass_Click);
116 //
117 // ToolColor_LightGreen
118 //
119 this.ToolColor_LightGreen.Name = "ToolColor_LightGreen";
120 this.ToolColor_LightGreen.Size = new System.Drawing.Size(100, 22);
121 this.ToolColor_LightGreen.Tag = "5";
122 this.ToolColor_LightGreen.Text = "翠绿";
123 this.ToolColor_LightGreen.Click += new System.EventHandler(this.ToolColor_Glass_Click);
124 //
125 // ToolClarity
126 //
127 this.ToolClarity.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
128 this.ToolClarity_10,
129 this.ToolClarity_20,
130 this.ToolClarity_30,
131 this.ToolClarity_40,
132 this.ToolClarity_50,
133 this.ToolClarity_60,
134 this.ToolClarity_70,
135 this.ToolClarity_80,
136 this.ToolClarity_90});
137 this.ToolClarity.Name = "ToolClarity";
138 this.ToolClarity.Size = new System.Drawing.Size(136, 22);
139 this.ToolClarity.Text = "透明度设置";
140 //
141 // ToolClarity_10
142 //
143 this.ToolClarity_10.Name = "ToolClarity_10";
144 this.ToolClarity_10.Size = new System.Drawing.Size(101, 22);
145 this.ToolClarity_10.Tag = "1";
146 this.ToolClarity_10.Text = "10%";
147 this.ToolClarity_10.Click += new System.EventHandler(this.ToolColor_Glass_Click);
148 //
149 // ToolClarity_20
150 //
151 this.ToolClarity_20.Name = "ToolClarity_20";
152 this.ToolClarity_20.Size = new System.Drawing.Size(101, 22);
153 this.ToolClarity_20.Tag = "2";
154 this.ToolClarity_20.Text = "20%";
155 this.ToolClarity_20.Click += new System.EventHandler(this.ToolColor_Glass_Click);
156 //
157 // ToolClarity_30
158 //
159 this.ToolClarity_30.Name = "ToolClarity_30";
160 this.ToolClarity_30.Size = new System.Drawing.Size(101, 22);
161 this.ToolClarity_30.Tag = "3";
162 this.ToolClarity_30.Text = "30%";
163 this.ToolClarity_30.Click += new System.EventHandler(this.ToolColor_Glass_Click);
164 //
165 // ToolClarity_40
166 //
167 this.ToolClarity_40.Name = "ToolClarity_40";
168 this.ToolClarity_40.Size = new System.Drawing.Size(101, 22);
169 this.ToolClarity_40.Tag = "4";
170 this.ToolClarity_40.Text = "40%";
171 this.ToolClarity_40.Click += new System.EventHandler(this.ToolColor_Glass_Click);
172 //
173 // ToolClarity_50
174 //
175 this.ToolClarity_50.Name = "ToolClarity_50";
176 this.ToolClarity_50.Size = new System.Drawing.Size(101, 22);
177 this.ToolClarity_50.Tag = "5";
178 this.ToolClarity_50.Text = "50%";
179 this.ToolClarity_50.Click += new System.EventHandler(this.ToolColor_Glass_Click);
180 //
181 // ToolClarity_60
182 //
183 this.ToolClarity_60.Name = "ToolClarity_60";
184 this.ToolClarity_60.Size = new System.Drawing.Size(101, 22);
185 this.ToolClarity_60.Tag = "6";
186 this.ToolClarity_60.Text = "60%";
187 this.ToolClarity_60.Click += new System.EventHandler(this.ToolColor_Glass_Click);
188 //
189 // ToolClarity_70
190 //
191 this.ToolClarity_70.Name = "ToolClarity_70";
192 this.ToolClarity_70.Size = new System.Drawing.Size(101, 22);
193 this.ToolClarity_70.Tag = "7";
194 this.ToolClarity_70.Text = "70%";
195 this.ToolClarity_70.Click += new System.EventHandler(this.ToolColor_Glass_Click);
196 //
197 // ToolClarity_80
198 //
199 this.ToolClarity_80.Name = "ToolClarity_80";
200 this.ToolClarity_80.Size = new System.Drawing.Size(101, 22);
201 this.ToolClarity_80.Tag = "8";
202 this.ToolClarity_80.Text = "80%";
203 this.ToolClarity_80.Click += new System.EventHandler(this.ToolColor_Glass_Click);
204 //
205 // ToolClarity_90
206 //
207 this.ToolClarity_90.Name = "ToolClarity_90";
208 this.ToolClarity_90.Size = new System.Drawing.Size(101, 22);
209 this.ToolClarity_90.Tag = "9";
210 this.ToolClarity_90.Text = "90%";
211 this.ToolClarity_90.Click += new System.EventHandler(this.ToolColor_Glass_Click);
212 //
213 // ToolAcquiescence
214 //
215 this.ToolAcquiescence.Name = "ToolAcquiescence";
216 this.ToolAcquiescence.Size = new System.Drawing.Size(136, 22);
217 this.ToolAcquiescence.Text = "默认效果";
218 this.ToolAcquiescence.Click += new System.EventHandler(this.ToolColor_Glass_Click);
219 //
220 // ToolClose
221 //
222 this.ToolClose.Name = "ToolClose";
223 this.ToolClose.Size = new System.Drawing.Size(136, 22);
224 this.ToolClose.Text = "退出";
225 this.ToolClose.Click += new System.EventHandler(this.ToolColor_Glass_Click);
226 //
227 // Frm_Main
228 //
229 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
230 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
231 this.BackColor = System.Drawing.Color.Gainsboro;
232 this.ClientSize = new System.Drawing.Size(383, 266);
233 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
234 this.Name = "Frm_Main";
235 this.Opacity = 0.6;
236 this.Text = "鼠标穿透窗体";
237 this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
238 this.Load += new System.EventHandler(this.Frm_Main_Load);
239 this.contextMenuStrip1.ResumeLayout(false);
240 this.ResumeLayout(false);
241
242 }
243
244 #endregion
245
246 private System.Windows.Forms.NotifyIcon notifyIcon1;
247 private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
248 private System.Windows.Forms.ToolStripMenuItem ToolColor;
249 private System.Windows.Forms.ToolStripMenuItem ToolClarity;
250 private System.Windows.Forms.ToolStripMenuItem ToolAcquiescence;
251 private System.Windows.Forms.ToolStripMenuItem ToolClose;
252 private System.Windows.Forms.ToolStripMenuItem ToolColor_Gainsboro;
253 private System.Windows.Forms.ToolStripMenuItem ToolColor_DarkOrchid;
254 private System.Windows.Forms.ToolStripMenuItem ToolColor_RoyalBlue;
255 private System.Windows.Forms.ToolStripMenuItem ToolColor_Gold;
256 private System.Windows.Forms.ToolStripMenuItem ToolColor_LightGreen;
257 private System.Windows.Forms.ToolStripMenuItem ToolClarity_10;
258 private System.Windows.Forms.ToolStripMenuItem ToolClarity_20;
259 private System.Windows.Forms.ToolStripMenuItem ToolClarity_30;
260 private System.Windows.Forms.ToolStripMenuItem ToolClarity_40;
261 private System.Windows.Forms.ToolStripMenuItem ToolClarity_50;
262 private System.Windows.Forms.ToolStripMenuItem ToolClarity_60;
263 private System.Windows.Forms.ToolStripMenuItem ToolClarity_70;
264 private System.Windows.Forms.ToolStripMenuItem ToolClarity_80;
265 private System.Windows.Forms.ToolStripMenuItem ToolClarity_90;
266
267 }
268 }




作者:墨明棋妙
出处:http://www.cnblogs.com/ynbt/
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。
 posted on 2012-04-27 13:33  纳米程序员  阅读(422)  评论(0编辑  收藏  举报