以前在玩射击游戏的时候,狙击枪的设定一般是开镜才有准星,所以想是不是可以自己造一个默认准星出来,思路是现在窗口上画一个准星,然后把窗体其他区域都透明,然后设置鼠标穿透;
结果是:
UpdateLayeredWindow 的不规则窗口中,添加鼠标穿透功能导致不规则窗口失效。
GraphicsPathForm 的不规则窗口中,可以愉快地使用鼠标穿透功能。
注意:
全屏的游戏,窗口是不能最前的,可以先把游戏设置成窗口模式(尴尬),一般游戏切换全屏的按键是“alt+enter”。
鼠标穿透
private const uint WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
private const int GWL_STYLE = (-16);
private const int GWL_EXSTYLE = (-20);
private const int LWA_ALPHA = 0;
[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(
IntPtr hwnd,
int nIndex,
uint dwNewLong
);
[DllImport("user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(
IntPtr hwnd,
int nIndex
);
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int SetLayeredWindowAttributes(
IntPtr hwnd,
int crKey,
int bAlpha,
int dwFlags
);
/// <summary>
/// 设置窗体具有鼠标穿透效果
/// </summary>
public void SetPenetrate()
{
this.TopMost = true;
SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
}
镂空窗体
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetWindowLong(Handle, GWL_EXSTYLE, WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0x00FF00, 255, LWA_COLORKEY);//替换某种颜色为透明(0x00FF00:绿色)
}
private const uint WS_EX_LAYERED = 0x80000;//异形窗体特效的实现
private const int GWL_EXSTYLE = -20;//设定一个新的扩展风格
private const int LWA_COLORKEY = 1;//透明方式
[DllImport("user32", EntryPoint = "SetWindowLong")]
//改变指定窗口的属性
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
//设置分层窗口透明度
private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
private void Form1_Load(object sender, EventArgs e)
{
//TransparencyKey = BackColor;
}
}
标签:const,鼠标,C#,private,int,uint,WS,Winform From: https://www.cnblogs.com/guangzhiruijie/p/17631157.html