The link you posted does show the only reliable way to discern between mouse messages generated by a physical mouse, and those synthesized in response to touch and pen input.
For completeness, here is the fully working code. The code relies on state that is valid only while handling a mouse message. Calling it at any other time has undefined behavior:
bool IsTouchEvent() {
const LONG_PTR c_SIGNATURE_MASK = 0xFFFFFF00;
const LONG_PTR c_MOUSEEVENTF_FROMTOUCH = 0xFF515700;
LONG_PTR extraInfo = GetMessageExtraInfo();
return ( ( extraInfo & c_SIGNATURE_MASK ) == c_MOUSEEVENTF_FROMTOUCH );
}
The additional WM_MOUSEMOVE
messages you are observing, are an artifact of how the system implements its internal bookkeeping. For example, if a window is shown or hidden, the mouse cursor may be over a different window now, and needs to be recalculated. To do this, the system synthesizes an artificial WM_MOUSEMOVE
message.
This effect is explained in Raymond Chen's blog: Why do I get spurious WM_MOUSEMOVE messages?.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…