VS2015 c++ clr 自定义类中引用窗体中的对象代码,寥寥几行代码,虽少,但实用。胜过百度、必应搜索N多次的结果。而且,从那上面搜索不到有用的结果。

//Form1.h 代码:
#pragma once
#include "stdafx.h"
namespace MYFrom1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	
	/// <summary>
	/// Form1 摘要
	/// </summary>

	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:

		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: 在此处添加构造函数代码
			//LOG("\nInitializeComponent:%p", this);
		}

	protected:
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	public: System::Windows::Forms::Button^  button1;
	public: System::Windows::Forms::TextBox^  textBox1;
	public: System::Windows::Forms::RichTextBox^  richTextBox1;
	protected:

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(151, 283);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(200, 72);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(39, 12);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(515, 142);
			this->textBox1->TabIndex = 1;
			// 
			// richTextBox1
			// 
			this->richTextBox1->Location = System::Drawing::Point(67, 160);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->Size = System::Drawing::Size(412, 117);
			this->richTextBox1->TabIndex = 2;
			this->richTextBox1->Text = L"";
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(582, 396);
			this->Controls->Add(this->richTextBox1);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button1);
			this->Name = L"MyForm";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e);
	};
}



//Form1.cpp代码:
#pragma once
#include "stdafx.h"
#include "gcroot.h" //VS2015以下版本要引用这个头文件。
#include "Form1.h"
using namespace MYFrom1 ;
gcroot<MYFrom1::MyForm^> vMySubFrm; //#include "gcroot.h" //VS2015以下版本要引用这个头文件。


System::Void MyForm::button1_Click(System::Object^  sender, System::EventArgs^  e)
{
	

	vMySubFrm = this;
	
	this->richTextBox1->AppendText("\nHello World OK");

};
//CMyClass.h代码:
#pragma once
#include "stdafx.h"

class CMyClass
{
public :
	CMyXMdApi(){}
	~CMyXMdApi(){}
    void pro1();
};

//CMyClass.cpp代码:
#pragma once
#include "stdafx.h"
#include "CMyClass.h"
#include "Form1.h"
using namespace MYFrom1 ;
extern gcroot<MYFrom1::MyForm^> vMySubFrm;
void CMyClass::pro1()
{
	
	System::String ^ str = "\nHello World\n";
	vMySubFrm->richTextBox1->AppendText(str);
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值