Windows窗口程序的线程中如何修改主窗口中的控件

Windows窗口程序的线程中如何修改主窗口中的控件

想实现的功能:从串口读取数据,当读到数据后在主窗口的lable1标签中进行显示。
我的思路:在主窗口初始化之前,开了一个线程监听串口并读取数据,读取到数据后修改form1类lable1标签的Text值。我用label1->Text、form1.label1->Text、form1->label1->Text都不行。我已经在form1类中将lable1改成public了,我知道问题应该是在lable1的左边需要明确指出窗口名,但是怎么才能找到这个主窗口的实例名或者是句柄。自动生成的代码中也没有定义form1类实例的地方,就一条Application::Run(gcnew Form1());。还有另一种解决思路,线程收到数据后向form1类发送一个消息,然后form1类去定义消息处理函数来修改lable1,但是这中方法我根本没有找到消息队列的地方,vs生成的代码中就没有看见消息处理的相关代码。这个代码封装到哪里去了。现在的问题是编译通不过,程序中我会加判断代码主窗口知否已经初始化完成了,所以不用担心修改在主窗口初始化之前。请大神们帮帮忙

//new_form.cpp
#include "stdafx.h"
#include "Form1.h"
#include<iostream>
#include<fstream>
#include <process.h> 

using namespace std;
using namespace new_form;

unsigned int _stdcall ListenThread(void* pParam) ;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	volatile HANDLE    m_hListenThread= INVALID_HANDLE_VALUE;
	unsigned int threadId;
	m_hListenThread = (HANDLE)_beginthreadex(NULL, 0, ListenThread, NULL, 0, &threadId);//开启监听线程
	// 在创建任何控件之前启用 Windows XP 可视化效果
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// 创建主窗口并运行它
	Application::Run(gcnew Form1());
	return 0;
}

unsigned int _stdcall ListenThread(void* pParam) 
{
	label1->Text="有数据需要显示!";
}

头文件

//form1.h
#pragma once
#pragma comment(lib,"User32.lib")

#include<windows.h>
#include<iostream>
#include<string>
#include <process.h> 

using namespace std;
using namespace System::Runtime::InteropServices;
namespace new_form {

	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 Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			}

	protected:
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	
	public: System::Windows::Forms::Label^  label1;
	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// label1
			// 
			this->label1->Font = (gcnew System::Drawing::Font(L"宋体", 36, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label1->Location = System::Drawing::Point(30, 85);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(559, 47);
			this->label1->TabIndex = 19;
			this->label1->Text = L"默认显示内容:";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(623, 340);
			this->Controls->Add(this->label1);
			this->ForeColor = System::Drawing::Color::CornflowerBlue;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"呼叫器";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();
		}
#pragma endregion
};

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值