日期:2014-06-10  浏览次数:20609 次

【实例说明】
 用户在对桌面进行操作时,为了使桌面更加美观,可以在桌面的上面加一层类似于玻璃的效果,用户可以用鼠标透过“玻璃”对桌面进行操作。
 本实例通过使用鼠标穿透窗体来实现以上功能。

【关键技术】
 本实例实现时主要用到了API函数SetWindowLong和GetWindowLong,下面分别对他们进行详细的讲解。
 (1)SetWindowLong函数:该函数主要用于在窗口结构中为指定的窗口设置信息。语法格式如下:

1 [DllImport("user32", EntryPoint = "SetWindowLong")]
2 private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

(2)GetWindowLong函数:该函数主要从指定窗口的结构中取得信息。语法格式如下:

1 [DllImport("user32", EntryPoint = "GetWindowLong")]
2 private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);


【设计过程】
 (1)打开Visual Studio,新建WinForm应用程序,命名为MouseThroughForm。
 (2)更改默认窗体Form1的Name属性为Frm_Main,并将该窗体的FormBorderStyle属性设置为None。
 (3)在Frm_Main窗体中添加一个NotifyIcon控件,并设置Icon属性为指定的图标,用来显示提示信息。
 (4)添加一个ContextMenuStrip控件,用来作为程序的快捷菜单。
 (5)Frm_Main窗体中,首先自顶一个方法CanPenetrate,该方法用来通过API函数SetWindowLong和GetWindowLong来实现鼠标的穿透效果。

窗体实现代码如下:

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

 

 

 

 

 

【来自:[LonelyShadow 博客] http://www.cnblogs.com/LonelyShadow