c++builder动态给fastreport的memo、checkbox、picture赋值

来源:互联网 发布:程序员试用期个人总结 编辑:程序博客网 时间:2024/05/01 03:54

#include <iostream>

#include <list>

using namespace std;
typedef TfrxCheckBoxView* PFCBV;
typedef list<PFCBV> PFCBVLIST;
typedef list<PFCBV>::iterator PFCBVLIST_IT;


/****************************************************************************

/*    功能:为fastreport报表中的Edit赋值的通用方法
/*    参数:1、AnsiString表示数据库标字段名   2、fastreport中edit控件的name
/*  返回值:void
/***************************************************************************/
void TForm1::SetEdtTypeOfValueProc(AnsiString str1,AnsiString str2,BOOL flag){
    if((str1=="") && (str2=="")){
        return;
    }
    TField* pfield= frxdbdtst1->GetDataSet()->FieldByName(str1);
    AnsiString strtmp = pfield->AsString.Trim();
    TfrxMemoView* pfmv = (TfrxMemoView*)frxrprt1 ->FindObject(str2);
    if(strtmp==""){
        pfmv->Memo->Text = " ";
        return;
    }
    if(flag){
          AnsiString strOutDate="";
          FormatDateStringBySeparativeSign(strtmp,"-",strOutDate);
          pfmv->Memo->Text = strOutDate;
    }else{
          pfmv->Memo->Text = strtmp;
    }

}

/****************************************************************************
/*    功能:为fastreport报表中的CheckBox赋值
/*    参数:1、const AnsiString表示每组checkBox对应的字段名   2、const int表示每组CheckBox在tcl数组中的起始索引
/*          3、const int表示每组CheckBox在tcl数组中的终止索引
/*  返回值:BOOL  false:表示字段名为空   true表示成功
/***************************************************************************/
BOOL TForm1::SetValueForCheckBoxInReportCommomFunc(const AnsiString strFieldName,const int nStartIndex,const int nEndIndex){
        if(strFieldName==""){
                return false;
        }
        PFCBVLIST pfcbList;
        PFCBVLIST_IT it;
        int i;
        for(i=nStartIndex;i<=nEndIndex;i++){
                pfcbList.push_back((TfrxCheckBoxView*)frxrprt1->FindObject(tc1[i].strName));
        }
        TField* pfield = frxdbdtst1->DataSet->FieldByName(strFieldName);
        AnsiString str = pfield->AsString.Trim();
        for(i=nStartIndex,it=pfcbList.begin();it!=pfcbList.end() && i<=nEndIndex;it++,i++){
                if(str==this->tc1[i].strLable){
                     (*it)->Checked=True;
                }else{
                     (*it)->Checked=False;
                }
        }
        return true;
}

/****************************************************************************
/*    功能:保存fastreport报表中的每个CheckBox的lable和name到tcl数组中
/*    参数:无
/*  返回值:void
/***************************************************************************/
void TForm1::SaveCheckBoxLableAndNameInReport(){
       tc1[0].strLable="身份证";
       tc1[0].strName="CBaZJLXaSFZ";
       tc1[1].strLable="户口本";
       tc1[1].strName="CBaZJLXaHKB";
       tc1[2].strLable="护照";
       tc1[2].strName="CBaZJLXaHZ";


       tc1[3].strLable="已婚";
       tc1[3].strName="CBaHYZKaYH";
       tc1[4].strLable="未婚";
       tc1[4].strName="CBaHYZKaWH";
       tc1[5].strLable="离异";
       tc1[5].strName="CBaHYZKaLY";
       tc1[6].strLable="丧偶";
       tc1[6].strName="CBaHYZKaSO";


       tc1[7].strLable="在职";
       tc1[7].strName="CBaRYZTaZZ";
       tc1[8].strLable="退休";
       tc1[8].strName="CBaRYZTaTX";
       tc1[9].strLable="其他";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/****************************************************************************
/*    功能:为fastreport中的picture控件赋值
/*    参数:1、AnsiString表示报表中pictrue控件的name  2、AnsiString表示照片的名称
/*  返回值:BOOL  false表示picture控件为空  true表示不空
/***************************************************************************/
BOOL TForm1::PrintPictureInReport(const AnsiString picControlName,const AnsiString strPictureName){
                if(picControlName=="" || strPictureName==""){
                        return False;
                }
                TField* pfield= frxdbdtst1->GetDataSet()->FieldByName(strPictureName);//照片名称是数据库表中的一个字段
                TfrxPictureView *pfpv = (TfrxPictureView*)frxrprt1->FindObject(picControlName);
                if(pfield->AsString!=""){
                        AnsiString str;
                        GetPothoOrSLTDir(pfield->AsString.Trim(),str);//获得照片的本地路径
                        if(FileExists(str)){
                                pfpv->Visible = True;
                                pfpv->Picture->LoadFromFile(str); //加载照片
                        }else{
                                pfpv->Visible = False;
                                pfpv->Picture = NULL;
                        }
                }else{
                        pfpv->Visible = False;
                        pfpv->Picture = NULL;
                }
                return True;
}

/****************************************************************************
/*    功能:根据图片、缩略图文件名得到其存放路径
/*    参数:AnsiString图片、缩略图文件名 AnsiString&表示获得的图片、缩略图的存放路径字符串 
/*  返回值:BOOL false表示失败    true表示成功
/***************************************************************************/
BOOL TForm1::GetPothoOrSLTDir(AnsiString strPicName,AnsiString& outStr){
        if(ReadFileToMemoByFileName(mmo1,"xxxx.ini")){   //把文件读到memo中
                return false;
        }
        AnsiString str1 = mmo1->Lines->Strings[13];
        int n = str1.Pos("]");
        str1 = str1.SubString(n+1,str1.Length()-n+2)+strPicName+".jpg";
        outStr = str1.Trim();
        return true;
}

/****************************************************************************
/*    功能:根据指定的文件名读取文件到memo中
/*    参数:TMemo* 表示要保存文件的Memo控件指针;AnsiString表示指定的文件名
/*  返回值:int   0:正常   -1:文件名为空    1:文件不存在  2:内存申请失败
/***************************************************************************/
int TForm1::ReadFileToMemoByFileName(TMemo* pMemo,AnsiString strFileName){
        if(strFileName==""){
                return -1;
        }
        AnsiString ExePath = ExtractFilePath(ParamStr(0));
        ExePath += strFileName;
        if(!FileExists(ExePath)){
               return 1;
        }
        int iFileHandle;
        int iFileLength;
        char* pszBuffer;
        pMemo->Clear();
try
{
iFileHandle = FileOpen(ExePath, fmOpenRead);
iFileLength = FileSeek(iFileHandle, 0, 2);
                FileSeek(iFileHandle, 0, 0);
pszBuffer = new char[iFileLength + 1];
                if(pszBuffer==NULL){
                     return 2;
                }
FileRead(iFileHandle, pszBuffer, iFileLength);
FileClose(iFileHandle);
                String str(pszBuffer);
                pMemo->Lines->Clear();
pMemo->Lines->Text = str;
delete[] pszBuffer;
}
catch (...)
{
                ExePath += "  文件读取失败!!!";
Application->MessageBox(ExePath.c_str(),"File Error", IDOK);
}
        return 0;
}