如何使用WndProc?

发布于 2024-10-14 02:09:39 字数 2246 浏览 4 评论 0原文

如何在我的表单中使用 WndProc 函数作为图片框? 我像这段代码一样尝试,但它不起作用,并且没有任何消息发送到我的 public: virtual void WndProc(消息%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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-10-21 02:09:39

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

但我发现您仍然在尖叫如何编写代码来使用自定义 PictureBox 类(您覆盖了 WndProc 函数的类),而不是内置一个。这实际上是一个简单的问题,将对 System::Windows::Forms::PictureBox 的所有引用更改为 pictureBox1 (您的自定义类)。当然,您需要更改其中一个名称,但我建议为所有内容选择比默认名称更好的名称。

例如,尝试以下操作:

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

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
枕头说它不想醒 2024-10-21 02:09:39

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

但是,不要

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文