TrackMouseEvent in C#?

  • Thread starter Thread starter Tyron Madlener
  • Start date Start date
Hi,

You can use the designer to trap the event or you can write it on your own:

//
// 'this' is the form
//
this.MouseLeave += new System.EventHandler(this.Form1_MouseLeave);

private void Form1_MouseLeave(object sender, System.EventArgs e)
{

}
 
The MouseLeave Event from the Designer uses WM_MOUSELEAVE.
What I need is to get the WM_NCMOUSELEAVE event (with the function
TrackMouseEvent()).
 
I tried writing some code to call that function in the Hope that it will
work at the first go - but it didn't :/
Code:

[DllImport("kernel32.dll", SetLastError=true)]
static extern int GetLastError ();

[DllImport("user32")]
public static extern bool TrackMouseEvent(TRACKMOUSEEVENT lpEventTrack);

[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct TRACKMOUSEEVENT {
public long cbSize;
public long dwFlags;
public long hwndTrack;
public long dwHoverTime;
}


and in the C# App Constructor:

TRACKMOUSEEVENT foo = new TRACKMOUSEEVENT();
foo.cbSize = Marshal.SizeOf(typeof(TRACKMOUSEEVENT));
foo.dwFlags = TME_NONCLIENT;
foo.hwndTrack = (long)this.Handle;
bool bar = TrackMouseEvent(foo);
MessageBox.Show(""+GetLastError());

The TrackMouseEvent function fails and returns zero.
So I called (as msdn advised me) GetLastError() to get the error msg and
it returns 998 which means:
ERROR_NOACCESS Invalid access to memory location.

Has anyone a clue what my OS is trying to tell me here?
 
Back
Top