Change size of canvas by click

697 views Asked by At

There is a circle made with the help of canvas method MainForm -> Canvas -> Ellipse(350,100,550,300). The question is to change size of canvas by clicking "+" or "-" buttons. Are there any methods?

1

There are 1 answers

0
Spektre On

for resizing ellipse:

  1. create some global or Form member variables:

    • int x0=350,y0=100,x1=550,y1=300;
    • this holds the ellipse parameters
  2. create some draw function (member to form or not)

    • for example:

      void draw(TCanvas *scr)
       {
       scr->Pen->Color=clWhite;
       scr->Brush->Color=clBlack;
       scr->FillRect(TRect(0,0,MainForm->ClientWidth,MainForm->ClientHeight));
       scr->Brush->Color=clBlue;
       scr->Ellipse(x0,y0,x1,y1);
       }
      
    • create events on resize and on paint for the main form and add draw(Canvas); call in booth

    • that will repaint your form when needed
  3. create on (mouse) click event

    • inside you have calling variables X,Y,Shift holding mouse state
    • so simply add this to it
    • x1=X; y1=Y; draw(Canvas);

That is all ... hope I did not make some typo (wrote from memory directly here)

If you need to resize the form then:

  • instead of x1=X, y1=Y; do
  • ClientWidth=X+32; ClientHeight=Y+32;
  • or use SetBounds(Left,Top,X+32,Y+32);
  • the +32 is resize area to ensure you can also enlarging ...

[edit1] did miss the +/- buttons (I assumed mouse resize) so here are the buttons

  1. create on + button click event:

    • leave the existing code as is and add to the event code this

      int cx=(x0+x1)>>1,ax=(x1-cx); ax+=16; x0=cx-ax; x1=cx+ax;
      int cy=(y0+y1)>>1,ay=(y1+cy); ay+=16; y0=cy-ay; y1=cy+ay; draw(Canvas);
      
    • that will enlarge booth semi-axises by 16 on each click

  2. create on - button click event:

    • code is similar to +

      int cx=(x0+x1)>>1,ax=(x1-cx); ax-=16; if (ax<1) ax=1; x0=cx-ax; x1=cx+ax;
      int cy=(y0+y1)>>1,ay=(y1+cy); ay-=16; if (ay<1) ay=1; y0=cy-ay; y1=cy+ay; draw(Canvas);
      
    • that will shrink booth semi-axises by 16 on each click and stop on size 1

if you need the form to resize then adapt the above code ... like in the on mouse event example