C++/CLI 创建WinForm程序

本文演示下用CLR创建一个简单的winform程序,IDE:VS2015

可以参考另一篇文章:http://blog.csdn.net/wcc27857285/article/details/78135314

第一步:


第二步:在头文件文件夹中新增class,选择windows Form


然后查看右侧引用,你会发现多了很多winform专用的dll,VS自动帮我们添加了这些引用

接下来,打开MyForm.cpp,输入代码如下:

#include "MyForm.h"

using namespace ClrWinForm;


int main(array<System::String^>^args)
{
	Application::EnableVisualStyles();
	MyForm^ form = gcnew MyForm();
	Application::Run(form);
	return 0;
}


至此F5运行,可以看到熟悉的winform界面:


然后,双击右侧MyForm.h,可以看到设计器,然后可以看到左侧的toolbox里面有winform熟悉的各种控件,又可以拖控件了!

在窗体上右键查看View code,可以看到代码如下:

#pragma once

namespace ClrWinForm {

	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>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = gcnew System::ComponentModel::Container();
			this->Size = System::Drawing::Size(300,300);
			this->Text = L"MyForm";
			this->Padding = System::Windows::Forms::Padding(0);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		}
#pragma endregion
	};
}
这是VS自动生成的代码,和传统的winform的desinger.cs中的代码很相似,但是这里使用C++/CLI写的





  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 C# 和 WinForms 的示例代码,用于上位机开发TCP/IP协议的WinForm程序: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Windows.Forms; namespace TCPClientApp { public partial class MainForm : Form { private TcpClient tcpClient; private NetworkStream networkStream; private byte[] buffer; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { // 创建TCP客户端 tcpClient = new TcpClient(); buffer = new byte[1024]; } private void btnConnect_Click(object sender, EventArgs e) { try { // 连接到服务器 string ipAddress = txtServerIP.Text; int port = int.Parse(txtServerPort.Text); tcpClient.Connect(IPAddress.Parse(ipAddress), port); // 获取网络流 networkStream = tcpClient.GetStream(); lblStatus.Text = "已连接到服务器"; } catch (Exception ex) { lblStatus.Text = "无法连接到服务器:" + ex.Message; } } private void btnSend_Click(object sender, EventArgs e) { try { // 发送数据到服务器 string data = txtData.Text; byte[] dataBytes = Encoding.ASCII.GetBytes(data); networkStream.Write(dataBytes, 0, dataBytes.Length); // 接收服务器响应 int bytesRead = networkStream.Read(buffer, 0, buffer.Length); string response = Encoding.ASCII.GetString(buffer, 0, bytesRead); txtResponse.Text = response; } catch (Exception ex) { lblStatus.Text = "发送数据时发生错误:" + ex.Message; } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // 关闭网络流和TCP客户端 networkStream?.Close(); tcpClient?.Close(); } } } ``` 这个示例代码创建了一个名为 `MainForm` 的 WinForms 窗体。在窗体加载时,它创建了一个 `TcpClient` 对象和一个用于接收数据的缓冲区。 窗体中有两个按钮,`btnConnect` 用于连接到服务器,`btnSend` 用于发送数据并接收服务器的响应。 点击 `btnConnect` 按钮时,它会尝试连接到指定的服务器IP地址和端口号,并获取网络流。如果连接成功,状态标签 `lblStatus` 将显示已连接到服务器的消息。 点击 `btnSend` 按钮时,它会将文本框 `txtData` 中的数据发送到服务器,并接收服务器的响应。响应数据将显示在文本框 `txtResponse` 中。 在窗体关闭时,它会关闭网络流和TCP客户端。 请注意,这只是一个示例代码,实际的上位机开发可能需要更多的功能和处理逻辑。希望对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值