Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. global mouse capture

global mouse capture

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 105393
    wrote on last edited by
    #1

    I need a program's main window to roll up when the mouse leaves, I think the best way to do this would be with some kind of global mouse capture function but have no idea how to do this, can someone please point me in the right direction? Much Appreciated, spoonchops

    M R 2 Replies Last reply
    0
    • U User 105393

      I need a program's main window to roll up when the mouse leaves, I think the best way to do this would be with some kind of global mouse capture function but have no idea how to do this, can someone please point me in the right direction? Much Appreciated, spoonchops

      M Offline
      M Offline
      Moak
      wrote on last edited by
      #2

      I am not sure if this works... but how about this:

      void CMyControl::OnLButtonDown(UINT nFlags, CPoint point)
      {
      CStatic::OnLButtonDown(nFlags, point);
      SetCapture(); //claim mouse input
      }

      void CMyControl::OnMouseMove(UINT nFlags, CPoint point)
      {
      DoStuff(); // <--- your stuff :)
      CStatic::OnMouseMove(nFlags, point);
      }

      void CMyControl::OnLButtonUp(UINT nFlags, CPoint point)
      {
      ReleaseCapture(); //back to normal mouse input processing
      CStatic::OnLButtonUp(nFlags, point);
      }

      I assume here your control is derived from CStatic.

      U 1 Reply Last reply
      0
      • M Moak

        I am not sure if this works... but how about this:

        void CMyControl::OnLButtonDown(UINT nFlags, CPoint point)
        {
        CStatic::OnLButtonDown(nFlags, point);
        SetCapture(); //claim mouse input
        }

        void CMyControl::OnMouseMove(UINT nFlags, CPoint point)
        {
        DoStuff(); // <--- your stuff :)
        CStatic::OnMouseMove(nFlags, point);
        }

        void CMyControl::OnLButtonUp(UINT nFlags, CPoint point)
        {
        ReleaseCapture(); //back to normal mouse input processing
        CStatic::OnLButtonUp(nFlags, point);
        }

        I assume here your control is derived from CStatic.

        U Offline
        U Offline
        User 105393
        wrote on last edited by
        #3

        Well that's great but it's actually the implementation of the SetCapture() and ReleaseCapture() functions I was after as it were....

        J M 2 Replies Last reply
        0
        • U User 105393

          Well that's great but it's actually the implementation of the SetCapture() and ReleaseCapture() functions I was after as it were....

          J Offline
          J Offline
          Joan M
          wrote on last edited by
          #4

          If you want to know when the control has been left you can use the message handler: LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam) in the parent window of your control. Hope this helps...

          https://www.robotecnik.com freelance robots, PLC and CNC programmer.

          1 Reply Last reply
          0
          • U User 105393

            Well that's great but it's actually the implementation of the SetCapture() and ReleaseCapture() functions I was after as it were....

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            oops sorry man. I did misread "when the mouse leaves" as "when the mouse moves". time for a :coffee:

            1 Reply Last reply
            0
            • U User 105393

              I need a program's main window to roll up when the mouse leaves, I think the best way to do this would be with some kind of global mouse capture function but have no idea how to do this, can someone please point me in the right direction? Much Appreciated, spoonchops

              R Offline
              R Offline
              Roger Allen
              wrote on last edited by
              #6

              You need to make use of TrackMouseEvent. The trick is that entering one of you dialogs child windows will also trigger this, so you will need to do a GetCursorPos() and WindowFromPoint() and then see whether the window with the mouse in it is a child of your dialog. If it is, you then need to retrack the mouse event again, until it leaves the child window and enters/leaves all the windows of your dialog. At the point where its not in your dialog or a child of your dialog you can then roll up your window. Its a bit difficult to get right (we did it here). The above bit was the hardest bit to get working correctly. Roger Allen Sonork 100.10016 WHats brown and sticky? A stick or some smelly stuff!

              U 1 Reply Last reply
              0
              • R Roger Allen

                You need to make use of TrackMouseEvent. The trick is that entering one of you dialogs child windows will also trigger this, so you will need to do a GetCursorPos() and WindowFromPoint() and then see whether the window with the mouse in it is a child of your dialog. If it is, you then need to retrack the mouse event again, until it leaves the child window and enters/leaves all the windows of your dialog. At the point where its not in your dialog or a child of your dialog you can then roll up your window. Its a bit difficult to get right (we did it here). The above bit was the hardest bit to get working correctly. Roger Allen Sonork 100.10016 WHats brown and sticky? A stick or some smelly stuff!

                U Offline
                U Offline
                User 105393
                wrote on last edited by
                #7

                Thanks Roger, that's exactly what I was looking for :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups