Search code examples
c++mfccursordetectmousemove

If mouse movement detected, show cursor in a dialog window C++ MFC


The program written in C++ MFC has a Dialog Window which plays a full screen video and the cursor is hidden.

  1. I want to display the cursor when there is movement in mouse (video is playing in background)

  2. Cursor disappears when the mouse is inactive for 3 seconds (Video still playing)

Example:It is just like any video player in fullscreen mode, where the controls are hidden if mouse is inactive and mouse movement gets the controls back.

I have tried

if(WM_MOUSEMOVE)
{ShowCursor(TRUE)}

in the BOOL CDialog1::OnInitDialog()

But it shows (TRUE) even if there is no mouse movement.

Thank you!


Solution

  • This code:

    I have tried if(WM_MOUSEMOVE) {ShowCursor(TRUE)
    
    } in the BOOL CDialog1::OnInitDialog()
    

    looks like if it was a pseudo code, if(WM_MOUSEMOVE) is equivalent to if(true).

    What you should do is to catch WM_MOUSEMOVE message and then show your cursor, still inside this message handler set a timer with time for example 3 seconds, in timer handler hide your cursor. Remember to recreate your timer each time WM_MOUSEMOVE is received so it will reset it to start counting again from begining.

    I am not getting into details, as this question is not on how to receive messages with MFC, right? You dont catch messages inside OnInitDialog.


    BOOL CDlg::PreTranslateMessage(MSG* pMsg)
    {
      if (pMsg->message == WM_MOUSEMOVE)
      {}
      return CDialogEx::PreTranslateMessage(pMsg);
    }