CreateParams Class
Encapsulates the information needed when creating a control.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
| Name | Description | |
|---|---|---|
![]() | CreateParams() | Initializes a new instance of the CreateParams class. |
| Name | Description | |
|---|---|---|
![]() | Caption | Gets or sets the control's initial text. |
![]() | ClassName | Gets or sets the name of the Windows class to derive the control from. |
![]() | ClassStyle | Gets or sets a bitwise combination of class style values. |
![]() | ExStyle | Gets or sets a bitwise combination of extended window style values. |
![]() | Height | Gets or sets the initial height of the control. |
![]() | Param | Gets or sets additional parameter information needed to create the control. |
![]() | Parent | Gets or sets the control's parent. |
![]() | Style | Gets or sets a bitwise combination of window style values. |
![]() | Width | Gets or sets the initial width of the control. |
![]() | X | Gets or sets the initial left position of the control. |
![]() | Y | Gets or sets the top position of the initial location of the control. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | ToString() | Returns a string that represents the current object.(Overrides Object::ToString().) |
The information in a CreateParams can be used to pass information about the initial state and appearance of a control. Most Control derived controls override the CreateParams property to pass in the appropriate values or include additional information in the CreateParams.
For more information about creating control parameters, see the CreateWindow and CreateWindowEx functions and the CREATESTRUCT structure documentation in the Windows Platform SDK reference located in the MSDN Library.
Note |
|---|
The constants used to set the Style, ExStyle, and ClassStyle properties are defined in the Winuser.h header file. This file is installed by the Platform SDK or Visual Studio. |
The following code example creates a Button derived class named MyIconButton and provides the implementation needed for the button to display an Icon rather than an Image. The CreateParams property is extended and a value added to the Style property that causes the button to display an Icon rather than an Image.
#include <windows.h> #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::Runtime::InteropServices; using namespace System::Diagnostics; using namespace System::IO; public ref class MyIconButton: public Button { private: Icon^ icon; public: MyIconButton() { // Set the button's FlatStyle property. FlatStyle = ::FlatStyle::System; } MyIconButton( Icon^ ButtonIcon ) { // Set the button's FlatStyle property. FlatStyle = ::FlatStyle::System; // Assign the icon to the private field. this->icon = ButtonIcon; // Size the button to 4 pixels larger than the icon. this->Height = icon->Height + 4; this->Width = icon->Width + 4; } protected: property System::Windows::Forms::CreateParams^ CreateParams { virtual System::Windows::Forms::CreateParams^ get() override { // Extend the CreateParams property of the Button class. System::Windows::Forms::CreateParams^ cp = __super::CreateParams; // Update the button Style. cp->Style |= 0x00000040; // BS_ICON value return cp; } } public: property System::Drawing::Icon^ Icon { System::Drawing::Icon^ get() { return icon; } void set(System::Drawing::Icon^ value) { icon = value; UpdateIcon(); this->Height = icon->Height + 4; this->Width = icon->Width + 4; } } protected: virtual void OnHandleCreated( EventArgs^ e ) override { Button::OnHandleCreated( e ); // Update the icon on the button if there is currently an icon assigned to the icon field. if ( icon != nullptr ) { UpdateIcon(); } } private: void UpdateIcon() { IntPtr iconHandle = IntPtr::Zero; // Get the icon's handle. if ( icon != nullptr ) { iconHandle = icon->Handle; } // Send Windows the message to update the button. SendMessage( (HWND)Handle.ToPointer(), 0x00F7, 1, (int)iconHandle ); /*BM_SETIMAGE value*/ /*IMAGE_ICON value*/ } public: [DllImport("user32.dll")] static LRESULT SendMessage(HWND hWnd, int msg, int wParam, int lParam); };
The following code example creates an instance of a standard Button control and an instance of the derived control, MyIconButton, created in the example above. This example requires that there is an Icon file named Default.ico in the same location as the application. When the application starts, the Default icon is displayed on the MyIconButton button. If the Default icon is not present, the button face is blank. When the standard Button is clicked, an OpenFileDialog box will appear so you can select a new Icon to display on the MyIconButton.
public ref class MyApplication: public Form { private: MyIconButton^ myIconButton; Button^ stdButton; OpenFileDialog^ openDlg; public: MyApplication() { try { // Create the button with the default icon. myIconButton = gcnew MyIconButton( gcnew System::Drawing::Icon( String::Concat( Application::StartupPath, "\\Default.ico" ) ) ); } catch ( Exception^ ex ) { // If the default icon does not exist, create the button without an icon. myIconButton = gcnew MyIconButton; #if defined(DEBUG) Debug::WriteLine( ex ); #endif } finally { stdButton = gcnew Button; // Add the Click event handlers. myIconButton->Click += gcnew EventHandler( this, &MyApplication::myIconButton_Click ); stdButton->Click += gcnew EventHandler( this, &MyApplication::stdButton_Click ); // Set the location, text and width of the standard button. stdButton->Location = Point(myIconButton->Location.X,myIconButton->Location.Y + myIconButton->Height + 20); stdButton->Text = "Change Icon"; stdButton->Width = 100; // Add the buttons to the Form. this->Controls->Add( stdButton ); this->Controls->Add( myIconButton ); } } private: void myIconButton_Click( Object^ /*Sender*/, EventArgs^ /*e*/ ) { #undef MessageBox // Make sure MyIconButton works. MessageBox::Show( "MyIconButton was clicked!" ); } void stdButton_Click( Object^ /*Sender*/, EventArgs^ /*e*/ ) { // Use an OpenFileDialog to allow the user to assign a new image to the derived button. openDlg = gcnew OpenFileDialog; openDlg->InitialDirectory = Application::StartupPath; openDlg->Filter = "Icon files (*.ico)|*.ico"; openDlg->Multiselect = false; openDlg->ShowDialog(); if ( !openDlg->FileName->Equals( "" ) ) { myIconButton->Icon = gcnew System::Drawing::Icon( openDlg->FileName ); } } }; int main() { Application::Run( gcnew MyApplication ); }
Available since 1.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.



