delphi 取控件下的图象

本文介绍如何在Delphi中使用Bitmap技术直接从控件中获取背景图像,并将其显示到指定的面板上,实现高效且灵活的背景定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, jpeg;

type
  TForm1 = class(TForm)
    pnl1: TPanel;
    pnl2: TPanel;
    img1: TImage;
    img2: TImage;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
   var
    DC : HDC;
    SaveIndex : HDC;
    Position: TPoint;
    Control : TWinControl ;
    Buf : TBitmap;
    w:integer;
begin
    Buf := TBitmap.Create();
    Buf.PixelFormat := pfDevice;
    Buf.Width := btn1.Width;
    Buf.Height := btn1.Height;

     Control:= btn1;
   if Control.Parent <> nil then
    begin
{$R-}
        DC := Buf.Canvas.Handle;
        SaveIndex := SaveDC(DC);
        GetViewportOrgEx(DC, Position);
        SetViewportOrgEx(DC, Position.X - Control.Left, Position.Y - Control.Top, nil);
        IntersectClipRect(DC, 0, 0, Control.Parent.ClientWidth, Control.Parent.ClientHeight);
        Control.Parent.Perform(WM_ERASEBKGND, DC, 0);
        Control.Parent.Perform(WM_PAINT, DC, 0);
        RestoreDC(DC, SaveIndex);
         w:=Buf.Width;
        self.img1.Picture.Graphic:=Buf;
{$R+}
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    self.btn1.Parent:=self.pnl1;
end;

end.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值