Solved

redrawing form background

Posted on 2004-08-04
9
306 Views
Last Modified: 2010-04-05
hi experts, I want to have the background of my prog update without using onpaint to draw the whole thing again because it's causing significant slowdown: I'm drawing a complex background directly to canvas, then have the user draw on the form. I think Onpaint is called to redraw background and as more shapes are drawn, things really slowdown. Also the form resizes and more of the background has to show. thanks
0
Comment
Question by:IOOI
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 4

Expert Comment

by:alikoank
ID: 11723469
You can do your drawing on a buffer, then copy buffer to actual canvas.
0
 
LVL 7

Assisted Solution

by:LRHGuy
LRHGuy earned 100 total points
ID: 11724942
The best bet is to keep your drawing on a canvas/bitmap, then you can copy it to the screen whenever needed.

This sample has the drawing in fBackgroupBitMap, it centers it in the window's client area.

procedure TfrmMainEstimator.FormPaint(Sender: TObject);
var
  w,h,bh,bw:integer;
begin
  { Center bitmap! }
  w:=ClientWidth;
  h:=ClientHeight;
  bw:=fBackgroundBitMap.Width;
  bh:=fBackgroundBitMap.Height;
  Canvas.CopyMode:=cmSrcCopy;
  Canvas.Draw((W-bw) div 2,(H-bh) div 2,fBackgroundBitMap);
end;

You could instead put the routine into the wm_EraseBackground event with the same results.

Hope that helps!
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11726210
If you want your form repainted, you can just post WM_PAINT message like this:

SendMessage(Form1.Handle, WM_PAINT, 0, 0);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:IOOI
ID: 11732401
alikoank or others: can you provide code for drawing on a buffer, then copying buffer to actual canvas? I'm hesitant to use bitmap as it can complicate other stuff I'm doing later in the prog. I've increased the points.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11732854
hello  IOOI,  I am not sure that just doing your "Drawing" on a bitmap and then painting that bitmap on your display (Form I guess) will speed things up at all, if you have to do the drawing each time, if you could draw the bitmap ONE Time, then you can just draw the bitmap on the Form canvas. . . since it seems that it's your "Drawing" that is what is slow for you. . . I have drawn all sorts of things and for the most part these draw operations are really fast, even if I do very many of them, seems like you need a way to get your drawing to be faster. What sort of drawing is taking so long?
0
 
LVL 4

Expert Comment

by:alikoank
ID: 11733127
create a TBitmap variable and draw your image onto it.

and in your Control's PaintWindow event copy this TBitmap to canvas.

like this:

xxx.PainWindow(DC: HDC);
var
  cnv:TCanvas;
begin
 ...
  cnv.Handle := DC;
  try
    ...
    cnv.CopyRect(Rect(...), MyBitmap.Canvas, Rect(...));
  finally
    cnv.Handle=0;
  end;
 ...
0
 

Author Comment

by:IOOI
ID: 11745022
hi Slick812,
Yes you're right in saying draw operations are pretty fast. Perhaps I had worded it wrong. It seems the way to go as you suggested is to draw the bitmap once and use it to repaint background.
thanks
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11746253
That is what I suggested...keeping the drawing in a bitmap and painting it when needed...
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 100 total points
ID: 11748159
OK, here is some code that might help, Iam not sure about what "Drawing" operations you do, this just draws lines, I use a TPaiintbox, but you can use the same events on your form


private
    { Private declarations }
    DisplayBmp: TBitmap; // this is bitmap to save all drawing operations
    MouseDraw: Boolean;
    mosePos: TPoint; // records last mouse Position




procedure TForm1.FormCreate(Sender: TObject);

begin
MouseDraw := False;
DisplayBmp := TBitmap.Create;
DisplayBmp.Canvas.Brush.Color := $6699DD;
DisplayBmp.Height := PaintBox2.Height;
DisplayBmp.Width := PaintBox2.Width;
DisplayBmp.Canvas.TextOut(20,20, 'PaintBox2 Bitmap');
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeAndNil(DisplayBmp);
end;



procedure TForm1.PaintBox2Paint(Sender: TObject);
begin
// only draw the bitmap, no Drawing operations
PaintBox2.Canvas.Draw(0,0,DisplayBmp);
end;

procedure TForm1.PaintBox2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
MouseDraw := True;
mosePos.x := X;
mosePos.y := Y;
PaintBox2.Canvas.Pen.Color := $FFCC33;
DisplayBmp.Canvas.Pen.Color := $FFCC33;
end;

procedure TForm1.PaintBox2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
MouseDraw := False;
end;

procedure TForm1.PaintBox2MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
if MouseDraw then
  begin
// Draw on the PaintBox and Bitmap at the same time;
  PaintBox2.Canvas.MoveTo(mosePos.x, mosePos.y);
  DisplayBmp.Canvas.MoveTo(mosePos.x, mosePos.y);
  PaintBox2.Canvas.LineTo(X, Y);
  DisplayBmp.Canvas.LineTo(X, Y);
  mosePos.x := X;
  mosePos.y := Y;
  end;
end;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question