Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi bmp in Panel zeichnen (https://www.delphipraxis.net/133514-bmp-panel-zeichnen.html)

youuu 3. Mai 2009 17:56


bmp in Panel zeichnen
 
Hallo,

kann man ein bmp in einen Panel zeichnen ohne Paintbox oder TImage?

DeddyH 3. Mai 2009 18:02

Re: bmp in Panel zeichnen
 
Leite Dir eine Klasse von TPanel ab, mache dort die Canvas-Eigenschaft öffentlich und zeichne dann darauf.

Fussball-Robby 3. Mai 2009 18:05

Re: bmp in Panel zeichnen
 
Oder ohne Ableiten mittels GetWindowDC, ein kleines Beispiel:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  X: TBitmap;
  H: HDC;
begin
  X := TBitmap.Create;
  try
    X.Width := 20;
    X.Height := 20;
    X.Canvas.Ellipse(0, 0, 20, 20);
    H := GetWindowDC(Panel1.Handle);
    BitBlt(H, 0, 0, 20, 20, X.Canvas.Handle, 0, 0, SRCCOPY);
  finally
    X.Free;
  end;
end;
Gruß

youuu 3. Mai 2009 18:06

Re: bmp in Panel zeichnen
 
Hast du darüber zufällig ein Tutorial?
Habe soetwas noch nie gemacht.

DeddyH 3. Mai 2009 18:08

Re: bmp in Panel zeichnen
 
Da vermisse ich aber ein ReleaseDC ;)

[edit]
Zitat:

Zitat von youuu
Hast du darüber zufällig ein Tutorial?
Habe soetwas noch nie gemacht.

Wen meinst Du jetzt? [/edit]

Fussball-Robby 3. Mai 2009 18:11

Re: bmp in Panel zeichnen
 
Zitat:

Zitat von DeddyH
Da vermisse ich aber ein ReleaseDC ;)

Huch, mir war nicht bewusst, dass man das machen muss :| Habe selber nicht oft damit gearbeitet :wink:
Also, dahinter noch ein
Delphi-Quellcode:
ReleaseDC(Panel1.Handle, H);
Ich hoffe das stimmt jetzt so..

turboPASCAL 3. Mai 2009 18:24

Re: bmp in Panel zeichnen
 
@Fussball-Robby ...und dach dem verschieben oder wenn ein fenster drüber war ist alles pfutsch. ;)

stoxx 3. Mai 2009 18:52

Re: bmp in Panel zeichnen
 
Du musst natürlich im Ereignis Onpaint des Formulars oder Panels immer wieder alles neu reinmalen.
Das übernimmt ansonsten Delphi für Dich, da aber Delphi nix davon weiß, weil Du ja keine Paintbox genommen hast, dann musst Du Dich selbst um das neuzeichnen kümmern ..

DeddyH 3. Mai 2009 18:59

Re: bmp in Panel zeichnen
 
Also in etwa so:
Delphi-Quellcode:
...
interface

uses ..., ExtCtrls;

type
  TPanel = class(ExtCtrls.TPanel)
  protected
    procedure Paint;override; //erspart dann das Öffentlichmachen von Canvas
  end;

  TForm1 = class(TForm)
    Panel1: TPanel;
    ...
  end;

...

implementation

procedure TPanel.Paint;
var bmp: TBitmap;
begin
  inherited;
  bmp := TBitmap.Create;
  try
    bmp.LoadFromFile('C:\Program Files\Common Files\CodeGear Shared\Images\BackGrnd\Calendar.bmp');
    Canvas.Draw(0,0,bmp);
  finally
    bmp.Free;
  end;
end;

...
Das ist natürlich nur ein Beispiel, man kann die Bitmap auch zum Programmstart einmalig laden und im Speicher halten etc.

turboPASCAL 3. Mai 2009 19:03

Re: bmp in Panel zeichnen
 
Oder So:

Delphi-Quellcode:
unit Unit1;

interface

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

type
  TPanel = class(ExtCtrls.TPanel)  // Wichtig, als erstes for TForm1
  private
    { Private-Deklarationen }
    FOnPaint: TNotifyEvent;
  protected
    { Protected-Deklarationen }
    procedure Paint; override;
  public
    { Public-Deklarationen }
    property Canvas;
  published
    { Published-Deklarationen }
    property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
  end;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure OnPanel1Paint(Sender: TOBject);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TPanel.Paint;
begin
  inherited;
  if assigned(fOnPaint) then fOnPaint(Self);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel1.OnPaint := OnPanel1Paint;
end;

procedure TForm1.OnPanel1Paint(Sender: TOBject);
var r: Trect;
begin
  r := Panel1.ClientRect;
  InflateRect(r, -2, -2);

  With Panel1 do
  begin
    Canvas.Brush.Color := clWindow;
    Canvas.RoundRect(r.Left, r.Top, r.Right, r.Bottom, 20, 20);
    Canvas.Brush.Color := Panel1.Color;
    Canvas.Draw(5, 5, Application.Icon);
  end;
end;

end.
- eine neues Project erstellen
- Ein Panel auf eine Form klatschen
- zwei mal auf die Form klicken und sich im FormCreate wiederfinden
- Copy & Paste mit diesem Quelltext den alten Quelltext ersetzen
- fertisch.



//Edit:

@Detlef, das gibt ne Beule ! ;)

Code:
procedure [color=#ff001f]TPanel.Paint;[/color] [color=#ff001f] <--<<<[/color]
var bmp: TBitmap;
begin
  ...
    bmp.[color=#ff001f]LoadFromFile[/color]('C:\Program Files\Common Files\CodeGear Shared\Images\BackGrnd\Calendar.bmp'); [color=#ff001f]<--<<<[/color]
  ...
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:19 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz