How to subclass windows in Windows Forms by using Visual C++ .NET or Visual C++ 2005 (815774)
The information in this article applies to:
- Microsoft .NET Framework 1.1
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
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 TASKSUMMARYThis 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
topSubclass 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.
- To add a new class to the SubclassDemo project, follow
these steps:
- On the Project menu, click Add
Class. The Add Class - SubclassDemo dialog box
appears.
- Under Categories, click
Generic.
Note In Visual Studio 2005, Generic is changed to C++. - Under Templates, click Generic
C++ Class.
Note In Visual Studio 2005, Generic
C++ Class is changed to C++ Class. - Click Open. The Generic C++
Class Wizard - SubclassDemo dialog box appears.
- In the Class name box, type
SCTextBox, and then click
Finish.
- Add the following statement at the top of the code in the
SCTextBox.h file:
using namespace System::Windows::Forms; - 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 - 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- 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:These steps apply to the whole article.
- Open the Form1 form in Design view, and then add a
TextBox control to Form1.
- Open the Form1.h file, and then add the following code
after the #pragma directive:
#include "SCTextBox.h" - 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(); - Save the project, and then press F5 to run the application
in debug mode.
- Type some sample text in the text box that is located on
the Form1 form.
- 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.
- Right-click in the text box, and then click
Cut. Notice that the selected text is deleted.
Back to the topSubclass a form- To create a Visual C++ .NET or Visual C++ 2005 Windows Application project
that is named SubclassingDemo, follow these steps:
- Start Visual Studio .NET 2003 or 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
SubclassingDemo, and then click
OK.
By default, the Form1 form is created.
- 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);
} - Make sure that the Output window is open or press
CTRL+ALT+O to open the Output window.
- Save the project, and then press F5 to run the application
in debug mode.
- 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. Back to the topSubclass any HWND handle- Add a new class that is named SubclassHWND to the SubclassingDemo project. To do this, follow these steps:
- On the Project menu, click Add
Class. The Add Class - SubclassingDemo dialog box
appears.
- Under Categories, click
Generic.
Note In Visual Studio 2005, Generic is changed to C++. - Under Templates, click Generic
C++ Class.
Note In Visual Studio 2005,Generic
C++ Class is changed to C++ Class. - Click Open. The Generic C++
Class Wizard - SubclassingDemo dialog box appears.
- In the Class name box, type
SubclassHWND, and then click
Finish.
- Add the following statement at the top of the code in the
SubclassHWND.h file:
using namespace System::Windows::Forms; - 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);
}
}; - Open the Form1.h file, and then add the following code
after the #pragma directive:
#include "SubclassHWND.h" - 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. - Make sure that the Output window is open or press
CTRL+ALT+O to open the Output window.
- Save the project, and then press F5 to run the application
in debug mode.
- 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. Your SubClassHWND object receives these messages. Back to the
topREFERENCES
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
Modification Type: | Major | Last Reviewed: | 1/17/2006 |
---|
Keywords: | kbMsg kbForms kbWindowsForms kbWndwProc kbCtrl kbControl kbHOWTOmaster KB815774 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|