五、鼠标穿透
以前在玩射击游戏的时候,狙击枪的设定一般是开镜才有准星,所以想是不是可以自己造一个默认准星出来,思路是现在窗口上画一个准星,然后把窗体其他区域都透明,然后设置鼠标穿透;
结果是:
- 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);
}
扩展阅读:如何在Windows下使一个窗口在全屏游戏时置顶?