CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    How can I identify which of two subclassed controls the mouse is over?

    I have a dialog app with two separate static controls. Both controls have been subclassed from CStatic derived class to implement drag and drop and double clicking. The class works just fine, but I cannot figure out which of the controls is being double clicked or dragged and dropped onto because I only have the code below operating. I need some method in the derived class to determine which control is being manipulated.

    Code:
    // header file
    #pragma once
    
    #ifndef __AFXWIN_H__
    	#error "include 'stdafx.h' before including this file for PCH"
    #endif
    
    #include "resource.h"		// main symbols
    
    
    // CMyStaticDragApp:
    // See MyStaticDrag.cpp for the implementation of this class
    //
    
    class CMyStaticDragApp : public CWinApp
    {
    public:
    	CMyStaticDragApp();
    
    // Overrides
    public:
    	virtual BOOL InitInstance();
    
    // Implementation
    
    	DECLARE_MESSAGE_MAP()
    };
    
    extern CMyStaticDragApp theApp;
    
    // implementation file
    #include "stdafx.h"
    
    #include "StaticDragDrop.h"
    
    
    // CStaticDragDrop
    
    IMPLEMENT_DYNAMIC(CStaticDragDrop, CStatic)
    
    CStaticDragDrop::CStaticDragDrop()
    {
    
    }
    
    CStaticDragDrop::~CStaticDragDrop()
    {
    }
    
    
    BEGIN_MESSAGE_MAP(CStaticDragDrop, CStatic)
    	ON_WM_LBUTTONDBLCLK()
    	ON_WM_RBUTTONDBLCLK()
    	ON_WM_DROPFILES()
    	ON_WM_CREATE()
    END_MESSAGE_MAP()
    
    
    
    // CStaticDragDrop message handlers
    
    
    
    
    void CStaticDragDrop::OnLButtonDblClk(UINT nFlags, CPoint point)
    {
    	MessageBox(_T("You left button double clicked on me"), _T("Left Button Double Click"), MB_ICONINFORMATION);
    
    	CStatic::OnLButtonDblClk(nFlags, point);
    
    }// OnLButtonDblClk(UINT nFlags, CPoint point)
    
    
    void CStaticDragDrop::OnRButtonDblClk(UINT nFlags, CPoint point)
    {
    	MessageBox(_T("You right button double clicked on me"), _T("Right Button Double Click"), MB_ICONINFORMATION);
    
    	CStatic::OnRButtonDblClk(nFlags, point);
    
    }// OnRButtonDblClk(UINT nFlags, CPoint point)
    
    
    void CStaticDragDrop::OnDropFiles(HDROP hDropInfo)
    {
    	TRACE0("*********** OnDropFiles ************************\n");
    	UINT i = 0;
    	UINT nFiles = ::DragQueryFile(hDropInfo, (UINT) -1, NULL, 0);
    	for (i = 0; i < nFiles; i++)
    	{
    		TCHAR szFileName[_MAX_PATH];
    		::DragQueryFile(hDropInfo, i, szFileName, _MAX_PATH);
    
    		//ProcessMyFile(szFileName);
    		TRACE1("%s\n", szFileName);
    
    	}
    
    	::DragFinish(hDropInfo);
    	//CFrameWnd::OnDropFiles(hDropInfo);
    
    	CStatic::OnDropFiles(hDropInfo);
    }
    
    
    int CStaticDragDrop::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CStatic::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	// TODO:  Add your specialized creation code here
    	this->DragAcceptFiles(TRUE);
    
    	return 0;
    }
    You're welcome to tell me how stupid I am and that I should know better. That's OK. But please help. Thanks : )
    mpliam

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How can I identify which of two subclassed controls the mouse is over?

    How about using control IDs?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How can I identify which of two subclassed controls the mouse is over?

    Quote Originally Posted by Mike Pliam View Post
    I need some method in the derived class to determine which control is being manipulated.

    You're welcome to tell me how stupid I am and that I should know better. That's OK. But please help. Thanks : )
    The whole point about message reflection is to notify the particular object about being manipulated to let the one do all required processing on its own. If under the circumstances your control needs to identify itself, afraid that's something wrong with the design of objects relationship. Anyway, as Victor suggested, this self identification can be done based on the control's ID. See the sample for that.
    Attached Files Attached Files
    Best regards,
    Igor

  4. #4
    Join Date
    Oct 2011
    Posts
    97

    Re: How can I identify which of two subclassed controls the mouse is over?

    Quote Originally Posted by Mike Pliam View Post
    I need some method in the derived class to determine which control is being manipulated.
    Then why not put a method in there? C++ supports multiple inheritance, so why not create a base class with a pure virtual method? Then you can call the method no matter which control it is and get the necessary response.

    Admittedly, this is somewhat an abuse of the class system, especially since there's only 2 controls, but I just wanted to point out that it's a possibility (especially if you add more controls).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured