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

📄 unit1.cpp

📁 关于书籍《Borland c++Builder工程实践》的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void __fastcall TMainForm::SpeedButton5Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtBrush;
        Image->Cursor=TCursor(crErase);

}
//---------------------------------------------------------------------------
//填充选取
void __fastcall TMainForm::SpeedButton6Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtFill;
        Image->Cursor=TCursor(crFill);

}
//---------------------------------------------------------------------------
//喷笔选取
void __fastcall TMainForm::SpeedButton7Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtFog;
        Image->Cursor=TCursor(crFill);

}
//---------------------------------------------------------------------------
//橡皮擦选取
void __fastcall TMainForm::SpeedButton8Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtErase;
        Image->Cursor=TCursor(crErase);

}
//---------------------------------------------------------------------------
//画直线
void __fastcall TMainForm::SpeedButton10Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtLine;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------
//文本输出
void __fastcall TMainForm::SpeedButton11Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtText;
        Image->Canvas->Brush->Style=bsClear;
        Image->Cursor=TCursor(crText);
}
//---------------------------------------------------------------------------
//画矩形
void __fastcall TMainForm::SpeedButton9Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtRectangle;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------
//画实心矩形
void __fastcall TMainForm::SpeedButton13Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtSolidRect;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------
//画圆矩形
void __fastcall TMainForm::SpeedButton16Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtRoundRect;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------
//画实心圆矩形
void __fastcall TMainForm::SpeedButton12Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtSolidRndRect;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------
//画椭圆
void __fastcall TMainForm::SpeedButton15Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtEllipse;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------
//画实心椭圆
void __fastcall TMainForm::SpeedButton14Click(TObject *Sender)
{
        EraseSelect();
        ToolState=dtSolidEllipse;
        Image->Cursor=TCursor(crDraw);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::N1Click(TObject *Sender)
{
     NewBMPForm->WidthEdit->Text = IntToStr(Image->Picture->Width);
     NewBMPForm->HeightEdit->Text = IntToStr(Image->Picture->Height);
     if (NewBMPForm->ShowModal() != IDCANCEL)
     {
          Graphics::TBitmap *Bitmap =
               new Graphics::TBitmap();
        Bitmap->Width = StrToInt(NewBMPForm->WidthEdit->Text);
        Bitmap->Height = StrToInt(NewBMPForm->HeightEdit->Text);
        Image->Picture->Graphic = Bitmap;
        CurrentFile = EmptyStr;
     }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::S1Click(TObject *Sender)
{
     if (CurrentFile != EmptyStr&&
          ExtractFileExt(CurrentFile)!=".jpg"){
          Image->Picture->SaveToFile(CurrentFile);
     }
     else{
          A1Click(Sender);
     }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::A1Click(TObject *Sender)
{
     if (SaveDialog1->Execute()){
          CurrentFile = SaveDialog1->FileName;
          S1Click(Sender);
     }
}
//---------------------------------------------------------------------------
//画笔笔宽选取
void __fastcall TMainForm::PenWidthChange(TObject *Sender)
{
        if (PenWidth->Value>50)
                PenWidth->Value=50;
        Image->Canvas->Pen->Width=PenWidth->Value;
}
//---------------------------------------------------------------------------

//实际绘图函数
void __fastcall TMainForm::DrawShape(TPoint TopLeft, TPoint BottomRight, TPenMode AMode)
{
     Image->Canvas->Pen->Mode = AMode;
     switch (ToolState){
          case dtLine:
          Image->Canvas->MoveTo(TopLeft.x, TopLeft.y);
          Image->Canvas->LineTo(BottomRight.x, BottomRight.y);
          break;
          case dtRectangle:
          Image->Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
          break;
          case dtEllipse:
          Image->Canvas->Ellipse(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
          break;
          case dtRoundRect:
          Image->Canvas->RoundRect(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y, (TopLeft.x - BottomRight.x)/2, (TopLeft.y - BottomRight.y)/2);
          break;
          case dtSolidRect:
          Image->Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
          break;
          case dtSolidEllipse:
          Image->Canvas->Ellipse(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
          break;
          case dtSolidRndRect:
          Image->Canvas->RoundRect(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y, (TopLeft.x - BottomRight.x)/2, (TopLeft.y - BottomRight.y)/2);
          break;
          case dtSelect:
          Image->Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
          break;
     }
     Image->Canvas->Pen->Mode = pmCopy;
}

//图形重画和擦除函数
void TMainForm::EraseSelect()
{

     if (isSelect){
     Image->Canvas->Brush->Style=bsClear;
     Image->Canvas->Pen->Style = psDot;
     Image->Canvas->Pen->Width = 1;
     isSelect=false;
     DrawShape(Origin, MovePt,pmNotXor);
     T1->Enabled=false;
     C1->Enabled=false;
     Image->Canvas->Brush->Style=bsSolid;
     Image->Canvas->Pen->Style = psSolid;
     Image->Canvas->Pen->Width=PenWidth->Value;
     }
}


void __fastcall TMainForm::N6Click(TObject *Sender)
{
     DWORD Color;
     for (int i=0;i<Image->Height;i++)
          for (int j=0;j<Image->Width;j++){
               Color=DWORD(Image->Canvas->Pixels[j][i]);
               Image->Canvas->Pixels[j][i]=RGB(
               0xFF-BYTE(Color),0xFF-BYTE(Color>>8),0xFF-BYTE(Color>>16));
          }
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::S2Click(TObject *Sender)
{
        ResizeBMPForm->WidthEdit->Text = IntToStr(Image->Width);
        ResizeBMPForm->HeightEdit->Text = IntToStr(Image->Height);
        if(ResizeBMPForm->ShowModal()==IDOK)
        {
            Graphics::TBitmap *tmpBitmap =
                new Graphics::TBitmap();
            tmpBitmap->Width = StrToInt(ResizeBMPForm->WidthEdit->Text);
            tmpBitmap->Height = StrToInt(ResizeBMPForm->HeightEdit->Text);
            tmpBitmap->Canvas->StretchDraw
                (TRect(0,0,tmpBitmap->Width,tmpBitmap->Height),
            Image->Picture->Graphic);
            Image->Picture->Graphic=tmpBitmap;
            delete tmpBitmap;
        }
}
//---------------------------------------------------------------------------




void __fastcall TMainForm::N7Click(TObject *Sender)
{
     long Gray;
     BYTE *ptr;
     Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
     tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
     tmpBitmap->PixelFormat=pf24bit;
     for (int y = 0; y < tmpBitmap->Height; y++)
     {
          ptr =(BYTE *) tmpBitmap->ScanLine[y];
          for (int x = 0; x < tmpBitmap->Width*3; x+=3){
               Gray=299*ptr[x+2]+587*ptr[x+1]
                    +114*ptr[x];
               ptr[x]=Gray/1000;
               ptr[x+1]=Gray/1000;
               ptr[x+2]=Gray/1000;
          }
     }
     Image->Picture->Graphic=tmpBitmap;
     delete tmpBitmap;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::N8Click(TObject *Sender)
{
     DoContrast(110);
}
//---------------------------------------------------------------------------


void TMainForm::DoContrast(int s)
{
     int XX;
     BYTE *ptr;
     Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
     tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
     tmpBitmap->PixelFormat=pf24bit;
     for (int y = 0; y < tmpBitmap->Height; y++)
     {
          ptr =(BYTE *) tmpBitmap->ScanLine[y];
          for (int x = 0; x < tmpBitmap->Width*3; x+=3){
               XX=((ptr[x+2]-128)*s+12800)/100;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+2]=XX;
               XX=((ptr[x+1]-128)*s+12800)/100;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+1]=XX;
               XX=((ptr[x]-128)*s+12800)/100;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+0]=XX;
          }
     }
     Image->Picture->Graphic=tmpBitmap;
     delete tmpBitmap;
}

void __fastcall TMainForm::N9Click(TObject *Sender)
{
     DoContrast(91);
}
//---------------------------------------------------------------------------

void TMainForm::DoBright(int b)
{
     int XX;
     BYTE *ptr;
     Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
     tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
     tmpBitmap->PixelFormat=pf24bit;
     for (int y = 0; y < tmpBitmap->Height; y++)
     {
          ptr =(BYTE *) tmpBitmap->ScanLine[y];
          for (int x = 0; x < tmpBitmap->Width*3; x+=3){
               XX=ptr[x+2]+b;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+2]=XX;
               XX=ptr[x+1]+b;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+1]=XX;
               XX=ptr[x]+b;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+0]=XX;
          }
     }
     Image->Picture->Graphic=tmpBitmap;
     delete tmpBitmap;
}

void __fastcall TMainForm::N10Click(TObject *Sender)
{
     DoBright(12);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::N11Click(TObject *Sender)
{
     DoBright(-12);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::N12Click(TObject *Sender)
{
     int XX;
     BYTE *ptr;
     Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
     tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
     tmpBitmap->PixelFormat=pf24bit;
     CForm->TrackBar1->Position=0;
     CForm->TrackBar2->Position=0;
     CForm->TrackBar3->Position=0;

     if (CForm->ShowModal()==IDOK){
     for (int y = 0; y < tmpBitmap->Height; y++)
     {
          ptr =(BYTE *) tmpBitmap->ScanLine[y];
          for (int x = 0; x < tmpBitmap->Width*3; x+=3){
               XX=ptr[x+2]+CForm->TrackBar1->Position;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+2]=XX;
               XX=ptr[x+1]+CForm->TrackBar2->Position;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+1]=XX;
               XX=ptr[x]+CForm->TrackBar3->Position;
               XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+0]=XX;
          }
     }
     Image->Picture->Graphic=tmpBitmap;
     }
     delete tmpBitmap;
}
//---------------------------------------------------------------------------

void TMainForm::DoFilter(int * flt, int Div)
{
     int XX[3];
     BYTE *ptr,*ptru,*ptrd,*ptr1;
     Graphics::TBitmap *Bitmap=new Graphics::TBitmap();
     Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
     Bitmap->Assign((TPersistent*)Image->Picture->Graphic);
     Bitmap->PixelFormat=pf24bit;
     tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
     tmpBitmap->PixelFormat=pf24bit;
     for (int y = 1; y < tmpBitmap->Height-1; y++)
     {
          ptr=(BYTE *)Bitmap->ScanLine[y];
          ptr1=(BYTE *)tmpBitmap->ScanLine[y];
          ptru=(BYTE *)tmpBitmap->ScanLine[y-1];
          ptrd=(BYTE *)tmpBitmap->ScanLine[y+1];
          for (int x=3;x<(tmpBitmap->Width-1)*3;x+=3){
               XX[0]=0;XX[1]=0;XX[2]=0;
               for (int i=-1;i<=1;i++)
               for (int j=0;j<3;j++)
                    XX[j]+=ptr1[x+3*i+j]*flt[4+i];
               for (int i=-1;i<=1;i++)
               for (int j=0;j<3;j++)
                    XX[j]+=ptru[x+3*i+j]*flt[1+i];
               for (int i=-1;i<=1;i++)
               for (int j=0;j<3;j++)
                    XX[j]+=ptrd[x+3*i+j]*flt[7+i];
               for (int i=0;i<3;i++){
                    XX[i]=XX[i]/Div;
                    XX[i]=MAX(XX[i],0);
                    XX[i]=MIN(XX[i],255);
                    ptr[x+i]=XX[i];
               }
          }
     }
     Image->Picture->Graphic=Bitmap;
     delete tmpBitmap;
     delete Bitmap;
}

//---------------------------------------------------------------------------

⌨️ 快捷键说明

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