Microsoft KB Archive/815774

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

Article ID: 815774

Article Last Modified on 1/17/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







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
 * Subclass a control
 * Subclass a form
 * Subclass any HWND handle
 * REFERENCES



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

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

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

By default, the Form1 form is created.</li></ol> </li> To add a new class to the SubclassDemo project, follow these steps: <ol style="list-style-type: lower-alpha;"> On the Project menu, click Add Class. The Add Class - SubclassDemo dialog box appears.</li> Under Categories, click Generic.

Note In Visual Studio 2005, Generic is changed to C++.</li> Under Templates, click Generic C++ Class.

Note In Visual Studio 2005, Generic C++ Class is changed to C++ Class.</li> Click Open. The Generic C++ Class Wizard - SubclassDemo dialog box appears.</li> In the Class name box, type SCTextBox, and then click Finish.</li></ol> </li>  Add the following statement at the top of the code in the SCTextBox.h file: using namespace System::Windows::Forms; </li>  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 </li>  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 &quot;X&quot; (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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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. </li> Open the Form1 form in Design view, and then add a TextBox control to Form1.</li>  Open the Form1.h file, and then add the following code after the #pragma directive:  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; </li> <li>Save the project, and then press F5 to run the application in debug mode.</li> <li>Type some sample text in the text box that is located on the Form1 form.</li> <li>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.</li> <li>Right-click in the text box, and then click Cut. Notice that the selected text is deleted.</li></ol>
 * 1) include &quot;SCTextBox.h&quot; </li>

Back to the top

Subclass a form
<ol> <li>To create a Visual C++ .NET or Visual C++ 2005 Windows Application project that is named SubclassingDemo, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Start Visual Studio .NET 2003 or Visual Studio 2005.</li> <li>On the File menu, point to New, and then click Project. The New Project dialog box appears.</li> <li>Under Project Types, click Visual C++ Projects.

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

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

By default, the Form1 form is created.</li></ol> </li> <li> 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&quot;Form1.WndProc&quot;); // Forward the message to the WndProc procedure of the base class. Form::WndProc(m); } </li> <li>Make sure that the Output window is open or press CTRL+ALT+O to open the Output window.</li> <li>Save the project, and then press F5 to run the application in debug mode.</li> <li>Move the mouse pointer over the Form1 form.

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.</li></ol>

Back to the top

Subclass any HWND handle
<ol> <li>Add a new class that is named SubclassHWND to the SubclassingDemo project. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Project menu, click Add Class. The Add Class - SubclassingDemo dialog box appears.</li> <li>Under Categories, click Generic.

Note In Visual Studio 2005, Generic is changed to C++.</li> <li>Under Templates, click Generic C++ Class.

Note In Visual Studio 2005,Generic C++ Class is changed to C++ Class.</li> <li>Click Open. The Generic C++ Class Wizard - SubclassingDemo dialog box appears.</li> <li>In the Class name box, type SubclassHWND, and then click Finish.</li></ol> </li> <li> Add the following statement at the top of the code in the SubclassHWND.h file: using namespace System::Windows::Forms; </li> <li> 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&quot;SubclassHWND&quot;); // Forward the message to the WndProc procedure of the base class. NativeWindow::WndProc(m); } }; </li> <li> Open the Form1.h file, and then add the following code after the #pragma directive: <li> 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. </li> <li>Make sure that the Output window is open or press CTRL+ALT+O to open the Output window.</li> <li>Save the project, and then press F5 to run the application in debug mode.</li> <li>Move the mouse pointer over the Form1 form.
 * 1) include &quot;SubclassHWND.h&quot; </li>

You notice Windows messages in the Output window for every action that you perform on the Form1 form. Your SubClassHWND object receives these messages.</li></ol>

Back to the top

<div class="references_section">