An CALLBACK function could be member of an class ?
I am trying to use CDC::EnumObjects method just like this:
Code:
CDC dc;
dc.Attach(hDC);
dc.EnumObjects(OBJ_PEN, EnumObjectHandler, 0);
from here: https://msdn.microsoft.com/en-us/lib...or=-2147217396
and
Code:
BOOL CALLBACK EnumObjectHandler(LPVOID lpLogObject, LPARAM lpData)
{
return TRUE;
}
and it works.
My question is, can I move this function, in an class, as member function (method), but, most important, without changing as static ? Yes, I can put here my trials, but none of them had worked ... perhaps it is not possible what I am trying to do :)
Re: An CALLBACK function could be member of an class ?
If you move EnumObjectHandler() into a class as a member function then it needs to be static.
Re: An CALLBACK function could be member of an class ?
Quote:
Originally Posted by
mesajflaviu
I am trying to use CDC::EnumObjects method just like this:
Code:
CDC dc;
dc.Attach(hDC);
dc.EnumObjects(OBJ_PEN, EnumObjectHandler, 0);
from here:
https://msdn.microsoft.com/en-us/lib...or=-2147217396
and
Code:
BOOL CALLBACK EnumObjectHandler(LPVOID lpLogObject, LPARAM lpData)
{
return TRUE;
}
and it works.
My question is, can I move this function, in an class, as member function (method), but, most important, without changing as static ? Yes, I can put here my trials, but none of them had worked ... perhaps it is not possible what I am trying to do :)
Yes, you can.
for example, you can pass the this pointer as a lpData parameter.
Then from within this static Callback function you'll be able to call any of your class members using the passed in this pointer.
Re: An CALLBACK function could be member of an class ?
Good point, my goal is to move the callback function inside of an class, as a member method ... and I understand that there is no alternative but declare as static. Sometime this solution could be a workaround. Kindly thank you all of you !
Re: An CALLBACK function could be member of an class ?
An alternative to member functions is to use lambdas, introduced by C++11.
Example:
Code:
void CEnumDialog::EnumPens(CDC& dc)
{
dc.EnumObjects(OBJ_PEN,
[](LPVOID lpLogObject, LPARAM lParam)->BOOL
{
LOGPEN* pPen = (LOGPEN*)lpLogObject;
CEnumDialog* pDlg = (CEnumDialog*)lParam;
// Do something cool here...
return TRUE; // ...then continue the enumeration.
}, (LPARAM)this);
}
See also Using Lambdas in MFC Applications – Replacing Callback Functions