Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

redrawing form background

Posted on 2004-08-04
9
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

618 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