小白一枚。。最近在做一个音乐播放器,做了个桌面歌词,需要鼠标穿透,通过网上找的代码已经实现,但是在不重新绘制窗口的情况下如何取消穿透??下面是代码
在窗口创建完时执行下面这个代码实现穿透
SourceInitialized += delegate
{
IntPtr hwnd = new WindowInteropHelper(this).Handle;
uint extendedStyle = GetWindowLong(hwnd, GwlExstyle);
SetWindowLong(hwnd, GwlExstyle, extendedStyle | WsExTransparent);
};
private const int WsExTransparent = 0x20;
private const int GwlExstyle = (-20);
[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);
怎么取消穿透呢
突然看到代码中的dwNewLong(难道还有OldLong?)结合查的资料,我这个小白终于明白了。这个鼠标穿透功能是个拓展样式,SetWindowLong函数是设置窗口样式的。
上面问题中的代码搞复杂了,这两行代码就足够了
IntPtr hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, (-20), 0x20);
hwnd是获取的窗口句柄,常量-20指的是拓展风格,0x20是鼠标穿透样式
[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
[DllImport("user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
在设置完穿透之后,那个拓展样式的值就变了,所以在设置之前,用GetWindowLong函数把拓展样式的值取出来( long OldLong = GetWindowLong(hwnd,(-20)); )就叫它OldLong吧...到时候把OldLong还原回去SetWindowLong(hwnd,(-20),OldLong); ,鼠标穿透样式就没了.