h3110w0r1d 2019-07-08 00:07 采纳率: 75%
浏览 659
已采纳

WPF 实现鼠标穿透后如何取消穿透

小白一枚。。最近在做一个音乐播放器,做了个桌面歌词,需要鼠标穿透,通过网上找的代码已经实现,但是在不重新绘制窗口的情况下如何取消穿透??下面是代码

在窗口创建完时执行下面这个代码实现穿透

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);

怎么取消穿透呢

  • 写回答

1条回答 默认 最新

  • h3110w0r1d 2019-07-08 12:24
    关注

    突然看到代码中的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); ,鼠标穿透样式就没了.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料