如何使用的WndProc? [英] How to use WndProc?
问题描述
如何使用的WndProc Funection在我的形式一个图片? 我试着像这样code,但它不工作,没有任何信息发送给我 市民:虚拟无效的WndProc(消息%M)
使用命名空间系统;
使用命名空间系统:ComponentModel;
使用命名空间系统::集合;
使用命名空间系统:视窗:表格;
使用命名空间系统:数据;
使用命名空间系统::绘图;
命名空间MyProject的{
公众参考Form1类:公共系统:视窗:形式:表
{
上市:
Form1的(无效){
的InitializeComponent();
// TODO:添加构造函数code在这里
}
保护:
〜Form1中(){
如果(部件)
删除组件;
}
私人:
系统:视窗:形式:图片框^ pictureBox1;
系统:: ComponentModel ::集装箱^组件;
无效的InitializeComponent(无效){
这 - > pictureBox1 = gcnew系统:视窗:形式:图片框();
(CLI :: safe_cast<系统:: ComponentModel :: ISupportInitialize接口^>(
这 - > pictureBox1)) - > BeginInit在();
这 - > SuspendLayout();
//
// pictureBox1
//
这 - > pictureBox1->位置=系统::绘画::点(41,27);
这 - > pictureBox1->名称= LpictureBox1;
这 - > pictureBox1->尺寸=系统::绘画::尺寸(206,203);
这 - > pictureBox1->的TabIndex = 0;
这 - > pictureBox1->接受tab = FALSE;
//
// Form1中
//
这 - > AutoScaleDimensions =系统::绘画::的SizeF(6,13);
这 - > AutoScaleMode =系统:视窗:形式:AutoScaleMode ::字体;
这 - >的ClientSize =系统::绘画::尺寸(292,265);
这 - >控制 - >添加(这 - > pictureBox1);
这 - >名称= LForm1的;
这 - >文字= LForm1的;
(CLI :: safe_cast<系统:: ComponentModel :: ISupportInitialize接口^>(
这 - > pictureBox1)) - > EndInit();
这 - > ResumeLayout(假);
}
};
引用类pictureBox1:图片框{
上市:
虚拟无效的WndProc(消息%M)替代{
__super :: WndProc中(M);
}
};
} //关闭命名空间
由SLaks提供的答案是正确的,我百分之百同意他的意见,你需要的明白的是什么code均值,而不是复制和粘贴一个神奇的片段关闭堆栈溢出。
不过,我看你还是尖叫如何你应该写的code能够使用自定义图片框
类(在其上覆盖了一个的WndProc
函数),而不是内置于一体。这是真正改变所有的引用来系统:视窗:形式简单的事情::图片框
到 pictureBox1
(自定义类)。当然,你需要改变一个或其他的名字,但我建议而不是默认的一切选择更好的名字。
例如,尝试以下操作:
命名空间MyProject的{
使用命名空间系统;
使用命名空间系统:ComponentModel;
使用命名空间系统::集合;
使用命名空间系统:视窗:表格;
使用命名空间系统:数据;
使用命名空间系统::绘图;
公众参考Form1类:公共系统:视窗:形式:表
{
上市:
Form1的(无效)
{
的InitializeComponent();
//
// TODO:添加构造函数code在这里
//
}
保护:
〜Form1中()
{
如果(部件)
{
删除组件;
}
}
私募:pictureBox1 ^的MyPictureBox;
保护:
私人:
系统:: ComponentModel ::集装箱^组件;
无效的InitializeComponent(无效)
{
这 - >的MyPictureBox =(gcnew pictureBox1());
(CLI :: safe_cast<系统:: ComponentModel :: ISupportInitialize接口^>(这 - >的MyPictureBox)) - > BeginInit在();
这 - > SuspendLayout();
//
//的MyPictureBox
//
这 - > myPictureBox->位置=系统::绘画::点(41,27);
这 - > myPictureBox->名称= L的MyPictureBox;
这 - > myPictureBox->尺寸=系统::绘画::尺寸(206,203);
这 - > myPictureBox->的TabIndex = 0;
这 - > myPictureBox->接受tab = FALSE;
//
// Form1中
//
这 - > AutoScaleDimensions =系统::绘画::的SizeF(6,13);
这 - > AutoScaleMode =系统:视窗:形式:AutoScaleMode ::字体;
这 - >的ClientSize =系统::绘画::尺寸(292,265);
这 - >控制 - >添加(这 - >的MyPictureBox);
这 - >名称= LForm1的;
这 - >文字= LForm1的;
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->myPictureBox))->EndInit();
这 - &GT; ResumeLayout(假);
}
};
引用类pictureBox1:图片框{
//保护:
上市:
虚拟无效的WndProc(消息%M)替代{
__super :: WndProc中(M);
}
};
} //关闭命名空间
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
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
这篇关于如何使用的WndProc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!