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 .

IN THIS TASK

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

  1. To create a new Visual C# .NET or Visual C# 2005 Windows Application that is named SubclassingDemo, follow these steps:
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
    2. On the File menu, point to
      New, and then click Projects.
    3. 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.
    4. In the Name text box, type
      SubclassingDemo.
  2. To add a new Class module to the project, click Add Class on the Project menu.
  3. In the Name text box, type
    SCTextBox.cs, and then click Open.
  4. Add the following statement to the top of the code in SCTextBox.cs:
    using System.Windows.Forms;
  5. 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);
    }
    }
  6. Drag a TextBox control from the Toolbox to Form1.cs.
  7. Edit the Form1.cs code as follows:
    1. Expand the region that is marked in the code as follows:
      Windows Form Designer generated code 
    2. 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();
  8. Save the project. On the Debug menu, click
    Start.
  9. Type some sample text in the text box.
  10. Select the text, and then press CTRL+X. Notice that this key combination does not delete the text.
  11. Right-click in the text box, and then click
    Cut. Notice that the text is deleted.
back to the top

Subclass a Form

  1. To create a new Visual C# .NET or Visual C# 2005 Windows Application that is named SubclassingDemo, follow these steps:
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
    2. On the File menu, point to
      New, and then click Projects.
    3. 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.
    4. In the Name text box, type
      SubclassingDemo.
  2. Edit the code for Form1.cs as follows:
    1. Add the following statement to the top of the Form1 code:
      using System.Windows.Forms;
    2. Expand the region that is marked as follows:
       Windows Form Designer generated code
    3. 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
      }
back to the top

Subclass Any HWND

  1. 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.
  2. In the Name text box, type
    SubclassHWND.cs, and then click
    Open.
  3. 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);
    }
    }
  4. 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.
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

References

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
320584 HOW TO: Trap Keystrokes in .NET Controls by Using Visual C# .NET
back to the top
Propriedades

ID do Artigo: 815775 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários