Home > Software engineering >  C programming window vs2010 before the initialization of the main window opened a thread, how to mod
C programming window vs2010 before the initialization of the main window opened a thread, how to mod

Time:09-20

To achieve the function: read data from the serial port, when reading data for display in the main window lable1 labels,
Before my train of thought: the main window initialization, opened a thread to monitor a serial port and reads the data, read data after modification form1 class lable1 label Text value, I use the label1 - & gt; Text, form1. Label1 - & gt; Text, form1 - & gt; Label1 - & gt; Text is not, I have been in the form1 will lable1 into a public class, I know that the problem should be on the left side of the lable1 need to specify the window, but how can I find the handle to the instance name or the main window, the automatically generated code also does not define form1 class instance, an Application: : Run (gcnew form1 ()); Threads, there is another solution, send a message to the form1 class after receipt of the data, then form1 class to define the message handler to modify lable1, but this way I didn't find the message queue, vs the generated code, there is no see message processing related code, the code package to where go to, the question now is compiled through, however, I will add in the program to judge the code main window knows no initialization has been completed, so don't have to worry about changes in the main window initialization before
I didn't understand the main three aspects:
Question 1: how to find the main window handle to the instance name or
Question 2: this vs automatically generated form application, how to custom message and processing function
Question 3: in the initialization function of class if you can open a thread, used to control the relevant properties of the class, the callback function should be how to define, I tried is defined as a class member function thread began to pass the code compilation, defined as the static function and no direct reference to control,

The great god, please help


` ` `
//new_form. CPP
# include "stdafx. H"
# include "Form1. H"
# include
# include
#include

using namespace std;
Using the namespace new_form;

Unsigned int _stdcall ListenThread (void * pParam);

[STAThreadAttribute]
Int main (array{
Volatile HANDLE m_hListenThread=INVALID_HANDLE_VALUE;
Unsigned int threadId;
M_hListenThread=(HANDLE) _beginthreadex (NULL, 0, ListenThread, NULL, 0, & amp; ThreadId);//open to monitor thread
//before creating any controls enable Windows XP visual effect
Application: : EnableVisualStyles ();
Application: : SetCompatibleTextRenderingDefault (false);

//create the main window and run it
Application: : Run (gcnew _click ());
return 0;
}

Unsigned int _stdcall ListenThread (void * pParam)
{
Label1 - & gt; Text="have data to display!" ;
}
` ` `

The header file
` ` `
//form1. H
# pragma once
# pragma comment (lib, "User32. Lib")

# include
# include
# include
#include

using namespace std;
Using the namespace System: : Runtime: : InteropServices;
The namespace new_form {

Using the namespace System;
Using the namespace System: : ComponentModel;
Using the namespace System: : Collections;
Using the namespace System: : Windows: : Forms;
Using the namespace System: : Data;
Using the namespace System: : Drawing;

///& lt; Summary>
///Form1
the///& lt;/summary>
Public ref class Form1: public System: : Windows: : Forms: : Form
{
Public:
_click (void)
{
InitializeComponent ();
}

Protected:
///& lt; Summary>
///clean up any resources being used,
///& lt;/summary>
~ _click ()
{
If (components)
{
Delete the components;
}
}

Public: System: : Windows: : Forms: : Label ^ label1;
Private:
///& lt; Summary>
///the necessary design variables,
///& lt;/summary>
System: : ComponentModel: : Container ^ components;

# pragma region Windows Form Designer generated code
///& lt; Summary>
///designer support required methods - not
///use the code editor to modify the contents of this method,
///& lt;/summary>
Void InitializeComponent (void)
{
This - & gt; Label1=(gcnew System: : Windows: : Forms: : Label ());
This - & gt; SuspendLayout ();
//label1
//
This - & gt; Label1 - & gt; The Font=(gcnew System: : Drawing: : the Font (L "tahoma", 36, System: : Drawing: : FontStyle: : Regular, System: : Drawing: : GraphicsUnit: : Point,
Static_cast & lt; System: : Byte> (134)));
This - & gt; Label1 - & gt; Location=System: : Drawing: Point (30, 85);
This - & gt; Label1 - & gt; Name=L "label1";
This - & gt; Label1 - & gt; Size=System: : Drawing: : Size (559, 47).
This - & gt; Label1 - & gt; TabIndex=19;
This - & gt; Label1 - & gt; Text=L "is the default display content:";
This - & gt; Label1 - & gt; Click +=gcnew System: : EventHandler (this, & amp; Form1: : label1_Click);
//
//Form1
//
This - & gt; AutoScaleDimensions=System: : Drawing: : SizeF (6, 12);
This - & gt; AutoScaleMode=System: : Windows: : Forms: : AutoScaleMode: : the Font;
This - & gt; ClientSize=System: : Drawing: : Size (623, 340);
This - & gt; Controls - & gt; Add (this - & gt; Label1);
This - & gt; ForeColor=System: : Drawing: : Color: : CornflowerBlue;
This - & gt; Name="Form1" L;
This - & gt; StartPosition=System: : Windows: : Forms: : FormStartPosition: : CenterScreen;
This - & gt; Text=L "pager";
This - & gt; The Load +=gcnew System: : EventHandler (this, & amp; Form1: : Form1_Load);
This - & gt; ResumeLayout (false);
This - & gt; PerformLayout ();
}
# pragma endregion
};

}
` ` `

CodePudding user response:

Open the serial port in the Form and the thread is easier to https://www.cnblogs.com/bacazy/p/3500117.html

CodePudding user response:

reference 1st floor zgl7903 response:
open the serial port and the thread is in the Form is easier to https://www.cnblogs.com/bacazy/p/3500117.html

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related