1

如何在我的表单中为图片框使用 WndProc 函数?我尝试使用此代码,但它不起作用并且没有任何消息发送给我的公众: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
4

2 回答 2

2

您创建了一个新类,但从未使用过它。
您需要将您的图片框更改为新类的实例。

但是,不要

WndProc除非绝对必要,否则不应在 .Net 开发中使用;在这里,没有必要。
您应该使用 .Net 事件。

于 2011-01-26T14:09:21.177 回答
1

SLaks 提供的答案是正确的,我 100% 同意他的评论,即您需要了解代码的含义,而不是从 Stack Overflow 上复制和粘贴一个神奇的片段。

但是我看到你仍然在尖叫你应该如何编写代码来使用你的自定义PictureBox类(你覆盖WndProc函数的那个​​),而不是内置的。更改System::Windows::Forms::PictureBoxpictureBox1(您的自定义类)的所有引用实际上是一件简单的事情。当然,您需要更改其中一个的名称,但我建议为所有内容选择比默认名称更好的名称。

例如,尝试以下操作:

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
于 2011-01-26T15:17:16.523 回答