c# - WinForms: Detect when cursor enters/leaves the form or its controls -
i need way of detecting when cursor enters or leaves form. form.mouseenter/mouseleave doesn't work when controls fill form, have subscribe mouseenter event of controls (e.g. panels on form). other way of tracking form cursor entry/exit globally?
you can try :
private void form3_load(object sender, eventargs e) { mousedetector m = new mousedetector(); m.mousemove += new mousedetector.mousemovedlg(m_mousemove); } void m_mousemove(object sender, point p) { point pt = this.pointtoclient(p); this.text = (this.clientsize.width >= pt.x && this.clientsize.height >= pt.y && pt.x > 0 && pt.y > 0)?"in":"out"; }
the mousedetector class :
using system; using system.collections.generic; using system.linq; using system.text; using system.runtime.interopservices; using system.windows.forms; using system.drawing; class mousedetector { #region apis [dllimport("gdi32")] public static extern uint getpixel(intptr hdc, int xpos, int ypos); [dllimport("user32.dll", charset = charset.auto)] public static extern bool getcursorpos(out point pt); [dllimport("user32.dll", charset = charset.auto)] public static extern intptr getwindowdc(intptr hwnd); #endregion timer tm = new timer() {interval = 10}; public delegate void mousemovedlg(object sender, point p); public event mousemovedlg mousemove; public mousedetector() { tm.tick += new eventhandler(tm_tick); tm.start(); } void tm_tick(object sender, eventargs e) { point p; getcursorpos(out p); if (mousemove != null) mousemove(this, new point(p.x,p.y)); } [structlayout(layoutkind.sequential)] public struct point { public int x; public int y; public point(int x, int y) { x = x; y = y; } } }
Comments
Post a Comment