📄 wnquery.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "WnQuery.h"
#include "gvar.h"
#include "commfunction.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmWnQuery *frmWnQuery;
//---------------------------------------------------------------------------
__fastcall TfrmWnQuery::TfrmWnQuery(TComponent* Owner)
: TForm(Owner)
{
//Application->MessageBox("万能查询器不能以这种方式启动!","出错信息",MB_OK);
//Close();
}
//---------------------------------------------------------------------------
__fastcall TfrmWnQuery::TfrmWnQuery(TComponent* Owner,AnsiString FCaption,AnsiString SqlStr)
: TForm(Owner)
{
FColName=new TStringList;
Founded=false;
CurPos.x=1;
CurPos.y=1;
bbOK->Enabled=false;
if(!FCaption.IsEmpty())
this->Caption=FCaption;
if(SqlStr.IsEmpty())
{
Application->MessageBox("查询语句不能为空!","出错信息",MB_OK);
Close();
}
DecQueryString(SqlStr);
FillGridWithData();
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::DecQueryString(AnsiString SqlStr)
{
AnsiString CurStr,ValStr;
FColCount=SqlStr.SubString(1,3).ToInt();
FColName->Clear();
CurStr=SqlStr.SubString(4,SqlStr.Length());
FColName->Add("序");
for(int i=0;i<FColCount;i++)
{
for(int j=1;j<=CurStr.Length();j++)
{
if(CurStr[j]=='[')
{
ValStr="";
continue;
}
else if(CurStr[j]==']')
{
FColName->Add(ValStr);
CurStr=CurStr.SubString(j+1,CurStr.Length());
break;
}
else
ValStr+=CurStr[j];
}
}
FSqlStr=CurStr;
sgResult->ColCount=FColCount+1;
sgResult->Rows[0]->Assign(FColName);
sgResult->RowCount=2;
sgResult->FixedRows=1;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::FillGridWithData()
{
TStringList *ItemStr;
TComResultSet *WnRs;
int CurRow,ErrNum;
try
{
ItemStr=new TStringList;
sgResult->RowCount=2;
Founded=false;
bbOK->Enabled=false;
WnRs=NewResultSet();
WnRs->Open(FSqlStr,"");
if(WnRs->RecordCount<1)
sgResult->RowCount=2;
else
sgResult->RowCount=WnRs->RecordCount+1;
CurRow=1;
while(WnRs->Eof == 0)
{
Founded=true;
ItemStr->Add(CurRow);
for(int i=0;i<FColCount;i++)
ItemStr->Add(WnRs->FieldByIndex(i));
sgResult->Rows[CurRow]->Assign(ItemStr);
CurRow++;
ItemStr->Clear();
WnRs->MoveNext();
Application->ProcessMessages();
}
WnRs->Close();
}
__finally
{
delete ItemStr;
delete WnRs;
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall TfrmWnQuery::GetColData(int ACol)
{
if(ACol>FColCount || (!Founded))
return "";
else
return sgResult->Cells[ACol][sgResult->Row];
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::bbRefreshClick(TObject *Sender)
{
try
{
mmHint->Visible=false;
StartWaitForm("正在刷新数据,请稍候...");
FillGridWithData();
}
__finally
{
EndWaitForm();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::sgResultDblClick(TObject *Sender)
{
mmHint->Visible=false;
bbOK->Click();
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::sgResultClick(TObject *Sender)
{
mmHint->Visible=false;
if(Founded)
bbOK->Enabled=true;
else
bbOK->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::sbFirstClick(TObject *Sender)
{
mmHint->Visible=false;
if(Founded)
sgResult->Row=1;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::sbNextClick(TObject *Sender)
{
mmHint->Visible=false;
if(Founded)
{
if(sgResult->Row<sgResult->RowCount-1)
sgResult->Row=sgResult->Row+1;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::sbLastClick(TObject *Sender)
{
mmHint->Visible=false;
if(Founded)
sgResult->Row=sgResult->RowCount-1;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::sbPriorClick(TObject *Sender)
{
mmHint->Visible=false;
if(Founded)
{
if(sgResult->Row>1)
sgResult->Row=sgResult->Row-1;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::GridPos(bool Dir)
{
int FStrLen,index;
AnsiString FindStr,MatchStr;
if(!Founded)
return;
if(etSearch->Text.IsEmpty())
return;
for(int i=1;i<=etSearch->Text.Length();i++)
{
if(FindStr.IsEmpty())
{
if(MatchStr.IsEmpty())
{
if(etSearch->Text[i]=='?' || etSearch->Text[i]=='*')
MatchStr=etSearch->Text[i];
else if(etSearch->Text[i]==' ')
continue;
else
FindStr+=etSearch->Text[i];
}
else
{
if(etSearch->Text[i]=='*')
{
if(MatchStr[1]=='?')
MatchStr="*";
}
else if(etSearch->Text[i]=='?')
{
if(MatchStr[1]=='?')
MatchStr+=etSearch->Text[i];
}
else
FindStr+=etSearch->Text[i];
}
}
else
FindStr+=etSearch->Text[i];
}
TStringList *FindList=new TStringList;
if(rbBMatch->Checked)
{
FStrLen=FindStr.Length();
CurPos.x=sgResult->Col;
CurPos.y=sgResult->Row;
if(Dir)
{
if(MatchStr.IsEmpty())
{
for(int i=CurPos.y+1;i<sgResult->RowCount;i++)
FindList->Add(sgResult->Cells[sgResult->Col][i].SubString(1,FStrLen));
index=FindList->IndexOf(FindStr);
if(index!=-1)
{
sgResult->Row=CurPos.y+index+1;
sgResult->Col=CurPos.x;
}
}
else
{
if(MatchStr=="*")
{
for(int i=CurPos.y+1;i<sgResult->RowCount;i++)
{
index=sgResult->Cells[sgResult->Col][i].UpperCase().AnsiPos(FindStr.UpperCase());
if(index>0)
{
sgResult->Row=i;
sgResult->Col=CurPos.x;
break;
}
}
}
else
{
for(int i=CurPos.y+1;i<sgResult->RowCount;i++)
FindList->Add(sgResult->Cells[sgResult->Col][i].SubString(MatchStr.Length()+1,FStrLen));
index=FindList->IndexOf(FindStr);
if(index!=-1)
{
sgResult->Row=CurPos.y+index+1;
sgResult->Col=CurPos.x;
}
}
}
}
else
{
if(MatchStr.IsEmpty())
{
for(int i=CurPos.y-1;i>0;i--)
FindList->Add(sgResult->Cells[sgResult->Col][i].SubString(1,FStrLen));
index=FindList->IndexOf(etSearch->Text);
if(index!=-1)
{
sgResult->Row=CurPos.y-index-1;
sgResult->Col=CurPos.x;
}
}
else
{
if(MatchStr=="*")
{
for(int i=CurPos.y-1;i>0;i--)
{
index=sgResult->Cells[sgResult->Col][i].UpperCase().AnsiPos(FindStr.UpperCase());
if(index>0)
{
sgResult->Row=i;
sgResult->Col=CurPos.x;
break;
}
}
}
else
{
for(int i=CurPos.y-1;i>0;i--)
FindList->Add(sgResult->Cells[sgResult->Col][i].SubString(MatchStr.Length()+1,FStrLen));
index=FindList->IndexOf(FindStr);
if(index!=-1)
{
sgResult->Row=CurPos.y-index-1;
sgResult->Col=CurPos.x;
}
}
}
}
}
else
{
CurPos.x=sgResult->Col;
CurPos.y=sgResult->Row;
if(Dir)
{
if(MatchStr.IsEmpty())
{
for(int i=CurPos.y+1;i<sgResult->RowCount;i++)
FindList->Add(sgResult->Cells[sgResult->Col][i]);
index=FindList->IndexOf(etSearch->Text);
if(index!=-1)
{
sgResult->Row=CurPos.y+index+1;
sgResult->Col=CurPos.x;
}
}
else
{
if(MatchStr=="*")
{
for(int i=CurPos.y+1;i<sgResult->RowCount;i++)
{
index=sgResult->Cells[sgResult->Col][i].AnsiPos(FindStr);
if(index>0)
{
sgResult->Row=i;
sgResult->Col=CurPos.x;
break;
}
}
}
else
{
for(int i=CurPos.y+1;i<sgResult->RowCount;i++)
FindList->Add(sgResult->Cells[sgResult->Col][i].SubString(MatchStr.Length()+1,FStrLen));
index=FindList->IndexOf(FindStr);
if(index!=-1)
{
sgResult->Row=CurPos.y+index+1;
sgResult->Col=CurPos.x;
}
}
}
}
else
{
if(MatchStr.IsEmpty())
{
for(int i=CurPos.y-1;i>0;i--)
FindList->Add(sgResult->Cells[sgResult->Col][i]);
index=FindList->IndexOf(etSearch->Text);
if(index!=-1)
{
sgResult->Row=CurPos.y-index-1;
sgResult->Col=CurPos.x;
}
}
else
{
if(MatchStr=="*")
{
for(int i=CurPos.y-1;i>0;i--)
{
index=sgResult->Cells[sgResult->Col][i].AnsiPos(FindStr);
if(index>0)
{
sgResult->Row=i;
sgResult->Col=CurPos.x;
break;
}
}
}
else
{
for(int i=CurPos.y-1;i>0;i--)
FindList->Add(sgResult->Cells[sgResult->Col][i].SubString(MatchStr.Length()+1,FStrLen));
index=FindList->IndexOf(FindStr);
if(index!=-1)
{
sgResult->Row=CurPos.y-index-1;
sgResult->Col=CurPos.x;
}
}
}
}
}
delete FindList;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::bbSearchNextClick(TObject *Sender)
{
if(mmHint->Visible)
mmHint->Visible=false;
GridPos(true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::bbSearchPriorClick(TObject *Sender)
{
mmHint->Visible=false;
GridPos(false);
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::Panel1Click(TObject *Sender)
{
mmHint->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::Label1Click(TObject *Sender)
{
mmHint->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::etSearchClick(TObject *Sender)
{
mmHint->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::rbBMatchClick(TObject *Sender)
{
mmHint->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::rbEMatchClick(TObject *Sender)
{
mmHint->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::Panel2Click(TObject *Sender)
{
mmHint->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::puHintClick(TObject *Sender)
{
mmHint->BringToFront();
mmHint->Visible=true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmWnQuery::FormClose(TObject *Sender,
TCloseAction &Action)
{
this->Visible=false;
Application->ProcessMessages();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -