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. Get mouse clicked component

Get mouse clicked component

Scheduled Pinned Locked Moved C / C++ / MFC
questioncareer
6 Posts 3 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.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #1

    Current, I have a parent CWnd, which is displaying many other child CWnds. All my message handling job are being done in parent CWnd, as I do not have access to child CWnds' code. I wish to know which child CWnds is being clicked. I can detect the right click

    afx_msg void OnContextMenu(
    CWnd* pWnd,
    CPoint pos
    );

    pWnd will be the clicked child window. However, how about left click? I know I can get the mouse coordinate, and perform some calculation accordingly to iterate through see which child CWnds falll into the point. However, I just don't want to go through all this. I which I can have something like.

    CWnd* childWind = mouseEvent.getParent();

    C E 2 Replies Last reply
    0
    • Y yccheok

      Current, I have a parent CWnd, which is displaying many other child CWnds. All my message handling job are being done in parent CWnd, as I do not have access to child CWnds' code. I wish to know which child CWnds is being clicked. I can detect the right click

      afx_msg void OnContextMenu(
      CWnd* pWnd,
      CPoint pos
      );

      pWnd will be the clicked child window. However, how about left click? I know I can get the mouse coordinate, and perform some calculation accordingly to iterate through see which child CWnds falll into the point. However, I just don't want to go through all this. I which I can have something like.

      CWnd* childWind = mouseEvent.getParent();

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      for simple child controls, override parent CWnd's PreTranslateMessage().

      BOOL CMyWnd::PreTranslateMessage(MSG* pMsg)
      {
      BOOL b = CDialog::PreTranslateMessage(pMsg);

      if(pMsg->message == WM\_LBUTTONDOWN)
      {
                  
      	if(pMsg->hwnd == m\_edit.GetSafeHwnd())
      	    MessageBox("Edit control);
      }
      
      return b;
      

      }

      if you need to get mouse click on edit field of combo box, or scroll bar of list box, i think you have to to subclass them.

      1 Reply Last reply
      0
      • Y yccheok

        Current, I have a parent CWnd, which is displaying many other child CWnds. All my message handling job are being done in parent CWnd, as I do not have access to child CWnds' code. I wish to know which child CWnds is being clicked. I can detect the right click

        afx_msg void OnContextMenu(
        CWnd* pWnd,
        CPoint pos
        );

        pWnd will be the clicked child window. However, how about left click? I know I can get the mouse coordinate, and perform some calculation accordingly to iterate through see which child CWnds falll into the point. However, I just don't want to go through all this. I which I can have something like.

        CWnd* childWind = mouseEvent.getParent();

        E Offline
        E Offline
        Eugen Podsypalnikov
        wrote on last edited by
        #3

        Please try it :) :

        /*virtual*/ BOOL CYourParentWnd::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
        {
        *pResult = 0;

        NMHDR* pHeader = (NMHDR*) lParam;

        switch (pHeader->code) {
        case NM_CLICK:
        {
        CWnd* pcChild = CWnd::FromHandle(pHeader->hwndFrom);
        //..
        }
        break;
        //..
        }

        return FALSE;
        }

        They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

        C 1 Reply Last reply
        0
        • E Eugen Podsypalnikov

          Please try it :) :

          /*virtual*/ BOOL CYourParentWnd::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
          {
          *pResult = 0;

          NMHDR* pHeader = (NMHDR*) lParam;

          switch (pHeader->code) {
          case NM_CLICK:
          {
          CWnd* pcChild = CWnd::FromHandle(pHeader->hwndFrom);
          //..
          }
          break;
          //..
          }

          return FALSE;
          }

          They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

          C Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          are you sure that WM_NOTIFY will be sent to parent window by all controls on mouse click?

          E 1 Reply Last reply
          0
          • C Cool_Dev

            are you sure that WM_NOTIFY will be sent to parent window by all controls on mouse click?

            E Offline
            E Offline
            Eugen Podsypalnikov
            wrote on last edited by
            #5

            No, I am not sure for "all" controls... ...but I would test it in the author's context :)

            They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

            C 1 Reply Last reply
            0
            • E Eugen Podsypalnikov

              No, I am not sure for "all" controls... ...but I would test it in the author's context :)

              They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

              C Offline
              C Offline
              Cool_Dev
              wrote on last edited by
              #6

              :)

              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