How to subclass Windows in Windows Forms by using Visual C# .NET or Visual C# 2005 (815775)
The information in this article applies to:
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# 2005, Express Edition
For a Microsoft Visual Basic .NET version of this
article, see
311317. IN THIS TASKSUMMARYThis step-by-step article describes several subclassing
techniques that you can use in Visual C# .NET or Visual C# 2005. back to the topSubclass 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.
- In the Name text box, type
SCTextBox.cs, and then click Open.
- Add the following statement to the top of the code in
SCTextBox.cs:
using System.Windows.Forms; - 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);
}
} - Drag a TextBox control from the Toolbox to Form1.cs.
- Edit the Form1.cs code as follows:
- Expand the region that is marked in
the code as follows:
Windows Form Designer generated code - 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();
- Save the project. On the Debug menu, click
Start.
- Type some sample text in the text box.
- Select the text, and then press CTRL+X. Notice that this key combination does not delete the text.
- Right-click in the text box, and then click
Cut. Notice that the text is deleted.
back to the
topSubclass a Form - 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 Studio 2005, click Visual C# under Project Types. - In the Name text box, type
SubclassingDemo.
- Edit the code for Form1.cs as follows:
- Add the following statement to the top of the Form1 code:
using System.Windows.Forms; - Expand the region that is marked as follows:
Windows Form Designer generated code
- 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
topSubclass Any HWND- 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.
- In the Name text box, type
SubclassHWND.cs, and then click
Open.
- 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);
}
} - 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
topTroubleshootThe 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 topREFERENCES 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
Modification Type: | Major | Last Reviewed: | 1/6/2006 |
---|
Keywords: | kbCtrl kbControl kbWindowsForms kbWndwProc kbForms kbHOWTOmaster KB815775 kbAudDeveloper |
---|
|