By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How to cast a pointer of a member function? (repost)

P: n/a
Hi,

I have a CWindowImpl derived class , see below at ***, that needs to
subclass simple controls like textboxes.

like this...: m_MyControl.SubclassWindow(GetDlgItem(IDC_MyContro l).m_hWnd);

==> problem here

m_MyControl.SetMouseHover(
reinterpret_cast<CMouseHover::ONMOUSEHOVER>(&this->myMemberFunction)
);

The VC 8 Compiler will return a casting error.
What can I do to fix this?

Thanks!

***
// for Subclassing controls so we have support for OnMouseHover and
OnMouseLeave
class CMouseHover: public CWindowImpl<CMouseHover>
{
public:
CMouseHover(): m_bIsMouseOver(false) , OnMouseHover(NULL),
OnMouseLeave(NULL)
{
}

//declare event signature
typedef void (CALLBACK* ONMOUSEHOVER)(int nFlags, int ID, POINT pt);
typedef void (CALLBACK* ONMOUSELEAVE)(int ID);

void SetMouseHover(ONMOUSEHOVER ptr) throw()
{
OnMouseHover = ptr;
}
ONMOUSEHOVER OnMouseHover;
ONMOUSELEAVE OnMouseLeave;
private:

bool m_bIsMouseOver;
BEGIN_MSG_MAP(CMouseHover)
MESSAGE_HANDLER(WM_MOUSEHOVER, OnMouseHover_System)
MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave_System)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove_System)
END_MSG_MAP()

LRESULT OnMouseHover_System(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
throw()
{
if (OnMouseHover != NULL)
{
POINT pt ={GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
UINT nFlags = (UINT)wParam;
//signal our delegate
OnMouseHover(nFlags, GetDlgCtrlID(), pt);
}
return FALSE;
}

LRESULT OnMouseLeave_System(UINT, WPARAM, LPARAM, BOOL&)
{
if (OnMouseLeave != NULL)
{
TRACKMOUSEEVENT evntTrack;
evntTrack.cbSize = sizeof(TRACKMOUSEEVENT);
evntTrack.hwndTrack = m_hWnd;
evntTrack.dwHoverTime = HOVER_DEFAULT;
evntTrack.dwFlags = TME_CANCEL | TME_LEAVE | TME_HOVER;
TrackMouseEvent(&evntTrack);
m_bIsMouseOver = false;
//signal delegate
OnMouseLeave(GetDlgCtrlID());
}
return 0;
}
LRESULT OnMouseMove_System(UINT, WPARAM, LPARAM, BOOL&)
{
ATLASSERT(::IsWindow(m_hWnd));
if (!m_bIsMouseOver)
{
TRACKMOUSEEVENT evntTrack;
evntTrack.cbSize = sizeof(TRACKMOUSEEVENT);
evntTrack.hwndTrack = m_hWnd;
evntTrack.dwHoverTime = HOVER_DEFAULT;
evntTrack.dwFlags = TME_LEAVE | TME_HOVER;
if (TrackMouseEvent(&evntTrack) == TRUE)
m_bIsMouseOver = true;
}
return FALSE;
}
};

Mar 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a CWindowImpl derived class , see below at ***, that needs to
subclass simple controls like textboxes.

like this...:
m_MyControl.SubclassWindow(GetDlgItem(IDC_MyContro l).m_hWnd);

==> problem here

m_MyControl.SetMouseHover(
reinterpret_cast<CMouseHover::ONMOUSEHOVER>(&this->myMemberFunction)
);


What's the inheritance relationship between *this and CMouseHover at this
point?

-cd
Mar 17 '06 #2

P: n/a

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uE**************@TK2MSFTNGP09.phx.gbl...
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a CWindowImpl derived class , see below at ***, that needs to
subclass simple controls like textboxes.

like this...:
m_MyControl.SubclassWindow(GetDlgItem(IDC_MyContro l).m_hWnd);

==> problem here

m_MyControl.SetMouseHover(
reinterpret_cast<CMouseHover::ONMOUSEHOVER>(&this->myMemberFunction)
);


What's the inheritance relationship between *this and CMouseHover at this
point?


This is a pointer to a dialog, the active dialog class.

m_MyControl (an instance of CMouseHover) is the control that has been
subclassed.
myMemberFunction is a single function (obviously from the dialog) that -all-
controls would delegate control to as soon as the mouse hovers them.

Making myMemberfunction static is not the solution.
Mar 17 '06 #3

P: n/a
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:u4**************@TK2MSFTNGP11.phx.gbl...

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uE**************@TK2MSFTNGP09.phx.gbl...
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a CWindowImpl derived class , see below at ***, that needs to
subclass simple controls like textboxes.

like this...:
m_MyControl.SubclassWindow(GetDlgItem(IDC_MyContro l).m_hWnd);

==> problem here

m_MyControl.SetMouseHover(
reinterpret_cast<CMouseHover::ONMOUSEHOVER>(&this->myMemberFunction)
);


What's the inheritance relationship between *this and CMouseHover at this
point?


This is a pointer to a dialog, the active dialog class.

m_MyControl (an instance of CMouseHover) is the control that has been
subclassed.
myMemberFunction is a single function (obviously from the dialog)
that -all- controls would delegate control to as soon as the mouse hovers
them.

Making myMemberfunction static is not the solution.


You didn't directly answer my question, but I think I can infer that there's
no inheritance relationship between *this and CMouseHover. In that case,
you can't do that cast - you need to change your design. You simply can't
cast a pointer to member of class A to pointer to member of class B unless
there's an inheritance relationship between A and B.

-cd
Mar 17 '06 #4

P: n/a

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2***************@TK2MSFTNGP10.phx.gbl...
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:u4**************@TK2MSFTNGP11.phx.gbl...

Making myMemberfunction static is not the solution.


You didn't directly answer my question, but I think I can infer that
there's no inheritance relationship between *this and CMouseHover. In
that case, you can't do that cast - you need to change your design. You
simply can't cast a pointer to member of class A to pointer to member of
class B unless there's an inheritance relationship between A and B.


Right.

What I was trying to do looks like how delegates in .NET work. I just
declare a typedef (signature) a method and theoretically, casting pointers
should be possible (just as GetProcAddress, sort of).

A previous post (which nobody answered) contained an article about C++ that
you could use delegates (unmanaged), but that article, contains to much
noise... (I don't understand it).

Mar 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.