www.pudn.com > mfcdraw.zip > Unit1.cpp
//--------------------------------------------------------------------------- #include#include #pragma hdrstop #include "Unit1.h" #include "Unit2.h" #include "Unit3.h" #pragma link "ATStat" #pragma resource "*.dfm" #define TOPPART 2 #define LEFTPART 1 #define RIGHTPART 1 #define HEADHEIGHT 2 #define RULEHEIGHT 2 #define FIXWIDTH 3 #define CENTIMETERPERINCH 2.54 #define METERPERINCH 0.0254 const TColor GradColor[7]={clBlack,clRed,clBlue,clYellow,clLime,clFuchsia,clAqua}; //--------------------------------------------------------------------------- TMainDraw *MainDraw; //--------------------------------------------------------------------------- __fastcall TMainDraw::TMainDraw(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TMainDraw::NewButtonClick(TObject *Sender) { SetupForm->ShowModal() ; if (SetupForm->SetupSuccess) { m_savepara.clear(); InitFromSetup(); Bitmap=new Graphics::TBitmap(); Bitmap->PixelFormat=pf4bit; // TSize1 size; BmpScale(blcselect,depthfrom,depthto); Bitmap->Width=GraphWidth; Bitmap->Height=GraphHeight; //+headheight+ruleheight+BmpScale(toppart); // do you know why i add 60*permeterheight? how sad ,something you won't do but you must do it; ItemsHeight=Bitmap->Height-PixelToCM(HEADHEIGHT+RULEHEIGHT); // if(depthselect==2) // permeterheight= permeterheight=ItemsHeight/depthscale; Bitmap->Height=GraphHeight+60*permeterheight; ScrollBox1->VertScrollBar->Range= Bitmap->Height; ScrollBox1->HorzScrollBar->Range= Bitmap->Width+200; Image1->Picture->Graphic=Bitmap; // Image1->Invalidate(); // CurrentFile=EmptyStr; DrawItemHead(); DrawFrameLine(); if(depthselect==2) DoWithTimeGraduation(); else DoWithDepthGraduation(); DealSQLString(); int temp=m_savepara.size(); for(int i=0;i RDSelect->ItemIndex; blcselect=SetupForm->CBblc->ItemIndex; m_timefrom=SetupForm->TimeFrom->ItemIndex; m_timeto=SetupForm->TimeTo->ItemIndex; depthfrom=atof(SetupForm->DepthFrom->Text.c_str()); depthto=atof(SetupForm->DepthTo->Text.c_str()); if(depthselect==2) { depthfrom=SetupForm->depthfrom; depthto=SetupForm->depthto; depthscale=m_timeto-m_timefrom; } else { depthfrom=atof(SetupForm->DepthFrom->Text.c_str()); depthto=atof(SetupForm->DepthTo->Text.c_str()); depthscale=depthfrom-depthto; } // depthscale=m_timeto-m_timefrom; AnsiString stringtemp[9]; for (int y=1;y StringGrid1->RowCount;y++) { if(SetupForm->StringGrid1->Cells[0][y].IsEmpty()) break; for (int x=0;x StringGrid1->ColCount;x++) { stringtemp[x]=SetupForm->StringGrid1->Cells[x][y]; } m_savepara.insert(m_savepara.end(),SavePara(stringtemp)); } } void __fastcall TMainDraw::N7Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void TMainDraw::BmpScale(int blc,float depthfrom,float depthto) { // PixelsPerInch=96; // size.cx means the bitmap's width int widthtemp=0; depthscale=depthto-depthfrom; int k=m_savepara.size(); for(int i=0;i Canvas->Font->Size; Image1->Canvas->Font->Size=14; switch(depthselect) { case 0: nametemp=" 实时井深"; break; case 1: nametemp=" 迟到井深"; break; case 2: nametemp=" 时 间"; break; } // Image1->Canvas->TextRect(TheRect, TheRect.Left+5, TheRect.Top+5, nametemp); DrawTextInCenterRect(Image1->Canvas->Handle, TheRect,nametemp); //void _fastcall TMainDraw::DrawTextInCenterRect( HDC hDC, RECT rt,AnsiString text) DownRect.top = HeadIniPos.y+PixelToCM(HEADHEIGHT/2); DownRect.left =HeadIniPos.x; DownRect.bottom =HeadIniPos.y+PixelToCM(HEADHEIGHT); DownRect.right = HeadIniPos.x+PixelToCM(FIXWIDTH); if(depthselect!=2) DrawTextInCenterRect(Image1->Canvas->Handle, DownRect,"M"); // Image1->Canvas->TextRect(DownRect, DownRect.Left+5, DownRect.Top+5, "M"); Image1->Canvas->Font->Size=14; for(int i=0;i Canvas->TextRect(TheRect, TheRect.Left+5, TheRect.Top+5, m_savepara[i].definename); DrawTextInCenterRect(Image1->Canvas->Handle, TheRect,m_savepara[i].definename); DownRect.left =DownRect.right; DownRect.right = DownRect.right+PixelToCM(m_savepara[i].trackwidth); DrawTextInCenterRect(Image1->Canvas->Handle, DownRect,m_savepara[i].unitname); // Image1->Canvas->TextRect(DownRect, DownRect.Left+5, DownRect.Top+5, m_savepara[i].unitname); } Image1->Canvas->Font->Size=orfontsize; } void _fastcall TMainDraw::DrawShape( TPoint TopLeft,TPoint BottomRight,TPenMode AMode) { Image1->Canvas->Pen->Mode=AMode; Image1->Canvas->Pen->Color=clBlack; switch(DrawingTool) { case dtLine: { Image1->Canvas->MoveTo(TopLeft.x,TopLeft.y); Image1->Canvas->LineTo(BottomRight.x,BottomRight.y); break; } case dtRectangle: { Image1->Canvas->Rectangle(TopLeft.x,TopLeft.y,BottomRight.x,BottomRight.y); break; } /* case dtRoundRect: { Image1->Canvas->RoundRect(TopLeft.x,TopLeft.y,BottomRight.x,BottomRight.y,(TopLeft.x-BottomRight.x)/2,(TopLeft.y-BottomRight.y)/2); break; } */ case dtText: { // Image1->Canvas->Rectangle(TopLeft.x,TopLeft.y,BottomRight.x,BottomRight.y); Image1->Canvas->MoveTo(TopLeft.x,TopLeft.y); Image1->Canvas->LineTo(BottomRight.x,TopLeft.y); Image1->Canvas->LineTo(BottomRight.x,BottomRight.y); Image1->Canvas->LineTo(TopLeft.x,BottomRight.y); Image1->Canvas->LineTo(TopLeft.x,TopLeft.y); break; } } } void _fastcall TMainDraw::DrawShape( TPoint TopLeft,TPoint BottomRight,int Style,long color,int width,int mode) { // Image1->Canvas->Pen->Mode=AMode; Image1->Canvas->Pen->Width=width; Image1->Canvas->Pen->Color=(Graphics::TColor)color; switch(Style) { case 0: Image1->Canvas->Pen->Style=psSolid; break; case 1: Image1->Canvas->Pen->Style=psDash; break; case 2: Image1->Canvas->Pen->Style=psDot; break; case 3: Image1->Canvas->Pen->Style=psDashDot; break; case 4: Image1->Canvas->Pen->Style=psDashDotDot; break; } switch(mode) { case 0: { Image1->Canvas->MoveTo(TopLeft.x,TopLeft.y); Image1->Canvas->LineTo(BottomRight.x,BottomRight.y); break; } case 1: { Image1->Canvas->Rectangle(TopLeft.x,TopLeft.y,BottomRight.x,BottomRight.y); break; } case 2: { /* Image1->Canvas->MoveTo(TopLeft.x,TopLeft.y); Image1->Canvas->LineTo(BottomRight.x,TopLeft.y); Image1->Canvas->LineTo(BottomRight.x,BottomRight.y); Image1->Canvas->LineTo(TopLeft.x,BottomRight.y); Image1->Canvas->LineTo(TopLeft.x,TopLeft.y); Image1->Canvas->Pen->Width=1; Image1->Canvas->MoveTo(TopLeft.x,BottomRight.y+ruleheight); Image1->Canvas->LineTo(BottomRight.x,BottomRight.y+ruleheight); */ } } } void TMainDraw::DrawFrameLine() { TPoint pointbegin,pointend; float lantemp; lantemp=FIXWIDTH; pointend.x=RuleIniPos.x+ItemsWidth; pointend.y=RuleIniPos.y; DrawShape(RuleIniPos,pointend,0,clBlack,1,0); pointend.x= RuleIniPos.x; pointend.y=RuleIniPos.y+ ItemsHeight+PixelToCM(RULEHEIGHT)+20; DrawShape(HeadIniPos,pointend,0,clBlack,2,0); pointbegin.x=RuleIniPos.x+PixelToCM(FIXWIDTH); pointbegin.y=HeadIniPos.y; pointend.x= pointbegin.x; pointend.y=RuleIniPos.y+ ItemsHeight+PixelToCM(RULEHEIGHT)+20; //test DrawShape(pointbegin,pointend,0,clBlack,2,0); for(int i=0;i Canvas->TextOut(ItemsIniPos.x+80,ItemsIniPos.y+m*permeterheight,AnsiString(m+depthfrom)); } if(fmod(m,25)==0) { pointbegin=Point(WordX,ItemsIniPos.y+m*permeterheight); pointend=Point(WordX-per25meterwidth,ItemsIniPos.y+m*permeterheight); DrawShape(pointbegin,pointend,0,RGB(0,0,0),1 ,0); if(fmod(m,100)!=0) Image1->Canvas->TextOut(ItemsIniPos.x+80,ItemsIniPos.y+m*permeterheight,AnsiString(m+depthfrom)); } else if(fmod(m,5)==0) { pointbegin=Point(WordX,ItemsIniPos.y+m*permeterheight); pointend=Point(WordX-per5meterwidth,ItemsIniPos.y+m*permeterheight); DrawShape(pointbegin,pointend,0,RGB(0,0,0),1 ,0); Image1->Canvas->TextOut(ItemsIniPos.x+80,ItemsIniPos.y+m*permeterheight,AnsiString(m+depthfrom)); } else { pointbegin=Point(WordX,ItemsIniPos.y+m*permeterheight); pointend=Point(WordX-permeterwidth,ItemsIniPos.y+m*permeterheight); DrawShape(pointbegin,pointend,0,RGB(0,0,0),1 ,0); } } } void _fastcall TMainDraw::DrawItem(int dsindex,float depthbegin,float depthend) { TPoint pointbegin,pointend,mediapoint; int depthinterval; float itemvalue; float itemvalue1,itemvalue2; float leftbegin; int lncolor; int lnwidth; int lnstyle; int lanwidth; int beginindex; int selectstyle[4]; int rtstyle; float maxvalue; float minvalue; float perunitwidth; depthinterval=depthend-depthbegin; leftbegin=FIXWIDTH+LEFTPART; for(int i=1;i<=dsindex;i++) leftbegin=leftbegin+m_savepara[dsindex-1].trackwidth; leftbegin=PixelToCM(leftbegin); // GRADUATION GM maxvalue=m_savepara[dsindex].maxvalue; minvalue=m_savepara[dsindex].minvalue; lncolor=m_savepara[dsindex].linecolor; lnstyle=m_savepara[dsindex].linestyle; lnwidth=m_savepara[dsindex].linewidth; lanwidth=m_savepara[dsindex].trackwidth; lanwidth=PixelToCM(lanwidth); perunitwidth=(float)lanwidth/(maxvalue-minvalue); Graduation(minvalue,maxvalue,leftbegin,lanwidth,lncolor,selectstyle); DataModule3->selectreal->Open(); DataModule3->selectreal->First(); itemvalue1=DataModule3->selectreal->Fields[dsindex]->AsFloat; // pointbegin=Point(leftbegin+perunitwidth*itemvalue,ItemsIniPos.y); for(int m=0;m selectreal->Fields[dsindex]->AsFloat; DataModule3->selectreal->Next(); itemvalue2=DataModule3->selectreal->Fields[dsindex]->AsFloat; CurveDraw(leftbegin,ypos1,ypos2,lanwidth,itemvalue1,itemvalue2,minvalue,maxvalue,selectstyle,lnstyle,lnwidth); } DataModule3->selectreal->Close(); } void _fastcall TMainDraw::Graduation(float minvalue,float maxvalue,float leftbegin,int lanwidth,int selectstyle,int *graduationstyle) { int perunitwidth=lanwidth/10; int perunitheight=PixelToCM(RULEHEIGHT)/4; int percurveheight=PixelToCM(0.1); TPoint graduationbegin,graduationend,bottomend; graduationstyle[0]=selectstyle; if(selectstyle==GradColor[0]||selectstyle==GradColor[1]||selectstyle==GradColor[2]||selectstyle==GradColor[3]) { for (int k=1;k<4;k++) graduationstyle[k]=GradColor[k+3]; } else { graduationstyle[1]=GradColor[1]; graduationstyle[2]=GradColor[2]; graduationstyle[3]=GradColor[3]; } int begin=leftbegin;//+ItemsPosition.x; for (int curve=0;curve<10;curve++) { graduationbegin=Point(begin+curve*perunitwidth,ItemsIniPos.y); graduationend=Point(begin+curve*perunitwidth,ItemsIniPos.y-percurveheight); bottomend=Point(begin+curve*perunitwidth,/*Bitmap->Height*/RuleIniPos.y+ ItemsHeight+PixelToCM(RULEHEIGHT)+20 ); DrawShape(graduationbegin,graduationend,0,RGB(0,0,0),1 ,0); DrawShape(graduationbegin,bottomend,0,RGB(0,0,0),1 ,0); } float maxtemp=maxvalue; for(int i=0;i<4;i++) { graduationbegin=Point(begin,ItemsIniPos.y-perunitheight*i); graduationend=Point(begin+lanwidth,ItemsIniPos.y-perunitheight*i); DrawShape(graduationbegin,graduationend,/*graduationstyle[i]*/0,graduationstyle[i],1 ,0); AnsiString strtemp; Image1->Canvas->TextOut(leftbegin+5,ItemsIniPos.y-14-perunitheight*i,strtemp.FormatFloat("#.##",minvalue)); switch(i) { case 1: maxvalue=maxtemp*2; break; case 2: maxvalue=maxtemp*5; break; case 3: maxvalue=maxtemp*10; break; } // Image1->Canvas->Font->Size=14; Image1->Canvas->TextOut(leftbegin+lanwidth-30,ItemsIniPos.y-14-perunitheight*i,strtemp.FormatFloat("#.##", maxvalue)); // DrawTextInRightRect(Image1->Canvas->Handle, } } void _fastcall TMainDraw::DealTextData(float &qcdata,int &rtstyle,float max,float min) { if(qcdata>=min&&qcdata<=max) rtstyle=0; if(qcdata<=2*max&&qcdata>max) { rtstyle=1; qcdata=qcdata/2; } if(qcdata<=5*max&&qcdata>2*max) { rtstyle=2; qcdata=qcdata/5; } if(qcdata>5*max) { rtstyle=3; qcdata=qcdata/10; } } void TMainDraw::DealSQLString() { int tracknum=m_savepara.size(); SQLString="select "; switch(depthselect) { case 0: for (int i=1;i StringGrid1->Cells[0][i]+" , "; SQLString=SQLString+SetupForm->StringGrid1->Cells[0][tracknum]; SQLString=SQLString+" from "+SetupForm->MainDBFName+ " where "; SQLString=SQLString+"depth>="+AnsiString(depthfrom); SQLString=SQLString+" and depth<="+AnsiString(depthto); SQLString=SQLString+" and mark='D' "; break; case 1: for (int i=1;i StringGrid1->Cells[0][i]+" , "; SQLString=SQLString+SetupForm->StringGrid1->Cells[0][tracknum]; SQLString=SQLString+" from "+SetupForm->MainDBFName+ " where "; SQLString=SQLString+"lagdepth>="+AnsiString(depthfrom); SQLString=SQLString+" and lagdepth<="+AnsiString(depthto); SQLString=SQLString+" and mark='L' "; break; case 2: for (int i=1;i StringGrid1->Cells[0][i]+" , "; SQLString=SQLString+SetupForm->StringGrid1->Cells[0][tracknum]; SQLString=SQLString+" from "+SetupForm->MainDBFName+ " where "; SQLString=SQLString+"lagdepth>="+AnsiString(depthfrom); SQLString=SQLString+" and lagdepth<="+AnsiString(depthto); break; } DataModule3->selectreal->Active=false; DataModule3->selectreal->SQL->Clear(); DataModule3->selectreal->SQL->Add(SQLString); DataModule3->selectreal->ExecSQL(); DataModule3->selectreal->Active=true; } void _fastcall TMainDraw::DrawTextInCenterRect( HDC hDC, RECT rt,AnsiString text) { DrawText(hDC ,text.c_str(),text.Length(),&rt, DT_CENTER|DT_VCENTER|DT_SINGLELINE); } void _fastcall TMainDraw::DrawTextInRightRect( HDC hDC, RECT rt,AnsiString text) { DrawText(hDC ,text.c_str(),text.Length(),&rt, DT_RIGHT|DT_VCENTER|DT_SINGLELINE); } void _fastcall TMainDraw::DoWithTimeGraduation() { // // int widthtemp=ItemsPosition.x; // int m_timefrom,m_timeto; AnsiString stringtime; // int depth; int permeterwidth=4;//PixelToCM(0.1); int per5meterwidth=8;//PixelToCM(0.3); int per25meterwidth=12;//PixelToCM(0.5); int WordX=ItemsIniPos.x+PixelToCM(FIXWIDTH); TPoint pointbegin,pointend; // m_timefrom=SetupForm->TimeFrom->ItemIndex; // m_timeto=SetupForm->TimeTo->ItemIndex; int depth=m_timeto-m_timefrom; AnsiString timestring; for(int m=0;m<=depth;m++) { timestring=SetupForm->TimeFrom->Items->Strings[m+m_timefrom]; if(fmod(m,100)==0) { Image1->Canvas->TextOut(ItemsIniPos.x+5,ItemsIniPos.y+m*permeterheight,timestring); } if(fmod(m,25)==0) { pointbegin=Point(WordX,ItemsIniPos.y+m*permeterheight); pointend=Point(WordX-per25meterwidth,ItemsIniPos.y+m*permeterheight); DrawShape(pointbegin,pointend,0,RGB(0,0,0),1 ,0); if(fmod(m,100)!=0) Image1->Canvas->TextOut(ItemsIniPos.x+5,ItemsIniPos.y+m*permeterheight,timestring); } else if(fmod(m,5)==0) { pointbegin=Point(WordX,ItemsIniPos.y+m*permeterheight); pointend=Point(WordX-per5meterwidth,ItemsIniPos.y+m*permeterheight); DrawShape(pointbegin,pointend,0,RGB(0,0,0),1 ,0); Image1->Canvas->TextOut(ItemsIniPos.x+5,ItemsIniPos.y+m*permeterheight,timestring); } else { pointbegin=Point(WordX,ItemsIniPos.y+m*permeterheight); pointend=Point(WordX-permeterwidth,ItemsIniPos.y+m*permeterheight); DrawShape(pointbegin,pointend,0,RGB(0,0,0),1 ,0); Image1->Canvas->TextOut(ItemsIniPos.x+5,ItemsIniPos.y+m*permeterheight,timestring); } } } void __fastcall TMainDraw::PrintButtonClick(TObject *Sender) { int BitmapInfoSize, BitmapImageSize; long DIBWidth, DIBHeight; PChar BitmapImage; Windows::PBitmapInfo BitmapInfo; Graphics::TBitmap *Bitmap; RECT rect; int pagenum; int xBand,yBand; int position=0; float pheight; float prnparax ; float prnparay ; float ScaleX,ScaleY; int height1,height2; // PrintDialog1->Options->Clear(); // PrintDialog1->Options << poPageNums << poSelection; PrintDialog1->FromPage =1; PrintDialog1->MinPage = 1; // PrintDialog1->ToPage = PrintDialog1->MaxPage = PageControl1->PageCount; if (PrintDialog1->Execute()) { Printer()->BeginDoc(); prnparax=GetDeviceCaps(Printer()->Handle,LOGPIXELSX); prnparay=GetDeviceCaps(Printer()->Handle,LOGPIXELSY); ScaleX=prnparax/(float)PixelsPerInch; ScaleY=prnparay/(float)PixelsPerInch; Bitmap = new Graphics::TBitmap(); Bitmap->Assign(Image1->Picture); GetDIBSizes(Bitmap->Handle, BitmapInfoSize, BitmapImageSize); BitmapInfo = (PBitmapInfo) new char[BitmapInfoSize]; // BitmapImage = (PChar) new char [BitmapImageSize]; BitmapImage=(PChar)GlobalAlloc(GPTR,BitmapImageSize); GetDIB(Bitmap->Handle, 0, BitmapInfo, BitmapImage); DIBWidth = BitmapInfo->bmiHeader.biWidth; DIBHeight = BitmapInfo->bmiHeader.biHeight; position=Printer()->PageHeight/ScaleY; pheight=Printer()->PageHeight/ScaleY; pagenum=DIBHeight/position; height1=DIBHeight-position*pagenum; height2=pheight-height1; int tmp=0; for(int i=0;i<=pagenum;i++) { StartPage(Printer()->Canvas->Handle); StretchDIBits(Printer()->Canvas->Handle, 0,0, DIBWidth*ScaleX, height1*ScaleY, 0, (pagenum-i)*pheight, DIBWidth,height1, BitmapImage, BitmapInfo, DIB_RGB_COLORS, SRCCOPY); if(i!=pagenum) { StretchDIBits(Printer()->Canvas->Handle, 0,height1*ScaleY, DIBWidth*ScaleX, height2*ScaleY, 0,DIBHeight-pheight*(i+1) , DIBWidth,height2, BitmapImage, BitmapInfo, DIB_RGB_COLORS, SRCCOPY); } EndPage(Printer()->Canvas->Handle); } Printer()->EndDoc(); GlobalFree(BitmapImage); delete [] BitmapInfo; delete Bitmap; } } //--------------------------------------------------------------------------- void TMainDraw:: CurveDraw(float leftbegin,float ypos1,float ypos2,int lanwidth,float value1,float value2,float minvalue,float maxvalue,int *linecolor,int linestyle,int linewidth) { int scaleindex1,scaleindex2; float dealvalue1,dealvalue2; TPoint pointbegin,pointend,pointmid; float perunitwidth; int ratio1,ratio2; DealData(value1,ratio1,dealvalue1,scaleindex1,maxvalue,minvalue); DealData(value2,ratio2,dealvalue2,scaleindex2,maxvalue,minvalue); perunitwidth=(float)lanwidth/(maxvalue-minvalue); if(scaleindex1==scaleindex2) { pointbegin.x=leftbegin+dealvalue1*perunitwidth; pointbegin.y=ypos1; pointend.x=leftbegin+dealvalue2*perunitwidth; pointend.y=ypos2; DrawShape(pointbegin,pointend,linestyle,linecolor[scaleindex1],linewidth,0); // DrawShape(pointbegin,pointend,lnstyle,selectstyle[rtstyle],lnwidth ,0); } if(scaleindex1>scaleindex2) { pointbegin.x=leftbegin+dealvalue1*perunitwidth; pointbegin.y=ypos1; pointend.x=leftbegin+value2*perunitwidth/ratio1; pointend.y=ypos2; DrawShape(pointbegin,pointend,linestyle,linecolor[scaleindex1],linewidth,0); } if(scaleindex1 =min&&value<=max) { rtstyle=0; qcdata=value; ratio=1; } if(value<=2*max&&value>max) { rtstyle=1; qcdata=value/2; ratio=2; } if(value<=5*max&&value>2*max) { rtstyle=2; qcdata=value/5; ratio=5; } if(value>5*max) { rtstyle=3; qcdata=value/10; ratio=10; } }