瓷砖/中心图像在窗体背景 [英] Tile/Center image in the forms background

查看:225
本文介绍了瓷砖/中心图像在窗体背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



还有,我需要将其他组件置于图像。



我尝试过rmControls,但我无法在图像之上放置任何东西。

解决方案

您可以在窗体的 OnPaint 处理程序中绘制图像。以下是一个简单的平铺示例:

  procedure TMyForm.FormPaint(Sender:TObject); 
var
位图:TBitmap;
左,顶:整数;
begin
Bitmap:= TBitmap.Create;
尝试
Bitmap.LoadFromFile('C:\desktop\bitmap.bmp');
左:= 0;
,而Left< Width do begin
顶部:= 0;
,而顶部< Height do begin
Canvas.Draw(Left,Top,Bitmap);
inc(Top,Bitmap.Height);
结束
inc(Left,Bitmap.Width);
结束
最后
Bitmap.Free;
结束;
结束

在实际的代码中,您希望缓存位图,而不是每次加载它。我相信你可以制定出如何适应这一点,使其位于中心位置。



输出如下所示:





然而,因为这是表格的背景,所以在处理程序中执行 WM_ERASEBACKGROUND 的绘画要好得多。这也将确保您调整大小时不会有任何闪烁。这是一个更高级的程序版本,演示了这个,以及一个拉伸选项。

 程序TMyForm.FormCreate(发件人: TObject); 
begin
FBitmap:= TBitmap.Create;
FBitmap.LoadFromFile('C:\desktop\bitmap.bmp');
结束

procedure TMyForm.RadioGroup1Click(Sender:TObject);
begin
无效;
结束

procedure TMyForm.FormResize(Sender:TObject);
begin
//拉伸绘图需要
无效;
结束

程序TMyForm.PaintTile(Canvas:TCanvas);
var
Left,Top:Integer;
begin
左:= 0;
,而Left< Width do begin
顶部:= 0;
,而顶部< Height do begin
Canvas.Draw(Left,Top,FBitmap);
inc(Top,FBitmap.Height);
结束
inc(Left,FBitmap.Width);
结束
结束

程序TMyForm.PaintStretch(Canvas:TCanvas);
begin
Canvas.StretchDraw(ClientRect,FBitmap);
结束

procedure TMyForm.WMEraseBkgnd(var Message:TWmEraseBkgnd);
var
画布:TCanvas;
begin
Canvas:= TCanvas.Create;
尝试
Canvas.Handle:= Message.DC;
case RadioGroup1.ItemIndex
0:
PaintTile(Canvas);
1:
PaintStretch(Canvas);
结束
最后
Canvas.Free;
结束;
Message.Result:= 1;
结束


Is there a way to place an image in the form background and be able to tile it or center it ?

Also I need to place other components on top of the image.

I tried rmControls but I cannot place anything on top of the image.

解决方案

You can paint your image in an OnPaint handler for the form. Here's a simple example of tiling:

procedure TMyForm.FormPaint(Sender: TObject);
var
  Bitmap: TBitmap;
  Left, Top: Integer;
begin
  Bitmap := TBitmap.Create;
  Try
    Bitmap.LoadFromFile('C:\desktop\bitmap.bmp');
    Left := 0;
    while Left<Width do begin
      Top := 0;
      while Top<Height do begin
        Canvas.Draw(Left, Top, Bitmap);
        inc(Top, Bitmap.Height);
      end;
      inc(Left, Bitmap.Width);
    end;
  Finally
    Bitmap.Free;
  End;
end;

In real code you would want to cache the bitmap rather than load it every time. I'm sure you can work out how to adapt this to centre a bitmap.

The output looks like this:

However, since this is the background to the form, it's much better to do the painting in a handler for WM_ERASEBACKGROUND. That will also make sure that you won't have any flickering when you resize. Here's a more advanced version of the program that demonstrates this, together with a stretch draw option.

procedure TMyForm.FormCreate(Sender: TObject);
begin
  FBitmap := TBitmap.Create;
  FBitmap.LoadFromFile('C:\desktop\bitmap.bmp');
end;

procedure TMyForm.RadioGroup1Click(Sender: TObject);
begin
  Invalidate;
end;

procedure TMyForm.FormResize(Sender: TObject);
begin
  //needed for stretch drawing
  Invalidate;
end;

procedure TMyForm.PaintTile(Canvas: TCanvas);
var
  Left, Top: Integer;
begin
  Left := 0;
  while Left<Width do begin
    Top := 0;
    while Top<Height do begin
      Canvas.Draw(Left, Top, FBitmap);
      inc(Top, FBitmap.Height);
    end;
    inc(Left, FBitmap.Width);
  end;
end;

procedure TMyForm.PaintStretch(Canvas: TCanvas);
begin
  Canvas.StretchDraw(ClientRect, FBitmap);
end;

procedure TMyForm.WMEraseBkgnd(var Message: TWmEraseBkgnd);
var
  Canvas: TCanvas;
begin
  Canvas := TCanvas.Create;
  Try
    Canvas.Handle := Message.DC;
    case RadioGroup1.ItemIndex of
    0:
      PaintTile(Canvas);
    1:
      PaintStretch(Canvas);
    end;
  Finally
    Canvas.Free;
  End;
  Message.Result := 1;
end;

这篇关于瓷砖/中心图像在窗体背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆