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

📄 linegrid.cpp

📁 C++写的POS前台程序
💻 CPP
字号:
/*******************************************************************************
	模  块:	网格行编辑.
	功  能:	网格上进行一行编辑.
	程序员:	雷中南.
	版  本:	v1.1
	时  间:	1999-05-18
*******************************************************************************/

#include <string.h>
#include <graphics.h>
#include "linegrid.h"

//构造函数.
LineGrid::LineGrid(struct RECT r, char *data[], int colwidth[], int cols, int mode)
{
	//获取边框
	Rect = r;

	//只读运行模式.将不显示编辑框.
	Mode = mode;

	//列数.
	Cols = cols;

	//当前列.
	Col = 0;

	for(int i=0; i<Cols; i++)
	{
		//列宽.
		ColWidth[i] = colwidth[i];

		//复制数据.字符串分配了显示字符数一倍的内存.
		Data[i] = new char[ColWidth[i]*2];

		//如果字符串长度小于正常长度.
		if(strlen(data[i]) <= ColWidth[i]*2-1)
		{
			strcpy(Data[i], data[i]);
		}
		else//如果字符串超长度.
		{
			strncpy(Data[i], data[i], ColWidth[i]*2-1);
			Data[i][ColWidth[i]*2-1] = NULL;
		}
	}

	//不可见.
	VisibleState = L_NO;
}

//析构函数.
LineGrid::~LineGrid()
{
	Hide();
	for(int i=0; i<Cols; i++)
	{
		//释放文本字符串.
		delete []Data[i];
	}
}

//画一行.这里不包含显示全部数据,背景是透明的.只画框线.
void
LineGrid::DrawBox()
{
	setviewport(0,0,639,479,1);
	setviewport(Rect.Left,Rect.Top,Rect.Left+Rect.Width,Rect.Top+Rect.Height,1);
	//设置异或模式.
	setwritemode(XOR_PUT);
	//画外边框.
	setcolor(7);
	rectangle(1, 1, Rect.Width - 1, Rect.Height - 1);
	//按单元格画竖线.
/*	int left = 0;
	for(int i=0; i<Cols; i++)
	{
		left = left + ColWidth[Col]*8;
		line(left, 0, left, Rect.Height);
	}
*/	//恢复原来画线模式.
	setwritemode(COPY_PUT);
}

//显示行.
void
LineGrid::Show()
{
	//如果行不可见,显示它.
	if(VisibleState == L_NO)
	{
		//改变状态.
		VisibleState = L_YES;
		//以异或模式重画.
		DrawBox();
	}
}

//隐藏行.
void
LineGrid::Hide()
{
	//如果它是可见的,隐藏它.
	if(VisibleState == L_YES)
	{
		//改变状态.
		VisibleState = L_NO;
		//异或模式重画.
		DrawBox();
	}
}

//运行行.
void
LineGrid::Run()
{
	DoIt();
}

//处理事件.
void
LineGrid::DoIt()
{
	//只读模式的键值.
	int KeyCode=0;
	if(Mode == L_NO)//非只读模式.
	{
		//获取编辑的文本框的左边界.
		int left = 0;
		for(int i=0; i<Col; i++)
		{
			//按单元格宽度累加.
			left = left + (ColWidth[i])*8 + 1;
		}
		//产生活动单元格.
		Cell = new Edit(CreateRect(Rect.Left + left + 1, Rect.Top + 1,
											(ColWidth[Col])*8 + 2, Rect.Height - 2));
		Cell->ReturnFlag = L_YES;
		Cell->SetBkColor(15);
		Cell->SetFrameColor(7);
		//显示单元格.
		Cell->Show();
retry:
		//将文本写入单元格.
		Cell->SetText(Data[Col]);
		ChangeFlag = L_NO;
		//运行单元格.
		Cell->Run();
		//如果文本没有变化。
		if(strcmp(Data[Col], Cell->Text())!=0)
		{
			ChangeFlag=L_YES;
		}
		//获取返回的事件.
		unsigned int Event;
		GetEvent(Event);
		switch(Event)
		{
			//如果是退出的代码,恢复原来的文本.
			case EV_QUIT:
				if(strcmp(Data[Col], Cell->Text())==0)
				{
					break;
				}
				goto retry;
		}
		PutEvent(Event);
		//按返回字符串长度决定如何修改字符串.
		if(strlen(Cell->Text()) <= ColWidth[Col]*2-1)
		{
			//全部拷贝.
			strcpy(Data[Col], Cell->Text());
		}
		else
		{
			//拷贝允许的最大长度.
			strncpy(Data[Col], Cell->Text(), ColWidth[Col]*2-1);
			Data[Col][ColWidth[Col]*2-1] = NULL;
		}
		//删除编辑框.
		delete Cell;
	}
	else//只读模式.
	{
		for(;;)
		{
			//读取一个键.
			KeyCode = GetKey();
			switch ( KeyCode )
			{
				case KEY_ESC:
					//退出.
					PutEvent(EV_QUIT);
					return;
				case KEY_LEFT:
				case KEY_UP:
					//上一个.
					PutEvent(EV_PREV);
					Show();
					return;
				case KEY_RIGHT:
				case KEY_DOWN:
					//下一个.
					PutEvent(EV_NEXT);
					Show();
					return;
				default:
					PutEvent( KeyCode );
					return;
			}
		}
	}
}

⌨️ 快捷键说明

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