Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Update() method



Permlink Replies: 2 - Last Post: Feb 25, 2016 12:15 AM Last Post By: Mario Sassone Threads: [ Previous | Next ]
Mario Sassone

Posts: 12
Registered: 10/28/06
Update() method
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 24, 2016 10:10 AM
A thing that I cant understand: in visual c++ VCL components sometinìmes I need to call the Update() method, in order to make visible a change in the graphic appereance (color etc.). Buti it works if I call Update() before I made the changes, not after.

Working example (WinControl11 is a new component based on TWinControl):
void __fastcall TForm1::FormActivate(TObject *Sender) {

WinControl11->Update();

TControlCanvas *canvas = new TControlCanvas;
canvas->Control = WinControl11;
WinControl11->canvas->Brush->Color = clBlue;
WinControl11->canvas->FillRect(TRect(0,0,100,100));
}

Not working example:

void __fastcall TForm1::FormActivate(TObject *Sender) {
TControlCanvas *canvas = new TControlCanvas;
canvas->Control = WinControl11;
WinControl11->canvas->Brush->Color = clBlue;
WinControl11->canvas->FillRect(TRect(0,0,100,100));

WinControl11->Update();
}
Thank you
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Update() method
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 24, 2016 11:09 AM   in response to: Mario Sassone in response to: Mario Sassone
Mario wrote:

A thing that I cant understand: in visual c++ VCL components
sometinìmes I need to call the Update() method, in order to
make visible a change in the graphic appereance (color etc.).
Buti it works if I call Update() before I made the changes, not
after.

Update() triggers an immediate WM_PAINT message be sent to the window *only
if* it has already been invalidated previously, otherwise it does nothing.
Calling Update() before changing a window may validate the window so the
change can then invalidate it, thus triggering a new repaint when the OS
is ready for it. Without Update(), if the window was already invalidated,
changing the window won't re-invalidate it, it will still be waiting for
a repaint from the earlier invalidate.

Working example (WinControl11 is a new component based on
TWinControl):

If you want to force a repaint, you should be using Invalidate() or Refresh()
instead of Update().

That being said, you should not be drawing on a control outside of its WM_PAINT
message handler. Such drawings are not persistent and will be erased on
the next paint.

WinControl1 should:

1. derive from TCustomControl instead of TWinControl directly. TCustomControl
derives from TWinControl to add a Canvas property.

2. expose its own properties for colors, and call Invalidate() on itself
when those properties change value.

3. apply those colors in its WM_PAINT handler (or, in the case of TCustomControl,
in an overrides Paint() method).

For example:

class TMyControl : public TCustomControl
{
private:
    TColor FBoxColor;
    void __fastcall SetBoxColor(TColor value);
 
protected:
    virtual void __fastcall Paint();
 
public:
    __fastcall TMyControl(TComponent *Owner);
 
__published:
    __property TColor BoxColor = {read=FBoxColor, write=SetBoxColor, default=clNone};
};
 
__fastcall TMyControl::TMyControl(TComponent *Owner)
    : TCustomControl(Owner)
{
    FBoxColor = clNone;
}
 
void __fastcall TMyControl::SetBoxColor(TColor value)
{
    if (FBoxColor != value)
    {
        FBoxColor = value;
        Invalidate();
    }
}
 
void __fastcall TMyControl::Paint()
{
    TCustomControl::Paint();
    if (FBoxColor != clNone)
    {
        Canvas->Brush->Color = FBoxColor;
        Canvas->FillRect(TRect(0, 0, 100, 100));
    }
}


Then you can do this:

void __fastcall TForm1::FormActivate(TObject *Sender)
{
    WinControl11->BoxColor = clBlue;
}


--
Remy Lebeau (TeamB)
Mario Sassone

Posts: 12
Registered: 10/28/06
Re: Update() method
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2016 12:15 AM   in response to: Mario Sassone in response to: Mario Sassone
Thank you very much, Im going to try your suggestion on my components.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02