Hi! I'm a beginer in Delphi.
Can somebody send me an exemple of how I can use the function BitBln to move a simple TBitmap
(without flickers)? Can I do that in any other way?
Thanx !!!
Who is Participating?
mheacockConnect With a Mentor Commented:
This code is from a component I am currently writing.

// here's the declarations
  bmpBG:         TBitmap;
  bmpBuff:       TBitmap;

// here's one of the drawing functions.
procedure CSimGaugeDevice.DrawGauge(const i_ggGauge: CGauge; const i_fData, i_fMin, i_fMax: Single);
  rectGauge: TRect;
  fPercent:  Single;
  if ((i_ggGauge.Enabled <> True) or (Width = 0) or (Height = 0)) then
  bmpBuff.Canvas.Brush.Color := i_ggGauge.Color;
  rectGauge.TopLeft.X := i_ggGauge.Left;
  rectGauge.TopLeft.Y := i_ggGauge.Top;
  rectGauge.BottomRight.X := (rectGauge.TopLeft.X + i_ggGauge.Width) - 1;
  rectGauge.BottomRight.Y := (rectGauge.TopLeft.Y + i_ggGauge.Height) - 1;
    if (i_fData <= i_fMin) then
      fPercent := 0
      fPercent := (i_fData - i_fMin) / (i_fMax - i_fMin);
  except on EZeroDivide do
    fPercent := 0;
  if (i_ggGauge.GaugeType = gt_Vertical) then
    rectGauge.TopLeft.Y := rectGauge.TopLeft.Y +
                          (Round((1 - fPercent) * (rectGauge.BottomRight.Y - rectGauge.TopLeft.Y  + 1)))
    rectGauge.BottomRight.X := rectGauge.BottomRight.X -
                              (Round((1 - fPercent) * (rectGauge.BottomRight.X - rectGauge.TopLeft.X + 1)));
end; {procedure CSimGaugeDevice.DrawGauge}

//here are the calls.
  bmpBG.LoadFromFile('background.bmp'); //load background bitmap, you only need to do this once
  bmpBuff.Width := bmpBG.Width; //set width and height, you only need to do this once per loadfromfile
  bmpBuff.Height := bmpBG.Height;
  bmpBuff.Canvas.Draw(0, 0, bmpBG); //copy background to drawing buffer
  DrawGauge(ggsGauges.UserGauge1, KWDData.UserData1, KWDData.EngineerMin, KWDData.EngineerMax); //here we will draw a gauge on the buffer
// the following statements are identical.
  Image1.Canvas.Draw(0, 0, bmpBuff);  //use only one of these statements, both are identical.  Draw is siply a wrapper for BitBlt
         0, 0,
         bmpBuff.Width - 1, bmpBuff.Height - 1,
         0, 0,
In Delphi you don't need to use the explicit API call, you can use the Copy method of TBitmaps.

You know about double buffering?  This is what will eliminate your flickers.

You need a TBitmap buffer where all your drawing will happen.  After all drawing is done on this buffer, you will copy it to  your screen buffer (a Canvas, either of the form or a TImage).

If this isn't enough, please comment.  But it should get you started.
If you bump the points up to 100, I will send you some sample code.  Not much I can do beyond what I've already told you for only 25 pts.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

cobianAuthor Commented:
OK I bump the points up to 100.Send me the code,but I can only evaluate your answer as Poor.If I like your code and it is good explicative I´ll evaluate it as Good (And maybe excellent if you tell me about Btnbln too)
BtnBln??  BitBln??  You are talking about BitBlt, right?

You can grade my answer as poor, as you did, but you aren't going to get much more for 25 points.  From anyone.  It isn't worth our time.

Your points are still at 25.  No code until it is increased to 100.  I've done what you are asking many many times. I can give you code for the Copy method and sample code for BitBlt.
cobianAuthor Commented:
Here are my points (and a little more...)
If you have comments, please ask.
You also stated you want to move the bitmap?  Do you mean around the form??  Or within a TImage (like a sprite)??
cobianAuthor Commented:
I want to move the TBitmap with the mouse,using the events OnMouseDown,OnMouseMove and OnMouseUp around the form.I guess all this stuff must be placed into the OnMouseMove event.Is there something special I must do to make it works?
+10 points + excellent for a good answer.

Unfortunately, TBitmap is not a component, so you cannot capture MouseEvents for it.

But...if you draw the bitmap on the form's canvas, you can capture mouse events for the form.  Therefore for MouseDown events, you would check to see if the mouse coords are within the coords of the bitmap on the form (top, left, width, height).  When the user releases the mouse button, you simply redraw (bitblt) the bitmap on the form's canvas at the apprpriate point.  To make your moves more accurate, you'll even have to calculate the offset of the mouse click from the bitmaps (0, 0) coordinates and then redraw the bitmap based on this offset where the mouse button is released.

Whew!  Happy programming.
If you have questions when you begin implementing all this, feel free to ask them.
cobianAuthor Commented:
OK TBitmap is not a component,but TImage is it,right?...
Then I can use this stuff in my OnMouseMove event...
If you move (drag) a TImage, there's no guarantee that it won't flicker.

Actually it will flicker...but this is kind of out of our control, since this is Borland code.

I will experiment and get back to you.

cobianAuthor Commented:
Still no may have to resort to drawing the bitmap on the form's canvas and calculating mouse coordinates to for the dragging/dropping like I detailed above.  The code is not overly complex and the results would exaclty what you are after.

Not that you want to spend more points...but if you have fifty to spare, you can ask a specific question on how to move a TImage around a form (perhaps in a for...loop) without it flickering.
I'd ask it for you (because now I'm curious of the answer), but I'm flat out of points.

I suspect it is either impossible without rewriting the TImage source or very simple...perhaps setting a single proprty.  Either way, it should be an easy question.
Wait a day or two on the new question thing as I will try and get an answer for you myself.  I feel a little guilty that my answer did not solve your exact problem...of course you didn't state it as a TImage problem...but I feel a little guilty nonetheless.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.