关于.net:如何使用WndProc?

How to use WndProc?

如何在窗体中的图片框中使用WndProc函数?
我尝试像这样的代码,但它不起作用,也没有任何消息发送给我
public:虚拟void WndProc(Message%m)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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


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

但是,不要。

除非绝对必要,否则不应该在.Net开发中使用

WndProc。在这里,没有必要。
您应该使用.Net事件。


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

但是我看到您仍然在抱怨应该如何编写代码以使用自定义的PictureBox类(覆盖了WndProc函数的类),而不是内置的类。将所有对System::Windows::Forms::PictureBox的引用更改为pictureBox1(您的自定义类)确实很简单。当然,您需要更改一个或另一个的名称,但我建议为所有内容选择比默认名称更好的名称。

例如,尝试以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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