java 鼠标穿透_实现鼠标穿透窗体

本文介绍了如何在Java中创建一个窗体,使其具有鼠标穿透功能。通过使用System.Runtime.InteropServices来调用Windows API,设置窗口的额外样式,使得窗体在保持透明的同时,允许鼠标事件穿透。
摘要由CSDN通过智能技术生成

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace MouseThroughForm

{

public partial class Frm_Main : Form

{

public Frm_Main()

{

InitializeComponent();

}

private const uint WS_EX_LAYERED = 0x80000;

private const int WS_EX_TRANSPARENT = 0x20;

private const int GWL_EXSTYLE = (-20);

private string Var_genre = "";//记录当前操作的类型

#region 在窗口结构中为指定的窗口设置信息

///

/// 在窗口结构中为指定的窗口设置信息

///

/// 欲为其取得信息的窗口的句柄

/// 欲取回的信息

/// 由nIndex指定的窗口信息的新值

///

[DllImport("user32", EntryPoint = "SetWindowLong")]

private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

#endregion

#region 从指定窗口的结构中取得信息

///

/// 从指定窗口的结构中取得信息

///

/// 欲为其获取信息的窗口的句柄

/// 欲取回的信息

///

[DllImport("user32", EntryPoint = "GetWindowLong")]

private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

#endregion

#region 使窗口有鼠标穿透功能

///

/// 使窗口有鼠标穿透功能

///

private void CanPenetrate()

{

uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);

uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);

}

#endregion

private void Frm_Main_Load(object sender, EventArgs e)

{

this.ShowInTaskbar = false;//窗体不出现在Windows任务栏中

CanPenetrate();

this.TopMost = true;//使窗体始终在其它窗体之上

}

#region 设置颜色和透明度的状态

///

/// 设置颜色和透明度的状态

///

private void SetEstate(Form Frm, object sender)

{

Var_genre = ((ToolStripMenuItem)sender).Name;

string Tem_Str = Var_genre;

if (Var_genre.IndexOf('_') >= 0)

{

Var_genre = Tem_Str.Substring(0, Tem_Str.IndexOf('_'));

}

switch (Var_genre)

{

case "ToolColor":

{

Color Tem_Color=Color.Gainsboro;

switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))

{

case 1: Tem_Color = Color.Gainsboro; break;

case 2: Tem_Color = Color.DarkOrchid; break;

case 3: Tem_Color = Color.RoyalBlue; break;

case 4: Tem_Color = Color.Gold; break;

case 5: Tem_Color = Color.LightGreen; break;

}

Frm.BackColor = Tem_Color;

break;

}

case "ToolClarity":

{

double Tem_Double = 0.0;

switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))

{

case 1: Tem_Double = 0.1; break;

case 2: Tem_Double = 0.2; break;

case 3: Tem_Double = 0.3; break;

case 4: Tem_Double = 0.4; break;

case 5: Tem_Double = 0.5; break;

case 6: Tem_Double = 0.6; break;

case 7: Tem_Double = 0.7; break;

case 8: Tem_Double = 0.8; break;

case 9: Tem_Double = 0.9; break;

}

Frm.Opacity = Tem_Double;

break;

}

case "ToolAcquiescence":

{

Frm.BackColor = Color.Gainsboro;

Frm.Opacity = 0.6;

break;

}

case "ToolClose":

{

Close();

break;

}

}

}

#endregion

private void ToolColor_Glass_Click(object sender, EventArgs e)

{

SetEstate(this, sender);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值