Microsoft KB Archive/815775

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

Article ID: 815775

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





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

IN THIS TASK

 * SUMMARY
 * Subclass a Control
 * Subclass a Form
 * Subclass Any HWND
 * Troubleshoot
 * REFERENCES



SUMMARY
This step-by-step article describes several subclassing techniques that you can use in Visual C# .NET or Visual C# 2005.

back to the top

Subclass a Control
 To create a new Visual C# .NET or Visual C# 2005 Windows Application that is named SubclassingDemo, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Projects. Click Visual C# Projects under Project Types, and then click Windows Application under Templates.

Note In Visual C# 2005, click Visual C# under Project Types. In the Name text box, type SubclassingDemo.  To add a new Class module to the project, click Add Class on the Project menu.</li> In the Name text box, type SCTextBox.cs, and then click Open.</li>  Add the following statement to the top of the code in SCTextBox.cs: using System.Windows.Forms; </li>  Override the inherited WndProc. To do this, replace the existing SCTextBox class code with the following code: public class SCTextBox : TextBox {   private const int  WM_CHAR = 0x102; protected override void WndProc(ref Message m)     { //See if the CTRL key is being pressed. if ( SCTextBox.ModifierKeys.CompareTo(Keys.Control) == 0) {                                                                                                                                                      switch(m.Msg) {                case WM_CHAR: // Disable CTRL+X. switch(m.WParam.ToInt32) {                          case 24 ://X = 24th letter of alphabet break; // Do nothing here to disable the default message handling. default: //Make sure that you pass unhandled messages back to the default message handler. base.WndProc( ref m); break; }                       break; default: //Make sure that you pass unhandled messages back to the default message handler. base.WndProc(ref m); break; }             }           else //Make sure that you pass unhandled messages back to the default message handler. base.WndProc(ref m); } } </li> Drag a TextBox control from the Toolbox to Form1.cs.</li> Edit the Form1.cs code as follows:   Expand the region that is marked in the code as follows: Windows Form Designer generated code </li>  Replace references to the standard TextBox control with references to the new SCTextBox class. To do this, replace all occurrences of the TextBox class with SCTextBox in the code that is generated.

For example: //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></ol> </li> Save the project. On the Debug menu, click Start.</li> Type some sample text in the text box.</li> Select the text, and then press CTRL+X. Notice that this key combination does not delete the text.</li> Right-click in the text box, and then click Cut. Notice that the text is deleted.</li></ol>

back to the top

Subclass a Form
 To create a new Visual C# .NET or Visual C# 2005 Windows Application that is named SubclassingDemo, follow these steps:  <li>Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> <li>On the File menu, point to New, and then click Projects.</li> <li>Click Visual C# Projects under Project Types, and then click Windows Application under Templates.

Note In Visual Studio 2005, click Visual C# under Project Types.</li> <li>In the Name text box, type SubclassingDemo .</li></ol> </li> <li>Edit the code for Form1.cs as follows: <ol style="list-style-type: lower-alpha;"> <li> Add the following statement to the top of the Form1 code: using System.Windows.Forms; </li> <li> Expand the region that is marked as follows: Windows Form Designer generated code </li> <li> Override the inherited WndProc. To do this, add the override void WndProc procedure to the Form1 class as follows: public class Form1 : System.Windows.Forms.Form {  #region Windows Form Designer generated code protected override void WndProc(ref System.Windows.Forms.Message m)  { // Perform whatever custom processing you must have for this message. System.Diagnostics.Debug.WriteLine(m.ToString); //Forward message to base WndProc. base.WndProc(ref m); }     #endregion } </li></ol> </li></ol>

back to the top

Subclass Any HWND
<ol> <li>Add a new Class module that is named SubclassHWND.cs to the Visual C# .NET or Visual C# 2005 application. To do this, click Add Class on the Project menu.</li> <li>In the Name text box, type SubclassHWND.cs, and then click Open.</li> <li> Replace the SubclassHWND class code with the following code: public class SubclassHWND : NativeWindow {  protected override void WndProc(ref Message m)    { // Perform whatever custom processing you must have for this message System.Diagnostics.Debug.WriteLine(m.ToString); // forward message to base WndProc base.WndProc(ref m); } } </li> <li> To demonstrate its use, add the following code to the Load event of Form1: SubclassHWND s = new SubclassHWND; s.AssignHandle(this.Handle); //Now s should be listening to the messages of the form. </li></ol>

back to the top

Troubleshoot
The global hook must add itself in multiple processes These processes require a valid, consistent function to call into. Managed code has no concept of a consistent value for function pointers because these function pointers are proxies that are built on the fly.

back to the top

<div class="references_section">