Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a image of a panel with a combobox

I need to capture an image of panel.

The problem I am running into is that if the Panel contains a TCombobox the Text does not appear.

procedure AssignPanelImageToPicture(Panel : TPanel;Image : TImage);
var
 B : TBitmap;
begin
 B := TBitmap.Create;
 try
   B.Width := Panel.Width;
   B.Height := Panel.Height;
   B.Canvas.Lock;
   Panel.PaintTo(B.Canvas.Handle,0,0);
   B.Canvas.Unlock;
   Image1.Picture.Assign(B);
  finally
    B.Free;
  end;
end;

Using this code, I drop a panel with a TCombobox on it. Then Enter a value into the Text Property. I also drop a TImage Next two it. Then I add a button to call the above code.

Here is the result:

Imaging of Panel Painting Problem

Is there a better way to capture a true image of the panel.

like image 764
Robert Love Avatar asked Jan 11 '12 23:01

Robert Love


1 Answers

What about using the GetDC and BitBlt functions?

procedure AssignPanelImageToPicture(Panel : TPanel;Image : TImage);
var
 B : TBitmap;
 SrcDC: HDC;
begin
 B := TBitmap.Create;
 try
   B.Width := Panel.Width;
   B.Height := Panel.Height;
   SrcDC := GetDC(Panel.Handle);
   try
     BitBlt(B.Canvas.Handle, 0, 0, Panel.ClientWidth, Panel.ClientHeight, SrcDC, 0, 0, SRCCOPY);
   finally
      ReleaseDC(Panel.Handle, SrcDC);
   end;
   Image.Picture.Assign(B);
 finally
    B.Free;
  end;
end;
like image 68
RRUZ Avatar answered Oct 19 '22 18:10

RRUZ