dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    224

    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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,221

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,144

    Re: An CALLBACK function could be member of an class ?

    Quote Originally Posted by mesajflaviu View Post
    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.
    Last edited by VictorN; February 1st, 2018 at 01:49 AM.
    Victor Nijegorodov

  4. #4
    Join Date
    Jan 2009
    Posts
    224

    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 !

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,195

    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
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.