How do I repaint my ellipse...

I have created a form, on which I have 2 scrollbars, and
a speedbutton which opens a colorDialog!...

The 2 scrollbars is used to make an ellipse on a paintbox,
and the colorDialog is used to change the color of the
ellipse..

I have used the OnClose Event on the colorDialog, but
the color of the ellipse doesn't change immidiately..
I want it to do just that... But how???

I have tried paint, repaint, refresh, update.. Whatever..

Please help me, as I need to get this working....
I am in dire straits....!!
LVL 1
lar_jensAsked:
Who is Participating?
 
philipleighsConnect With a Mentor Commented:
Hi,

Thanks for the points.

Cheers,
Phil.

0
 
rwilson032697Commented:
Can you post some code that shows what is happening?
0
 
philipleighsCommented:
Hi,

You should have a private variable declared in your form of type TColor which gets set in the OnClose event of the color dialog. You should do the Invalidate, Update, Refresh or whatever here too. This will cause the OnPaint event to be called.

Then in the OnPaint event of the form you should set Canvas.Pen.Color to the TColor private variable, then draw the ellipse.

Cheers,
Phil.

0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
lar_jensAuthor Commented:
Sure I can.. :)

When I push the Speedbutton, this happens :
procedure TfrmSek3.SpeedButton1Click(Sender: TObject);
begin
     if clChoice.Execute then begin                           { Opens the colorDialog }
        pntDisp.Canvas.Pen.Color := clChoice.Color;   { sets the pen color       }
     end;
end;

And when the openDialog closes:
procedure TfrmSek3.clChoiceClose(Sender: TObject);
begin
     frmSek3.Repaint;
end;

Then when I change the scrollbars!!! :
procedure TfrmSek3.ScrollBarChange(Sender: TObject);
var
   c1, c2, c3, c4 :Integer;
begin
     pntDisp.Canvas.Brush.Color := clBtnFace;
     pntDisp.Canvas.FillRect(Rect(0,0,pntDisp.Width,pntDisp.Height));
     pntDisp.Canvas.Brush.Color := pntDisp.Canvas.Pen.Color;
     c1 := Round(pntDisp.Width/2)  - Round((scrHoriz.Max-scrHoriz.Position)/2);
     c2 := Round(pntDisp.Height/2) - Round(scrVert.Position/2);
     c3 := Round(pntDisp.Width/2)  + Round((scrHoriz.Max-scrHoriz.Position)/2);
     c4 := Round(pntDisp.Height/2) + Round(scrVert.Position/2);
     pntDisp.Canvas.Ellipse(c2, c1, c4, c3);
end;




0
 
lar_jensAuthor Commented:
Sure I can.. :)

When I push the Speedbutton, this happens :
procedure TfrmSek3.SpeedButton1Click(Sender: TObject);
begin
     if clChoice.Execute then begin                           { Opens the colorDialog }
        pntDisp.Canvas.Pen.Color := clChoice.Color;   { sets the pen color       }
     end;
end;

And when the openDialog closes:
procedure TfrmSek3.clChoiceClose(Sender: TObject);
begin
     frmSek3.Repaint;
end;

Then when I change the scrollbars!!! :
procedure TfrmSek3.ScrollBarChange(Sender: TObject);
var
   c1, c2, c3, c4 :Integer;
begin
     pntDisp.Canvas.Brush.Color := clBtnFace;
     pntDisp.Canvas.FillRect(Rect(0,0,pntDisp.Width,pntDisp.Height));
     pntDisp.Canvas.Brush.Color := pntDisp.Canvas.Pen.Color;
     c1 := Round(pntDisp.Width/2)  - Round((scrHoriz.Max-scrHoriz.Position)/2);
     c2 := Round(pntDisp.Height/2) - Round(scrVert.Position/2);
     c3 := Round(pntDisp.Width/2)  + Round((scrHoriz.Max-scrHoriz.Position)/2);
     c4 := Round(pntDisp.Height/2) + Round(scrVert.Position/2);
     pntDisp.Canvas.Ellipse(c2, c1, c4, c3);
end;




0
 
rwilson032697Commented:
Move the code from the ScrollBarChange method to a new method that ScrollBarChange calls.

eg:

procedure TfrmSek.DrawTheEllipse;

var
          c1, c2, c3, c4 :Integer;
       begin
            pntDisp.Canvas.Brush.Color := clBtnFace;
            pntDisp.Canvas.FillRect(Rect(0,0,pntDisp.Width,pntDisp.Height));
            pntDisp.Canvas.Brush.Color := pntDisp.Canvas.Pen.Color;
            c1 := Round(pntDisp.Width/2)  - Round((scrHoriz.Max-scrHoriz.Position)/2);
            c2 := Round(pntDisp.Height/2) - Round(scrVert.Position/2);
            c3 := Round(pntDisp.Width/2)  + Round((scrHoriz.Max-scrHoriz.Position)/2);
            c4 := Round(pntDisp.Height/2) + Round(scrVert.Position/2);
            pntDisp.Canvas.Ellipse(c2, c1, c4, c3);
end;

procedure TfrmSek3.ScrollBarChange(Sender: TObject);

       begin
           DrawTheEllipse;
       end;

Add a call to this method from the SpeedButton1Click method (see below)

procedure TfrmSek3.SpeedButton1Click(Sender: TObject);
       begin
            if clChoice.Execute then begin                           { Opens the colorDialog }
               pntDisp.Canvas.Pen.Color := clChoice.Color;   { sets the pen color       }
               DrawTheEllipse; // The method that draws the ellipse;
            end;
       end;

All you need to do extra is declare the DrawMyEllipse procedure in the form class and you're away!

Cheers,

Raymond.
0
 
lar_jensAuthor Commented:
Sure I can.. :)

When I push the Speedbutton, this happens :
procedure TfrmSek3.SpeedButton1Click(Sender: TObject);
begin
     if clChoice.Execute then begin                           { Opens the colorDialog }
        pntDisp.Canvas.Pen.Color := clChoice.Color;   { sets the pen color       }
     end;
end;

And when the openDialog closes:
procedure TfrmSek3.clChoiceClose(Sender: TObject);
begin
     frmSek3.Repaint;
end;

Then when I change the scrollbars!!! :
procedure TfrmSek3.ScrollBarChange(Sender: TObject);
var
   c1, c2, c3, c4 :Integer;
begin
     pntDisp.Canvas.Brush.Color := clBtnFace;
     pntDisp.Canvas.FillRect(Rect(0,0,pntDisp.Width,pntDisp.Height));
     pntDisp.Canvas.Brush.Color := pntDisp.Canvas.Pen.Color;
     c1 := Round(pntDisp.Width/2)  - Round((scrHoriz.Max-scrHoriz.Position)/2);
     c2 := Round(pntDisp.Height/2) - Round(scrVert.Position/2);
     c3 := Round(pntDisp.Width/2)  + Round((scrHoriz.Max-scrHoriz.Position)/2);
     c4 := Round(pntDisp.Height/2) + Round(scrVert.Position/2);
     pntDisp.Canvas.Ellipse(c2, c1, c4, c3);
end;




0
 
scrapdogCommented:
It appears as if the only time your ellipse gets redrawn is when you adjust the scrollbars.  You had no Paint method for the paint box, so it calling Repaint would do nothing.  You will want to repaint it when you change the scrollbars, OR when the color gets changed.  Therefore you will have to write on method for painting, and have the event handlers (for scroll bar change and color dialog close) call this method.  Change your code to look like this:

procedure TfrmSek3.SpeedButton1Click(Sender: TObject);
begin
     if clChoice.Execute then begin                           { Opens the colorDialog }
        pntDisp.Canvas.Pen.Color := clChoice.Color;   { sets the pen color       }
     end;
end;

procedure TfrmSek3.clChoiceClose(Sender: TObject);
begin
     pntDisp.Repaint;
end;

procedure TfrmSek3.ScrollBarChange(Sender: TObject);
begin
  pntDispPaint;
end;

procedure TfrmSek3.pntDispPaint(Sender: TObject);
var
   c1, c2, c3, c4 :Integer;
begin
     pntDisp.Canvas.Brush.Color := clBtnFace;
     pntDisp.Canvas.FillRect(Rect(0,0,pntDisp.Width,pntDisp.Height));
     pntDisp.Canvas.Brush.Color := pntDisp.Canvas.Pen.Color;
     c1 := Round(Width/2)  - Round((scrHoriz.Max-scrHoriz.Position)/2);
     c2 := Round(sp.Height/2) - Round(scrVert.Position/2);
     c3 := Round(pntDisp.Width/2)  + Round((scrHoriz.Max-scrHoriz.Position)/2);
     c4 := Round(pntDisp.Height/2) + Round(scrVert.Position/2);
     pntDisp.Canvas.Ellipse(c2, c1, c4, c3);
end;


pntDispPaint is the OnPaint event handler for the paintbox.  Since ScrollbarChange just calls this OnPaint event handler, you could also just set the OnChange event for the scroll bar directly to pntDispPaint if you wanted.
0
 
lar_jensAuthor Commented:
The comment from philipleighs, works better, since I can discard the code that clears
the canvas... So if you could please post your comment as an answer, I would be able
to give you the points...

0
 
lar_jensAuthor Commented:
The comment from philipleighs, works better, since I can discard the code that clears
the canvas... So if you could please post your comment as an answer, I would be able
to give you the points...

0
 
philipleighsCommented:
Hi again,

Glad I could help.

You will have to reject rwilsons answer before I can post my response as an answer.

Regards,
Phil.

0
 
rwilson032697Commented:
Um... Not wanting to be picky or anything, but drawing the ellipse on the form repaint will just duplicate what you are doing in the scroll bar change - it really is much nicer to wrap it up in a separet function.

But then its your question. Go ahead, make Phils day!
0
 
lar_jensAuthor Commented:
I'm sorry - but we all think different.. But after thinking a while (cause I don't want to be
unfair to anyone) I found that for me it is most logical to put everything that needs to be
painted inside the paint procdure.. I'm sorry..

0
All Courses

From novice to tech pro — start learning today.