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

📄 expressionpage.cpp

📁 输入一个公式
💻 CPP
字号:
// ExpressionPage.cpp : 实现文件
//

#include "stdafx.h"
#include "ExpressionPage.h"


// CExpressionPage 对话框

IMPLEMENT_DYNAMIC(CExpressionPage, CPropertyPage)
CExpressionPage::CExpressionPage()
	: CPropertyPage(CExpressionPage::IDD)
	, sExpression(_T(""))
	, commend(_T(""))
	, layerSel(0)
	, pVariable(NULL)
{
}

CExpressionPage::~CExpressionPage()
{
	ClearFunctionList();
}

void CExpressionPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_Expression, sExpression);
	DDX_Control(pDX, IDC_LIST_Function, functionList);
	DDX_Text(pDX, IDC_STATIC_Commend, commend);
	DDX_CBIndex(pDX, IDC_COMBO_FunctionLayer, layerSel);
	DDX_Control(pDX, IDC_EDIT_Expression, editExpression);
	DDX_Control(pDX, IDC_LIST_Variable, listVariable);
}


BEGIN_MESSAGE_MAP(CExpressionPage, CPropertyPage)
	ON_CBN_SELCHANGE(IDC_COMBO_FunctionLayer, OnCbnSelchangeComboFunctionlayer)
	ON_LBN_SELCHANGE(IDC_LIST_Function, OnLbnSelchangeListFunction)
	ON_LBN_DBLCLK(IDC_LIST_Function, OnLbnDblclkListFunction)
	ON_LBN_DBLCLK(IDC_LIST_Variable, OnLbnDblclkListVariable)
END_MESSAGE_MAP()


// CExpressionPage 消息处理程序

void CExpressionPage::OnCbnSelchangeComboFunctionlayer()
{
	UpdateData();
	ClearFunctionList();
	switch(layerSel)
	{
	case 0:		//全部函数
		AddFunction();
		AddOperator();
		AddLogicalOperation();
		break;
	case 1:		//常用运算符
		AddOperator();
		break;
	case 2:		//常用函数
		AddFunction();
		break;
	case 3:		//逻辑运算符
		AddLogicalOperation();
		break;
	}
	functionList.SetCurSel(0);
	OnLbnSelchangeListFunction();
}

void CExpressionPage::AddFunction(void)
{
	AddString(IDS_STRING_SIN);
	AddString(IDS_STRING_COS);
	AddString(IDS_STRING_TG);
	AddString(IDS_STRING_CTG);
	AddString(IDS_STRING_SQRT);
	AddString(IDS_STRING_EXP);
	AddString(IDS_STRING_LOG);
	AddString(IDS_STRING_ASIN);
	AddString(IDS_STRING_ACOS);
	AddString(IDS_STRING_ATG);
	AddString(IDS_STRING_FABS);
}

void CExpressionPage::AddLogicalOperation(void)
{
	AddString(IDS_STRING_LessThan);
	AddString(IDS_STRING_MoreThan);
	AddString(IDS_STRING_NOT);
}

void CExpressionPage::AddOperator(void)
{
	AddString(IDS_STRING_Add);
	AddString(IDS_STRING_SubtractionSign);
	AddString(IDS_STRING_Multiply);
	AddString(IDS_STRING_Divide);
	AddString(IDS_STRING_Parentheses);
	AddString(IDS_STRING_POW);
}

void CExpressionPage::AddString(UINT nID)
{
	CString str;
	str.LoadString(nID);
	int i=str.Find(";");
	if(i>0)
	{
		int sel=functionList.AddString(str.Mid(0,i));
		char *value=new char[256];
		sprintf(value,"%s",str.Mid(i+1));
		functionList.SetItemDataPtr(sel,(void*)value);
	}
}

void CExpressionPage::OnLbnSelchangeListFunction()
{
	UpdateData();
	int sel=functionList.GetCurSel();
	if(sel<0)return;
	char *value=(char*)functionList.GetItemDataPtr(sel);
	if(value) commend=value;
	UpdateData(0);
}

void CExpressionPage::OnLbnDblclkListFunction()
{
	UpdateData();
	int sel=functionList.GetCurSel();
	if(sel<0)return;
	CString str;
	functionList.GetText(sel,str);
	sel=commend.Find(";");
	if(sel>0) str=commend.Mid(0,sel);

	editExpression.SetFocus();
	CPoint point, pt;
	int fvl = editExpression.GetFirstVisibleLine();
	pt = editExpression.GetCaretPos();
	int n = editExpression.CharFromPos(pt);
	int nLineIndex = HIWORD(n);
	int nCharIndex = LOWORD(n);
	sExpression.Insert(nCharIndex, str);
	UpdateData(0);

	n=str.Find("(");
	if(n<0) nCharIndex+=1;
	else nCharIndex+=n+1;
	editExpression.SetSel(nCharIndex,nCharIndex);
}

BOOL CExpressionPage::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	OnCbnSelchangeComboFunctionlayer();
	OnLbnSelchangeListFunction();

	//增加变量
	if(pVariable && pVariable->GetCount()>0)
	{
		CValue* value;
		CString str;
		POSITION p=pVariable->GetStartPosition();
		while(p)
		{
			pVariable->GetNextAssoc(p,str,value);
			listVariable.AddString(str);
		}
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常:OCX 属性页应返回 FALSE
}

void CExpressionPage::ClearFunctionList(void)
{
	if(functionList.m_hWnd==NULL)return;
	if(functionList.GetCount()==0)return;
	char *value;
	for(int i=0;i<(int)functionList.GetCount();i++)
	{
		value=(char*)functionList.GetItemDataPtr(i);
		delete value;
	}
	functionList.ResetContent();
}

void CExpressionPage::OnLbnDblclkListVariable()
{
	UpdateData();
	int sel=listVariable.GetCurSel();
	if(sel<0)return;
	CString str;
	listVariable.GetText(sel,str);

	editExpression.SetFocus();
	CPoint point, pt;
	int fvl = editExpression.GetFirstVisibleLine();
	pt = editExpression.GetCaretPos();
	int n = editExpression.CharFromPos(pt);
	int nLineIndex = HIWORD(n);
	int nCharIndex = LOWORD(n);
	sExpression.Insert(nCharIndex, str);
	UpdateData(0);

	nCharIndex+=str.GetLength();
	editExpression.SetSel(nCharIndex,nCharIndex);
}

⌨️ 快捷键说明

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