How to subclass windows in Windows Forms by using Visual C++ .NET or Visual C++ 2005

For a Microsoft Visual Basic .NET version of this article, see
311317 .
For a Microsoft Visual C# .NET version of this article, see
815775 .

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System::ComponentModel
  • System::Collections
  • System::Windows::Forms

IN THIS TASK

Summary

This step-by-step article describes several subclassing techniques that you can use in Microsoft Visual C++ .NET.

This article describes how to subclass a TextBox control class. The WndProc method of the TextBox control class is overridden to provide different functionality. This article also describes how to subclass a Form class by overriding the WndProc method of the Form class.

This article also describes how to subclass any HWND control class. A new class is derived from the NativeWindow class, and the WndProc method of the base class is overridden to subclass the NativeWindow class.

Back to the top

Subclass a control

  1. To create a Visual C++ .NET Windows Application project that is named SubclassDemo, follow these steps:
    1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
    2. On the File menu, point to
      New, and then click Project. The New Project dialog box appears.
    3. Under Project Types, click
      Visual C++ Projects.

      Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++.
    4. Under Templates, click Windows Forms Application (.NET).

      Note In Visual Studio 2005, Windows Forms Application (.NET) is changed to Windows Forms Application.
    5. In the Name box, type
      SubclassDemo, and then click
      OK.

      By default, the Form1 form is created.
  2. To add a new class to the SubclassDemo project, follow these steps:
    1. On the Project menu, click Add Class. The Add Class - SubclassDemo dialog box appears.
    2. Under Categories, click
      Generic.

      Note In Visual Studio 2005, Generic is changed to C++.
    3. Under Templates, click Generic C++ Class.

      Note In Visual Studio 2005, Generic C++ Class is changed to C++ Class.
    4. Click Open. The Generic C++ Class Wizard - SubclassDemo dialog box appears.
    5. In the Class name box, type
      SCTextBox, and then click
      Finish.
  3. Add the following statement at the top of the code in the SCTextBox.h file:
    using namespace System::Windows::Forms;
  4. Make the SCTextBox class a public managed C++ class that derives from the standard TextBox class. To do this, change the definition of the SCTextBox class by using the following line of code:
    public __gc class SCTextBox : public TextBox
  5. Override the inherited WndProc method in the new subclass. First, trap the CTRL key. Then, trap the X key, and then disable the CTRL+X keyboard shortcut that you use to delete some text and to copy this text to the Clipboard. To do this, replace the existing code for the SCTextBox class with the following code:
    public __gc class SCTextBox : public TextBox
    {
    public:
    SCTextBox(void);
    ~SCTextBox(void);

    private:
    static const int WM_CHAR = 0x102;
    protected:
    void WndProc(Message *m)
    {
    // Check if the CTRL key is being pressed.
    if ( SCTextBox::ModifierKeys == Keys::Control )
    {
    switch(m->Msg)
    {
    case WM_CHAR:
    // Disable the CTRL+X key combination.
    switch(m->WParam.ToInt32())
    {
    // Check for "X" (the twenty-fourth letter of the alphabet).
    case 24 :
    break;
    // Do nothing here to disable the default message handling.
    default:
    // Make sure that you pass unhandled messages back to the default message handler.
    TextBox::WndProc(m);
    break;
    }
    break;
    default:
    // Make sure that you pass unhandled messages back to the default message handler.
    TextBox::WndProc(m);
    break;
    }
    }
    else
    // Make sure that you pass unhandled messages back to the default message handler.
    TextBox::WndProc(m);
    }
    };
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:

    1. Click Project, and then click <ProjectName> Properties.



      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click

      General.

    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the

      Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
    These steps apply to the whole article.
  6. Open the Form1 form in Design view, and then add a
    TextBox control to Form1.
  7. Open the Form1.h file, and then add the following code after the #pragma directive:
    #include "SCTextBox.h"
  8. Replace references to the standard TextBox control with references to the SCTextBox control that you have added. To do this, replace all occurrences of TextBox with SCTextBox in your code. For example, use the following code:
    // private: System::Windows::Forms::TextBox *  textBox1;
    // is now
    private: SCTextBox * textBox1;
    // this->textBox1 = new System::Windows::Forms::TextBox();
    // is now
    this->textBox1 = new SCTextBox();
  9. Save the project, and then press F5 to run the application in debug mode.
  10. Type some sample text in the text box that is located on the Form1 form.
  11. Select the text that you typed in step 10, and then press CTRL+X. Notice that the key combination does not delete the selected text.
  12. Right-click in the text box, and then click
    Cut. Notice that the selected text is deleted.
Back to the top

Subclass a form

  1. To create a Visual C++ .NET or Visual C++ 2005 Windows Application project that is named SubclassingDemo, follow these steps:
    1. Start Visual Studio .NET 2003 or Visual Studio 2005.
    2. On the File menu, point to
      New, and then click Project. The New Project dialog box appears.
    3. Under Project Types, click
      Visual C++ Projects.

      Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++.
    4. Under Templates, click Windows Forms Application (.NET).

      Note In Visual Studio 2005, Windows Forms Application (.NET) is changed to Windows Forms Application.
    5. In the Name box, type
      SubclassingDemo, and then click
      OK.

      By default, the Form1 form is created.
  2. Override the inherited WndProc method. To do this, add the void WndProc procedure to the Form1 class as follows:
    protected:
    void WndProc(System::Windows::Forms::Message *m)
    {
    // Perform the custom processing that you require for this message.
    System::Diagnostics::Debug::WriteLine(m->ToString(), S"Form1.WndProc");
    // Forward the message to the WndProc procedure of the base class.
    Form::WndProc(m);
    }
  3. Make sure that the Output window is open or press CTRL+ALT+O to open the Output window.
  4. Save the project, and then press F5 to run the application in debug mode.
  5. Move the mouse pointer over the Form1form.

    You notice Windows messages in the Output window for every action that you perform on the Form1 form. The subclassed
    Form1 form displays these messages.
Back to the top

Subclass any HWND handle

  1. Add a new class that is named SubclassHWND to the SubclassingDemo project. To do this, follow these steps:
    1. On the Project menu, click Add Class. The Add Class - SubclassingDemo dialog box appears.
    2. Under Categories, click
      Generic.

      Note In Visual Studio 2005, Generic is changed to C++.
    3. Under Templates, click Generic C++ Class.

      Note In Visual Studio 2005,Generic C++ Class is changed to C++ Class.
    4. Click Open. The Generic C++ Class Wizard - SubclassingDemo dialog box appears.
    5. In the Class name box, type
      SubclassHWND, and then click
      Finish.
  2. Add the following statement at the top of the code in the SubclassHWND.h file:
    using namespace System::Windows::Forms;
  3. Replace the existing code for the SubclassHWND class with the following code:
    public __gc class SubclassHWND : public NativeWindow
    {
    public:
    SubclassHWND(void);
    ~SubclassHWND(void);

    protected:
    void WndProc(Message *m)
    {
    // Perform the custom processing that you require for this message.
    System::Diagnostics::Debug::WriteLine(m->ToString(),S"SubclassHWND");
    // Forward the message to the WndProc procedure of the base class.
    NativeWindow::WndProc(m);
    }
    };
  4. Open the Form1.h file, and then add the following code after the #pragma directive:
    #include "SubclassHWND.h"
  5. To use the SubclassHWND class, add the following code to the Load event of the Form1 form:
    SubclassHWND * s = new SubclassHWND();
    s->AssignHandle(this->Handle);
    // The s subclass now listens to the messages of the form.
  6. Make sure that the Output window is open or press CTRL+ALT+O to open the Output window.
  7. Save the project, and then press F5 to run the application in debug mode.
  8. Move the mouse pointer over the Form1form.

    You notice Windows messages in the Output window for every action that you perform on the Form1 form. Your SubClassHWND object receives these messages.
Back to the top

References

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

816190 How to trap keystrokes in .NET controls by using Visual C++ .NET

Back to the top
Propriedades

ID do Artigo: 815774 - Última Revisão: 7 de dez de 2007 - Revisão: 1

Comentários