How to use WndProc?
-
24-10-2019 - |
Question
How to use WndProc Funection for a picturebox in my form ? i try it like this code but it not work and not any message send to my public: virtual void WndProc( Message% m )
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace MyProject {
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void) {
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~Form1() {
if (components)
delete components;
}
private:
System::Windows::Forms::PictureBox^ pictureBox1;
System::ComponentModel::Container ^components;
void InitializeComponent(void) {
this->pictureBox1 = gcnew System::Windows::Forms::PictureBox();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(
this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(41, 27);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(206, 203);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 265);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(
this->pictureBox1))->EndInit();
this->ResumeLayout(false);
}
};
ref class pictureBox1 : PictureBox {
public:
virtual void WndProc( Message% m ) override {
__super::WndProc(m);
}
};
}//close NameSpace
Solution
The answer provided by SLaks is correct, and I agree 100% with his comments that you need to understand what the code means, rather than copying and pasting a magical snippet off Stack Overflow.
But I see you're still screaming about how you should write the code to use your custom PictureBox
class (the one on which you overrode the WndProc
function), rather than the built-in one. That's really a simple matter of changing all of the references to System::Windows::Forms::PictureBox
to pictureBox1
(your custom class). Of course, you'll need to change the name of one or the other, but I recommend choosing better names than the default for everything.
For example, try the following:
namespace MyProject {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: pictureBox1^ myPictureBox;
protected:
private:
System::ComponentModel::Container ^components;
void InitializeComponent(void)
{
this->myPictureBox = (gcnew pictureBox1());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->myPictureBox))->BeginInit();
this->SuspendLayout();
//
// myPictureBox
//
this->myPictureBox->Location = System::Drawing::Point(41, 27);
this->myPictureBox->Name = L"myPictureBox";
this->myPictureBox->Size = System::Drawing::Size(206, 203);
this->myPictureBox->TabIndex = 0;
this->myPictureBox->TabStop = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 265);
this->Controls->Add(this->myPictureBox);
this->Name = L"Form1";
this->Text = L"Form1";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->myPictureBox))->EndInit();
this->ResumeLayout(false);
}
};
ref class pictureBox1 : PictureBox {
//protected:
public:
virtual void WndProc( Message% m ) override {
__super::WndProc(m);
}
};
}//close NameSpace
OTHER TIPS
You created a new class, but you never used it.
You need to change your picturebox to be an instance of your new class.
However, don't.
WndProc
should not be used in .Net development unless absolutely necessary; here, it is not necessary.
You should use .Net events.