📄 editacc.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "EditAcc.h"
#include "dido.h"
#include "option.h"
#include "CBuilder.h"
#include "Right.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//设置编辑框内容
static void SetEditText(TEditAccountForm *te,void *pHead,int nIndex);
//获取编辑框内容
static int GetEditText(TEditAccountForm *te,void *pHead,int nIndex);
//获取编辑单元
extern int GetTextUnit(TEditAccountForm *te,ACCOUNT *a);
//检查密码
static int CheckEditPassword(char *pszPass1,char *pszPass2);
//检查用户名
static int CheckEditName(char *pszName);
//获取相同编号(-1=>无,0=>原来,1=>其它记录)
static int GetOtherAccountOnTList(void *pHead,ACCOUNT *Sour,int nOld);
//获取权限检查框
static int GetRightCheckBox(TEditAccountForm *te,void *pAcc);
//设置权限检查框
static int SetRightCheckBox(TEditAccountForm *te,void *pAcc);
//设置权限检查框项目
static int SetRightCheckItem(TEditAccountForm *te,void *pAcc);
//---------------------------------------------------------------------------
__fastcall TEditAccountForm::TEditAccountForm(TComponent* Owner)
: TForm(Owner)
{
}
//修改帐号
int ReworkAccount(TComponent* Owner,void *pHead,int nIndex)
{
TEditAccountForm *te;
int rc;
rc=-1;
te = new TEditAccountForm(Owner);
if(te!=NULL){
SetEditText(te,pHead,nIndex);
if(te->ShowModal()==IDOK)
rc=te->m_nIndex;
delete te;
}
return(rc);
}
//设置编辑框内容
void SetEditText(TEditAccountForm *te,void *pHead,int nIndex)
{
char *p;
ACCOUNT a;
int i,n;
te->m_pHead=pHead; te->m_nIndex=nIndex;
n=GetDotTextOnTList(pHead,nIndex,&a,sizeof(a));
if(n==0)//增加
te->m_EditBut->Enabled=FALSE;
else te->m_AddBut->Enabled=FALSE;//修改
te->m_NameEdit->SetTextBuf(a.szName);
te->m_NewEdit->SetTextBuf(a.szPassword);
te->m_OkEdit->SetTextBuf(a.szPassword);
te->m_LevelCoBox->Clear();
for(i=0;;i++){
p = GetAccTextWithLevel(i);//获取用户级别的文本
if(p==NULL)break;
te->m_LevelCoBox->Items->Add(p);
}
n = a.nLevel;
if(n>=i)n=0;
te->m_LevelCoBox->ItemIndex=n;
SetRightCheckBox(te,&a);//设置权限检查框
te->m_LevelCoBoxChange(te->m_LevelCoBox);
}
//将编辑框内容写入帐号
int GetEditText(TEditAccountForm *te,void *pHead,int nIndex)
{
static char pszText[]="添加/修改的帐号:\"%s\"\n\n"
"已经有重复!";
char szBuf[100],szPass1[20],szPass2[20];
ACCOUNT a;
int nCur;
te->m_NameEdit->GetTextBuf(szBuf,sizeof(szBuf));
te->m_NewEdit->GetTextBuf(szPass1,sizeof(szPass1));
te->m_OkEdit->GetTextBuf(szPass2,sizeof(szPass2));
if(CheckEditPassword(szPass1,szPass2)==FALSE){
te->m_NewEdit->SetFocus();
return(-1);
}
else if(CheckEditName(szBuf)==FALSE){
te->m_NameEdit->SetFocus();
return(-1);
}
GetTextUnit(te,&a);
nCur= GetOtherAccountOnTList(pHead,&a,nIndex);
if(nIndex>=0 && nCur<=0){//修改
SetDotTextOnTList(pHead,nIndex,&a,sizeof(a));
nCur=nIndex;
}
else if(nCur<0){//不存在该记录,添加
if(AddContentToTList(pHead,&a,sizeof(a)))
nCur=LookCount(pHead)-1;
else nCur=-2;
}
else{ //重复,提示错误
wsprintf(szBuf,pszText,a.szName);
Application->MessageBox(szBuf,GetMainTitle(),
MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
te->m_NameEdit->SetFocus();
nCur=-1;
}
return(nCur);
}
//获取编辑单元
int GetTextUnit(TEditAccountForm *te,ACCOUNT *pAcc)
{
int n;
ZeroMemory(pAcc,sizeof(ACCOUNT));
te->m_NameEdit->GetTextBuf(pAcc->szName,sizeof(pAcc->szName));
te->m_NewEdit->GetTextBuf(pAcc->szPassword,sizeof(pAcc->szPassword));
n = te->m_LevelCoBox->ItemIndex;
if(n<0)n=0;
pAcc->nLevel=(BYTE)n;
GetRightCheckBox(te,pAcc);
return(TRUE);
}
//检查密码
int CheckEditPassword(char *pszPass1,char *pszPass2)
{
static char pszText[]="密码输入错误!\n\n"
"\"新密码\"和\"确认密码\"必须相同!";
int rc;
rc=TRUE;
if(lstrcmpi(pszPass1,pszPass2)!=NULL){
Application->MessageBox(pszText,GetMainTitle(),
MB_OK|MB_ICONINFORMATION);
rc=FALSE;
}
return(rc);
}
//检查用户名
int CheckEditName(char *pszName)
{
static char pszText[]="用户名输入错误!\n\n"
"\"用户名\"不能为空!";
int rc;
rc=TRUE;
if(pszName[0]==0){
Application->MessageBox(pszText,GetMainTitle(),
MB_OK|MB_ICONINFORMATION);
rc=FALSE;
}
return(rc);
}
//添加
void __fastcall TEditAccountForm::m_AddButClick(TObject *Sender)
{
int rc;
rc = GetEditText(this,m_pHead,-1);//获取编辑内容
if(rc>=0){ ModalResult=IDOK; m_nIndex=rc; }
else { ModalResult=0; }
}
//修改
void __fastcall TEditAccountForm::m_EditButClick(TObject *Sender)
{
int rc;
rc = GetEditText(this,m_pHead,m_nIndex);//获取编辑内容
if(rc>=0){ ModalResult=IDOK; m_nIndex=rc; }
else { ModalResult=0; }
}
void __fastcall TEditAccountForm::m_LevelCoBoxChange(TObject *Sender)
{
ACCOUNT a;
GetDotTextOnTList(m_pHead,m_nIndex,&a,sizeof(a));
a.nLevel = m_LevelCoBox->ItemIndex;
SetRightCheckItem(this,&a);//设置权限检查框项目
}
//获取相同编号(-1=>无,0=>原来,1=>其它记录)
int GetOtherAccountOnTList(void *pHead,ACCOUNT *Sour,int nOld)
{
void *pCur;
ACCOUNT *a;
int i,rc;
char *pszName;
rc=-1;
pszName= Sour->szName;
for(i=0;;i++){
pCur = GetDotOnTList(pHead,i);
if(pCur==NULL)break;
a=LookAccount(pCur);
if(lstrcmpi(a->szName,pszName)==0){
if(i!=nOld){rc=1;break;}//有不同的记录,则停止搜索
else rc=0; //相同的记录,则继续搜索
}
}
return(rc);
}
//获取权限检查框
int GetRightCheckBox(TEditAccountForm *te,void *pAcc)
{
TCheckBox *pChild;
int i,n,rc,nDone;
DWORD nRight;
n=0;
nRight = GetDefaultRight(((ACCOUNT *)pAcc)->nLevel);
if(nRight!=0)SetAllRightOnAcc(pAcc,nRight); //不是自定义级别
else{ //自定义级别,则允许修改
for(i=0;i<50;i++){
pChild = (TCheckBox *)GetChildComponent(te,100+i);
if(pChild!=NULL && pChild->Visible){
rc = pChild->Checked;
nDone = GetRightWithItem(i);
if(nDone>=0) SetOneRightOnAcc(pAcc,nDone,rc);
n++;
}
}
}
return(n);
}
//设置权限检查框
int SetRightCheckBox(TEditAccountForm *te,void *pAcc)
{
TCheckBox *pChild;
char *p;
int i,n,rc;
for(i=n=0;i<50;i++){
pChild = (TCheckBox *)GetChildComponent(te,100+i);
if(pChild==NULL)break;
p = GetRightTextWithItem(i);//获取单项权限名称
if(p!=NULL){
pChild->SetTextBuf(p);
pChild->Show();
n++;
}
else pChild->Hide();
}
return(n);
}
//设置权限检查框项目
int SetRightCheckItem(TEditAccountForm *te,void *pAcc)
{
TCheckBox *pChild;
int i,n,nDone;
int nRight,nEnable;
nRight = GetDefaultRight(((ACCOUNT *)pAcc)->nLevel);
if(nRight!=0){
SetAllRightOnAcc(pAcc,nRight);//设置帐号所有权限许可
nEnable=FALSE; //不是自定义级别
}
else nEnable=TRUE; //自定义级别,则允许修改
for(i=n=0;i<50;i++){
pChild = (TCheckBox *)GetChildComponent(te,100+i);
if(pChild!=NULL&& pChild->Visible){
nDone = GetRightWithItem(i);
nDone = GetOneRightOnAcc(pAcc,nDone);
pChild->Checked=nDone;
pChild->Enabled=nEnable;
n++;
}
}
return(n);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -