BitBlt(
DestDC: HDC; {目标 DC}
X, Y, Width, Height: Integer; {目标位置与大小}
SrcDC: HDC; {源 DC}
XSrc, YSrc: Integer; {源起始位置}
Rop: DWORD {光栅操作码, 也就是源像素与目标像素的混合方式}
): BOOL;
//可以使用光栅操作码的函数还有: PatBlt、StretchBlt、StretchDIBits
本例效果图:
代码文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
bit1,bit2: TBitmap;
cvs1,cvs2,cvs3: TCanvas;
procedure TForm1.FormCreate(Sender: TObject);
begin
with ListBox1.Items do
begin
Add('SRCCOPY ');
Add('SRCPAINT ');
Add('SRCAND ');
Add('SRCINVERT ');
Add('SRCERASE ');
Add('NOTSRCCOPY ');
Add('NOTSRCERASE');
Add('MERGECOPY ');
Add('MERGEPAINT ');
Add('PATCOPY ');
Add('PATPAINT ');
Add('PATINVERT ');
Add('DSTINVERT ');
Add('BLACKNESS ');
Add('WHITENESS ');
end;
Panel1.Caption := '';
Panel2.Caption := '';
Panel3.Caption := '';
bit1 := TBitmap.Create;
bit1.Width := Panel1.ClientWidth;
bit1.Height := Panel1.ClientHeight;
bit2 := TBitmap.Create;
bit2.Width := Panel2.ClientWidth;
bit2.Height := Panel2.ClientHeight;
cvs1 := TCanvas.Create;
cvs2 := TCanvas.Create;
cvs3 := TCanvas.Create;
cvs1.Handle := GetDC(Panel1.Handle);
cvs2.Handle := GetDC(Panel2.Handle);
cvs3.Handle := GetDC(Panel3.Handle);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
bit1.Free;
bit2.Free;
cvs1.Free;
cvs2.Free;
cvs3.Free;
end;
procedure TForm1.FormPaint(Sender: TObject);
const
ColorArr: array[0..5] of TColor = (clRed, clGreen, clBlue, clYellow, clFuchsia, clPurple);
var
wi,hi: Integer;
I: Integer;
begin
wi := bit1.Width div 4;
hi := bit1.Height div 4;
bit1.Canvas.Pen.Width := 10;
for I := 1 to 3 do
begin
bit1.Canvas.Pen.Color := ColorArr[i-1];
bit1.Canvas.MoveTo(wi*i, 0);
bit1.Canvas.LineTo(wi*i, bit1.Height);
bit1.Canvas.Pen.Color := ColorArr[i+2];
bit1.Canvas.MoveTo(0, hi*i);
bit1.Canvas.LineTo(bit1.Width, hi*i);
end;
bit2.Canvas.Pen.Width := 15;
bit2.Canvas.Ellipse(15, 15, bit2.Width-15, bit2.Height-15);
cvs1.Draw(0, 0, bit1);
cvs2.Draw(0, 0, bit2);
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
Rop: Cardinal;
begin
Repaint;
Rop := SRCCOPY;
case ListBox1.ItemIndex of
0 : Rop := SRCCOPY ;
1 : Rop := SRCPAINT ;
2 : Rop := SRCAND ;
3 : Rop := SRCINVERT ;
4 : Rop := SRCERASE ;
5 : Rop := NOTSRCCOPY ;
6 : Rop := NOTSRCERASE;
7 : Rop := MERGECOPY ;
8 : Rop := MERGEPAINT ;
9 : Rop := PATCOPY ;
10: Rop := PATPAINT ;
11: Rop := PATINVERT ;
12: Rop := DSTINVERT ;
13: Rop := BLACKNESS ;
14: Rop := WHITENESS ;
end;
cvs3.Draw(0, 0, bit1);
BitBlt(cvs3.Handle, (bit1.Width - bit2.Width) div 2, (bit1.Height - bit2.Height) div 2,
bit2.Width, bit2.Height, bit2.Canvas.Handle, 0, 0, Rop);
end;
end.
窗体文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 300
ClientWidth = 319
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnDestroy = FormDestroy
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 8
Top = 8
Width = 180
Height = 140
Caption = 'Panel1'
TabOrder = 0
end
object Panel2: TPanel
Left = 194
Top = 8
Width = 119
Height = 105
Caption = 'Panel2'
TabOrder = 1
end
object Panel3: TPanel
Left = 8
Top = 154
Width = 180
Height = 140
Caption = 'Panel3'
TabOrder = 2
end
object ListBox1: TListBox
Left = 194
Top = 119
Width = 119
Height = 176
ItemHeight = 13
TabOrder = 3
OnClick = ListBox1Click
end
end