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 : )
Re: How can I identify which of two subclassed controls the mouse is over?
Originally Posted by Mike Pliam
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.
Re: How can I identify which of two subclassed controls the mouse is over?
Originally Posted by Mike Pliam
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).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.