Subclassing controls in a class

This is a discussion on Subclassing controls in a class within the Windows Programming forums, part of the Platform Specific Boards category; I'm having a similar problem to the one that originally stumped me with regards to defining a non-static windows procedure ...

  1. #1
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87

    Subclassing controls in a class

    I'm having a similar problem to the one that originally stumped me with regards to defining a non-static windows procedure method within a class. This time the problem relates to Subclassing.

    I am attempting a similar solution, i.e. when I create the control, I am passing the "this" pointer as the LPARAM, so I can avoid having a purely static procedure for the control.

    The problem I am having is that the SetWindowLong API function wants a LONG as the third paramater, but I am unable to cast the method to a long.
    Code:
    OldCtrl = (WNDPROC)SetWindowLong(buttonCtrl, GWL_WNDPROC, (LONG)ButtonProc);
    i.e. the above is generates a compiler error.
    'type cast' : cannot convert from 'LRESULT (__stdcall CApp::* )(HWND,UINT,WPARAM,LPARAM)' to 'LONG'
    Any ideas?
    Visual C++ .net
    Windows XP profesional

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think what you want is SetWindowLongPtr which is defined as

    LONG_PTR SetWindowLongPtr(
    HWND hWnd,
    int nIndex,
    LONG_PTR dwNewLong
    );

  3. #3
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Nah, it gives you the same error only saying it can't convert to LONG_PTR.
    Visual C++ .net
    Windows XP profesional

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Are you sure? Your supposed to be casting to a LONG_PTR here not a long. C++ allows you to cast to any point time with the reservation that you have a pointer type.

  5. #5
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    It is impossible to do what you're trying to do.

    No amount of casting is going to work.

  6. #6
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Okay - so it is best to use a global control procedure?
    Visual C++ .net
    Windows XP profesional

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Use one of the many solutions provided here.

    gg

  8. #8
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Thanks Codeplug, but I have read those and have tried to implement the approach that I feel most comfortable with and was completely snookered by the fact you can't cast a class method to a LONG or LONG_PTR. This is most likely a problem of lack of knowledge with C++ (duh!).

    Let me try another approach. I don't need any code - just an opinion, I know there are always many different ways to achieve something, and I have acheived this using at least two methods that were listed in the other post. But I don't like both of them, because they require shed-loads of static class members.

    How do those that program using OOP constructs generally subclass their controls within a class, while trying to adhere to OOP principals (i.e. data encapsulation, polymorphism etc)?
    Visual C++ .net
    Windows XP profesional

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    They use one of the options you've most likely already read about in previous information.
    Perhaps a better understanding function pointers and how they relate to class member functions will help.

    gg

  10. #10
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    It probably will. Thanks I will read.
    Visual C++ .net
    Windows XP profesional

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21