⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfu.cpp

📁 小程序
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#include <Clipbrd.hpp>
#pragma hdrstop

#include "MainFU.h"

#include "LogoCmdU.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	Logos = 0; cb = 0;
}
//---------------------------------------------------------------------------

const static AnsiString CmdWord =
"up down left right forward back color redo \
{ } 提笔 落笔 左转 右转 前进 后退 色彩 重复";

void __fastcall TForm1::Init()
{
	//ViewPB->Align = alClient;
	ShowP->Caption = "";
	PointPB->Height = 10;
	PointPB->Width = 10;
	CmdRE->Align = alClient;
	PageNb->Align = alClient;
	ShowP->AutoSize = true;
	ShowP->ClientToParent(ViewSB->ClientOrigin,ViewSB);
	Ange = 0;
	Logos = new LogoCmd();
	Logos->Set(ViewPB->Canvas,PointPB);
//	Logos->Set(ShowP)
	PageNb->ActivePage = "Edit";
	CmdRE->Clear();
	CmdRE->Text = "";
	ViewPB->Width = PageNb->Width;
	ViewPB->Height = PageNb->Height;
	cb = Clipboard();
	Caption = "Logo语言仿真工具";
	HelpRE->Text = "                            操作说明\n\
	Logo语言,又称海龟语言,是一种初级轨迹绘图工具,其操作方式简单,适合于\n对\
计算机编程感兴趣的初学者。\n\
	其操作方法简单灵活,通过步进制计算,可以实现各种情况的复杂绘图。\n\
	作为一种语言,其指令十分简单,一共只有8个基本指令和若干辅助指令组成:\n\
		1 UP              提笔    取消绘线功能                 up\n\
		2 DOWN        落笔    准备绘线                         down\n\
		3 LEFT           左转    向左转一个角度             left 60\n\
		4 RIGHT        右转    向右转一个角度             right 30\n\
		5 FORWARD 前进    沿原方向移动一段距离 forward 100\n\
		6 BACK         后退     沿原方向反向移动一段距离    back 10\n\
		7 COLOR      色彩     设置绘图颜色                color 7\n\
		8 REDO        重复     以指定次数执行{}内限定指令  redo 3 { ... }\n\
格式要求:\n\
	指令与指令、限制符、数字之间需以空格、制表符、回车键等分割开来,数字为\n整数。\
颜色0-15,分别代表16种颜色";
}


void __fastcall TForm1::FormCreate(TObject *Sender)
{
	Init();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PointPBPaint(TObject *Sender)
{
	static bool NotInited = true;
	static double DD;
	static int X,Y;
	TPaintBox *pb = static_cast<TPaintBox*>(Sender);
	TCanvas *ca = pb->Canvas;
	if(NotInited){
		ca->Pen->Color = clBlue;
		X = pb->Width/2-1; Y = pb->Height/2-1;
		DD = pb->Width * pb->Width + pb->Height*pb->Height;
		DD = sqrt(DD);
		NotInited = false;}
	ca->Ellipse(0,0,pb->Width,pb->Height);
	Ange = Logos->Ange;
	int x = DD*cos(Ange)/2, y = DD*sin(Ange)/2;
	ca->MoveTo(X+x,Y+y);ca->LineTo(X,Y);
	ca->LineTo(X-y/2,Y+x/2);ca->LineTo(X+x,Y+y);
	ca->LineTo(X+y/2,Y-x/2);ca->LineTo(X,Y);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
	if(Logos){
		delete Logos;
		Logos=0;}
	if(cb){
		cb->Clear();
		cb = 0;}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NewFileNClick(TObject *Sender)
{
	if(CmdRE->Modified && !CmdRE->Text.Trim().IsEmpty()){
		if(FileName.Trim().IsEmpty())
			if(SaveDlg->Execute())
				FileName = SaveDlg->FileName;
			else
				return ;
		SaveFile(FileName);}
	CmdRE->Text = "";
	CmdRE->Modified = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SaveFile(AnsiString filename)
{
	if(filename.Trim().IsEmpty())
		return;
	TFileStream *fs = 0;
	try{
		if(FileExists(filename))
			fs = new TFileStream(filename,fmOpenWrite);
		else
			fs = new TFileStream(filename,fmCreate);
		}
	catch(...){
		return;
		}
	fs->Position = 0;
	fs->Write(CmdRE->Text.data(),CmdRE->Text.Length());
	fs->Size = fs->Position;
	delete fs;
}

void __fastcall TForm1::RunNClick(TObject *Sender)
{
	if(CmdRE->Text.Trim().IsEmpty())
		return;
	if(Logos){
		bool bk = Logos->Read(CmdRE->Text);
		if(!bk){
			StatSB->Panels->Items[1]->Text = "语句错误!";
			return;}
		PageNb->ActivePage = "Run";
		Logos->Ange =0;
		Logos->X = ViewPB->Width/2;
		Logos->Y = ViewPB->Height/2;
		bk = Logos->Run();
		if(!bk){
//			PageNb->ActivePage = "Edit";
			StatSB->Panels->Items[1]->Text = "运行错误!";
			return;}
		StatSB->Panels->Items[1]->Text = "完成";
		PointPB->Repaint();
		}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ViewPBPaint(TObject *Sender)
{
	if(Logos){
		Logos->Ange =0;
		Logos->X = ShowP->Width/2;
		Logos->Y = ShowP->Height/2;
		Logos->Run();
		PointPB->Left = ViewSB->Left+Logos->X - PointPB->Width/2;
		PointPB->Top = ViewSB->Top + Logos->Y - PointPB->Height/2;
		}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SaveNClick(TObject *Sender)
{
	if(CmdRE->Text.Trim().IsEmpty()||!CmdRE->Modified)
		return;
	if(FileName.Trim().IsEmpty())
		if(SaveDlg->Execute())
			FileName = SaveDlg->FileName;
		else
			return;
	SaveFile(FileName);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::OpenNClick(TObject *Sender)
{
	SaveN->Click();
	if(OpenDlg->Execute())
		FileName = OpenDlg->FileName;
	else
		return;

	TFileStream *fs = 0;
	try{
		if(FileExists(FileName))
			fs = new TFileStream(FileName,fmOpenRead);
		else
			return;
		}
	catch(...){
		return;}
	char buf[101]={0};
	CmdRE->Text ="";
	fs->Position = 0;
	for(int i=0,n=fs->Size/100;i<n;++i){
		fs->ReadBuffer(buf,100);
		CmdRE->Text = CmdRE->Text + buf;}
	int n = fs->Size%100;
	fs->ReadBuffer(buf,n);
	buf[n]='\0';
	CmdRE->Text = CmdRE->Text + buf;
	delete fs;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ExitNClick(TObject *Sender)
{
	SaveN->Click();
	Logos->FreeList();
	Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SaveAsNClick(TObject *Sender)
{
	if(CmdRE->Text.Trim().IsEmpty())
		return;
	if(SaveDlg->Execute())
		FileName = SaveDlg->FileName;
	else
		return;
	SaveFile(FileName);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EditNClick(TObject *Sender)
{
	PageNb->ActivePage = "Edit";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CloseNClick(TObject *Sender)
{
	CmdRE->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::HelpNClick(TObject *Sender)
{
	PageNb->ActivePage = "Help";
}
//---------------------------------------------------------------------------




void __fastcall TForm1::ShowPResize(TObject *Sender)
{
	ViewPB->Repaint();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::CmdRESaveClipboard(TObject *Sender, int NumObjects,
      int NumChars, bool &SaveClipboard)
{
	TRichEdit *re = static_cast<TRichEdit*>(Sender);
	if(re->SelText.IsEmpty())
		return;
	Buf = re->SelText;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PasteNClick(TObject *Sender)
{
	if(cb->AsText.IsEmpty())
		return;
	CmdRE->PasteFromClipboard();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CutNClick(TObject *Sender)
{
	if(CmdRE->SelText.IsEmpty())
		return;
	CmdRE->CopyToClipboard();
	CmdRE->ClearSelection();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CopyNClick(TObject *Sender)
{
	if(CmdRE->SelText.IsEmpty())
		return;
	CmdRE->CopyToClipboard();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DeleteNClick(TObject *Sender)
{
	if(CmdRE->SelText.IsEmpty())
		return;
	CmdRE->ClearSelection();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CmdREKeyPress(TObject *Sender, char &Key)
{
//	TRichEdit *re = static_cast<TRichEdit*>(Sender);
//	static int start=0;
//	int len;
//	static AnsiString s;
//	switch(Key){
//		case ' ':case '\t':case '\r':case '\n':
//			start = 0;
//			break;
//		default:
//			start ++;
//			len = re->Text.Length();
//			s = re->Text.SubString(len-start,start).LowerCase();
//			re->SelStart = len - start;
//			if(CmdWord.Pos(s))
//				re->SelAttributes->Color = clBlue;
//			else
//				re->SelAttributes->Color = clBlack;
//			re->SelLength = start;
//			re->ClearSelection();
//			re->SelText = s;
//			}
//
}
//---------------------------------------------------------------------------


void __fastcall TForm1::CmdREKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
//	TRichEdit *re = static_cast<TRichEdit*>(Sender);
//	static int start=0;
//	int len;
//	static AnsiString s;
//	switch(Key){
//		case ' ':case '\t':case '\r':case '\n':
//			start = 0;
//			break;
//		case '{':case '}':
//			re->SelStart = re->GetTextLen() - 1;
//			re->SelLength = 1;
//			re->ClearSelection();
//			re->SelAttributes->Style<< fsBold;
//			re->SelText = Key;
//			re->SelAttributes->Style >> fsBold;
//			start = 0;
//			break;
//		default:
//			if(Key<32)
//				break;
//			start ++;
//			len = re->GetTextLen();
//			s = re->Text.SubString(len-start,start).LowerCase();
//			re->SelStart = re->GetTextLen() - start;
//			//re->SelStart = len - start;
//			re->SelLength = start;
//			re->ClearSelection();
//			if(CmdWord.Pos(s))
//				re->SelAttributes->Color = clBlue;
//			else
//				re->SelAttributes->Color = clBlack;
//			re->SelText = s;
//			}
//
//
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -