使用C Plus Plus的TCP聊天GUI [英] TCP Chat GUI With C Plus Plus

查看:64
本文介绍了使用C Plus Plus的TCP聊天GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型聊天应用程序,用于Visual Studio 2012和GUI编程。我已经学习了一些c ++课程,但它们只涉及控制台编程,虽然我能够在控制台中运行应用程序,但我希望实现一个GUI前端。



据说我创建了聊天应用程序的服务器部分。它有一个带控制台和命令文本框的简单GUI。



我试图在代码中写入更新GUI的控制台窗口但是当我创建时修改表单的辅助类我无法访问控件。



这是我正在使用的form1.h文件。这是帮助类使用服务器命令。



I am working on a small Chat application to play with Visual studio 2012 and GUI programming. I have taken some classes in c++ but they only cover console programming, and while i am able to get the application working in a console i would like to implement a GUI front end.

That being said i have created the server section of the chat application. It has a simple GUI with a console and a command text box.

I am trying to write in the code to update the console window of the GUI however when i create a secondary class to modify the form i am unable to access the controls.

This is the form1.h file that i am using.and this is the helper class to work with Server commands.

#pragma once

namespace GUI_Chat {
	#include "Commands.h"

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace ChatCommands;
	
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		static Form1^ form1_Functions;
		Form1(void)
		{
			InitializeComponent();
			form1_Functions = this;
			//
			//TODO: Add the constructor code here
			//
		}

	void update_Console(System::String^ text)
		{
			rtbConsole->Text += text + Environment::NewLine;
		}
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::RichTextBox^  rtbConsole;
			 
	protected: 

	private: System::Windows::Forms::TextBox^  txtCommands;
	protected: 

	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->rtbConsole = (gcnew System::Windows::Forms::RichTextBox());
			this->txtCommands = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// rtbConsole
			// 
			this->rtbConsole->Enabled = false;
			this->rtbConsole->Location = System::Drawing::Point(0, 0);
			this->rtbConsole->Name = L"rtbConsole";
			this->rtbConsole->Size = System::Drawing::Size(554, 235);
			this->rtbConsole->TabIndex = 0;
			this->rtbConsole->Text = L"";
			// 
			// txtCommands
			// 
			this->txtCommands->Location = System::Drawing::Point(0, 241);
			this->txtCommands->Name = L"txtCommands";
			this->txtCommands->Size = System::Drawing::Size(554, 20);
			this->txtCommands->TabIndex = 1;
			this->txtCommands->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::txtCommands_KeyDown);
			// 
			// Form1
			// 
			this->AccessibleName = L"frmServer";
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::Black;
			this->ClientSize = System::Drawing::Size(554, 261);
			this->ControlBox = false;
			this->Controls->Add(this->txtCommands);
			this->Controls->Add(this->rtbConsole);
			this->MaximumSize = System::Drawing::Size(570, 300);
			this->MinimizeBox = false;
			this->MinimumSize = System::Drawing::Size(570, 300);
			this->Name = L"Form1";
			this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Hide;
			this->Text = L"Server";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		Chat_Commands CCommands;

		void txtCommands_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
		{
				
					// Determine whether the keystroke is an Enter
					if ( e->KeyCode == Keys::Return )
					{
						CCommands.command_Selection(txtCommands->Text);
						// DEBUG: Change text of rtbConsole to the value of txtCommands
						rtbConsole->Text += txtCommands->Text + Environment::NewLine;
						txtCommands->Text = "";
					}
				
		}
	};
}





这是帮助文件





And this is the Helper File

// Randy C. Miller
//
// This file containes all the server command code for the TCP GUI Chat application.
//
#pragma managed

namespace ChatCommands {

	//#ifndef GUI_Chat_Commands
		//#define GUI_Chat_Commands
	//#endif

	// Include Files
	//#include <string>
	//#include <stdlib.h>

	//using namespace std;
	using namespace System::Windows::Forms;
	using namespace GUI_Chat;

	// Class Decleration
	public ref class Chat_Commands
	{
		// Private section
	private:
		
		// Public section
	public:
		//void GUI_Chat_Commands(void);
		Chat_Commands(void);
		void command_Selection(System::String^ command);
		char change_Case(char letter);
		void display_Usage(void);

	};

	// Constructor
	Chat_Commands::Chat_Commands()
	{

	}

	// Determine what command is being entered.
	void Chat_Commands::command_Selection(System::String^ command)
	{
		System::String ^lowerCommand;
		
		for each (char letter in command)
		{
			lowerCommand += (wchar_t) change_Case(letter);
		}

		if ("exit" == lowerCommand)
		{
			Application::Exit();
			//System::Windows::Forms::MessageBox::Show("Exit");
		}
		if ("?" == lowerCommand)
		{
			display_Usage();
		}
	}

	char Chat_Commands::change_Case(char letter)
	{
		// This changes to uppercase
		//if (letter > 96 && letter < 122)  // Check if the letter is lowercase
		//{
			//letter -= 32;	// Change the letter to uppercase.
		//}
		if (letter > 'A' && letter < 'Z') // Check if the letter is uppercase
		{
			letter += 32;
		}
		return letter;
	}
	/*
	ref class ConnectionContainer
	{
		static GUI_Chat::Form1^ glblForm1 = nullptr;
		public :  
		static GUI_Chat::Form1^ get_Form1_Object()
		{
			if (glblForm1 == nullptr) 
	        {
				glblForm1 = gcnew GUI_Chat::Form1;
			}
			return glblForm1;
		}
}	;
	*/
	void display_Usage(void)
	{
		//ConnectionContainer^ Container = gcnew ConnectionContainer();
		//GUI_Chat::Form1^ Connection = Container->get_Form1_Object();
		
		//GUI_Chat::Form1::rtbConsole->Text += "Usage" + Environment::NewLine;
		//Connection->update_Console("Usage");
		Form1::form1_Functions->update_Console("Usage");
	}
}







There are allot of commented out lines as i have been working with this for a while and trying different things.
I tried many different ways of resolving this and searched for hours on Google.

Any help that anyone can provide would be appreciated, also any links to GUI programming tutorials would be helpfull.

Thank you

推荐答案

一个简单的技巧可以帮助你解决这个相当简单的问题:而不是有一个单独的帮助器类,你可以使用相同的类,并且,为了避免代码混乱,使用部分类语法,并添加部分是一个单独的文件:

http://msdn.microsoft.com/en-us/library/vstudio/hh972420.aspx [ ^ ]。



但是,如果您需要将此帮助程序类重用于不同的表单类,则此方法无济于事。您应该为辅助类提供对控件的一些访问权限。您可以将控制成员声明为内部控件,它会为您提供访问权限,但会违反正确的封装。最强大的方法是实现一些接口(再次,在单独的类部分中执行,以避免混乱代码),并将引用传递给窗体作为接口引用,以避免破坏封装并将接口传递给整个窗体。这个接口应该提供对控件的正确访问,并作为使辅助类和表单类更加松散耦合的方法。



请另见我过去的回答:如何获取数据从一种形式到另一种形式。 [ ^ ]。



参见: http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
One simple technique could help you to work around this rather simple problem: instead of having a separate helper class, you can use the same class, and, to avoid code clutter, use the partial class syntax, and add the part is a separate file:
http://msdn.microsoft.com/en-us/library/vstudio/hh972420.aspx[^].

However, if you need this helper class to be reused for different form class, this approach won't help. The you should provide some access to your control to the helper class. You could declare control members as internal, it would give you the access but violate proper encapsulation. The most robust approach is to implement some interface (again, do it in a separate class part, to avoid cluttering your code), and pass the reference to the form as interface reference, to avoid breaking encapsulation and passing the interface to the whole form. This interface should provide proper access to the controls and serve as the method making the helper class and the form class more loosely coupled.

Please see also my past answer: How to get data from one form to another.[^].

See also: http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于使用C Plus Plus的TCP聊天GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆