CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2007
    Posts
    143

    Wink SubclassWindow???

    Hello Guys,
    what is SubclassWindow with an example ?
    thanks in advance

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: SubclassWindow???

    Hi,

    according to the MSDN about SubclassWindow:
    Call this member function to "dynamically subclass" a window and attach it to this CWnd object. When a window is dynamically subclassed, windows messages will route through the CWnd’s message map and call message handlers in the CWnd’s class first. Messages that are passed to the base class will be passed to the default message handler in the window.
    So SubclassWindow is a member of CWnd (and all CWnd derived classes).

    This subclassing is a mechanism to derive all properties from a MS-Windows window to your "overridden" class (in terms of MFC and not of C++ OOP).

    I often use this to overwrite dialogue controls.
    For instance, if you want to have a listbox that should do a certain - non standard - action if you click it with the mouse, follow these steps:

    Create a dialog and insert a "normal" listbox.

    Use the class wizzard to create your own CMyListBox class (derived publicly from CListBox).

    Use the class wizzard to create a OnLButtonDown() handler in CMyListBox and an OnInitDialog() handler in the dialog.

    Add a member of CMyListBox to the dialog class.

    Inside CMyDialog::OnInitDialog() call:
    PHP Code:
      m_MyListBox.SubclassDlgItem(IDC_LIST1); // use the ID of your list box 
    That should work...

    Note: I explained SubclassDlgItem since it is easier in this case.
    To do the same job with SubclassWindow (which is more general and not only for dialogue controls) write the same as:
    PHP Code:
    CWndpTheListBox GetDlgItem(IDC_LIST1);
    m_MyListBox.SubclassWindow(pTheListBox->GetSafeHwnd()); 
    Many examples can be found by searching the www.

    With regards
    Programartist

  3. #3
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: SubclassWindow???

    First, let me clarify:
    I know nomenclature can be confusing. Windows class has nothing to do with MFC/C++ classes.
    To create window that is not Windows common control one must register own window class filling WNDCLASS or WNDCLASSEX structure and call RegisterClass or RegisterClassEx.
    Window class not only sets behavior of the window but it sets windows procedure that handles all Windows messages.

    If you want to use Window controls and control behavior, you would have to subclass this control. What it means,you replace control’s window procedure with your own to handle some messages or call original procedure.

    When MFC creates window it passes own procedure address. For Window controls it subclasses each newly created controls.

    This attaches control to an MFC object so you can use MFC wrappers.

    Asigning variable to a dialog’s control transparently subclasses this control with MFC procedure. This attaches control (window) to a class of your chice (MFC or derived) giving you ability to handle control’s messages.

    For exapmple: Dialog butto’s window class name is BUTTON. You could use any MFC class to attach button window to the object of this class; however, CButton is most appropriate, since it was specifically designed to suppoer button functionality.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: SubclassWindow???

    I'm just trying to understand what subclassing is so correct me if I'm wrong:

    is subclassing a special way to derive my own class not just from a "dead" base class like from CListBox in @ProgramArtist's example but from an existing "living" instance of this base class so that I can extend or modify it's behavior? in this context, does it mean that I can subclass a part (a control) of another program?

    or is subclassing just a way to customize predefined controls?
    Last edited by memeloo; February 6th, 2010 at 02:59 AM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: SubclassWindow???

    Quote Originally Posted by memeloo View Post
    is subclassing a special way to derive my own class not just from a "dead" base class like from CListBox
    I am not sure if I understand CListBox is C++ class and is not dead. It is a wrapper for a Windows list box control.
    I thing you can find an answer in my previous post. Please read it and let me know what is not clear and I will try to explain it further.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: SubclassWindow???

    Quote Originally Posted by JohnCz View Post
    I thing you can find an answer in my previous post. Please read it and let me know what is not clear and I will try to explain it further.
    indeed, your post answers my question. I don't know why I didn't see it.

    however I must translate it in to my world of understanding
    in this world I see subclassing as a special method of deriving a class from an instance of another class. this is what I mean that the class is living as an object, then I subclass it and take over the control of what it does? unlike deriving from another class and instantiating my customized one.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  7. #7
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: SubclassWindow???

    Quote Originally Posted by memeloo View Post
    in this world I see subclassing as a special method of deriving a class . . .
    Quote Originally Posted by me earlier View Post
    First, let me clarify:
    . . . [b]Windows class has nothing to do with MFC/C++ classes.[b]
    Quote Originally Posted by memeloo View Post
    in this world I see subclassing as a special method of deriving a class from an instance of another class.
    Short answer: NO.
    Long answer:
    You cannot derive class from another class’ instance. Class is a recipe telling how to create object and defines object’s behavior. In C++ you can write one recipe and use it as a base to create another one. It is called deriving one class from a base class. Word SUBCLASSING has no meaning in C++ (or at least did not). Nowadays, people use nomenclature from other languages and call deriving subclassing; it creates confusion and it should not be used. The same applies to using parent and child class. Let’s us use old good naming convention.
    Quote Originally Posted by memeloo View Post
    then I subclass it and take over the control of what it does? unlike deriving from another class and instantiating my customized one.
    I do not quite understand this part but I assume that you are still confusing C++ class with Window class.
    For a moment forget about C++ and MFC. There was a time that programming Windows meant using native Windows APIs.
    As I have already mentioned, in order to create window that is not a predefined Windows control window class had to be registered by calling RegisterClass API passing various parameters telling system about window behavior and what is most important, passing pointer to a function that serves as window procedure processing all messages delivered to a window.
    IF you wanted to use one of Windows predefined control (list control for example), you had create window first and subclass it. That means you had to replace list control’s window procedure with a pointer to your own procedure, taking over control how messages are handled. It is achieved by calling SetClassLong using GCL_WNDPROC index.
    Now let’s introduce C++/MFC again. MFC does exactly the same thing as I described in one of the wrappers and it is transparent for a programmer.

    In conclusion: you have to distinguish C++ class that defines object from Windows class that tells Windows OS how to create window and what function processes messages.
    One more thing you most likely are missing. C++/MFC class is not a window; never. It is a C++ object that is derived from CWnd class and encapsulates window handle (m_hWnd). Instantiation of the object does not create window. Window is attached to an MFC object by calling Create, Attach or Subclass window.
    Create member creates window and attaches it to an object, Attach or SubclassWindow uses already existing window and attaches it to an object.
    In all cases, m_hWnd is initialized to a handle of new or existing window.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured