我目前正在通过拦截各种 WndProc 消息并在非客户区进行绘制来编写某种自定义表单边框。要为关闭按钮等创建某种悬停效果,我需要跟踪鼠标指针。根据 MSDN,这工作得很好,但要接收WM_NCMOUSELEAVE消息,我必须先调用_TrackMouseEvent 。
令人惊讶的是,它不起作用。_TrackMouseEvent 失败,Marshal.GetLastWin32Error() 返回998(对内存位置的访问无效)。
我一无所知,所以这是我的代码:
class Native
{
[DllImport("comctl32.dll", SetLastError = true)]
public static extern bool _TrackMouseEvent(TRACKMOUSEEVENT tme);
public struct TRACKMOUSEEVENT
{
public int cbSize;
public int dwFlags;
public IntPtr hwndTrack;
public int dwHowerTime;
}
public const int TME_LEAVE = 0x00000002;
public const int TME_NONCLIENT = 0x00000010;
}
private void ActivateLeaveTracking()
{
Native.TRACKMOUSEEVENT tme = new Native.TRACKMOUSEEVENT();
tme.hwndTrack = this.Handle;
tme.dwHowerTime = 0;
tme.dwFlags = Native.TME_LEAVE | Native.TME_NONCLIENT;
tme.cbSize = Marshal.SizeOf(typeof(Native.TRACKMOUSEEVENT));
if (!Native._TrackMouseEvent(tme))
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
}
欢迎任何帮助。:)