如何彻底解决在MainForm中加入图片的问题

wuxiao 2000-03-18 09:58:00
建立MDI项目时,在其MainForm加入Image并Load一个BMP文件,想作为应用程序的背景,但程序运行时图片并不能显示出来.现在,网上,参考书上有介绍在MainForm中加入图片的办法,但不能实现STRETCH为'TRUE的'变比填充',如何在MainForm中加入图片,并让图片随窗体的大小变化而变化?
...全文
182 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yue 2000-03-20
  • 打赏
  • 举报
回复
在mdiform中加一个panel,align属性为clClient,在这个panel上加image,align为clClient,stretch为true,加入你的图片,一切搞定!
Jean 2000-03-19
  • 打赏
  • 举报
回复
{***********************************************************}
{ }
{ Chou's Delphi VCL Extensions (CX Lib) }
{ }
{ Copyright (c) 1999 Jean-Christopher Chou }
{ }
{ ------------------------------------------------------- }
{ }
{ Class Name: TCxWallpaper }
{ Package: CwCtrls.dpk }
{ Writor: Jean-Christopher Chou }
{ Create Date: 1999-08-27 }
{ Description: Put it on your form and give it a bitmap }
{ then on runtime the form's background }
{ will be filled by a tiled bitmap. }
{ }
{***********************************************************}

unit CxWallpaper;

interface

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

type
TCxWallpaper = class(TComponent)
private
FForm: TForm;
FHandle: HWND;
FMargins: array [0..9] of Integer;
FClientInstance: TFarProc;
FPrevClientProc: TFarProc;
FBitmap: TBitmap;
procedure ClientWndProc(var Message: TMessage);
procedure SetBitmap(const Value: TBitmap);
protected
procedure Loaded; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Bitmap: TBitmap read FBitmap write SetBitmap;
end;

implementation

constructor TCxWallpaper.Create(AOwner: TComponent);
var
i: Integer;
begin
inherited Create(AOwner);
FBitmap := TBitmap.Create;
Randomize;
for i := 0 to 9 do
FMargins[i] := Random(100);
end;

destructor TCxWallpaper.Destroy;
begin
FBitmap.Free;
inherited Destroy;
end;

procedure TCxWallpaper.Loaded;
begin
inherited Loaded;
if not (csDesigning in ComponentState) then
begin
if not (Owner is TForm) then
raise Exception.Create('我只能在Form上用!')
else
begin
FForm := Owner as TForm;
if FForm.FormStyle = fsMDIForm then
FHandle := FForm.ClientHandle
else
FHandle := FForm.Handle;
FClientInstance := MakeObjectInstance(ClientWndProc);
FPrevClientProc := Pointer(GetWindowLong(FHandle, GWL_WNDPROC));
SetWindowLong(FHandle, GWL_WNDPROC, LongInt(FClientInstance));
end;
end;
end;

procedure TCxWallpaper.ClientWndProc(var Message: TMessage);
var
DC: HDC;
W, H, Mrg, Row, Col: Integer;
begin
with Message do
case Msg of
WM_ERASEBKGND:
if not FBitmap.Empty then
begin
DC := TWMEraseBkGnd(Message).DC;
W := FBitmap.Width;
H := FBitmap.Height;
for Row := 0 to FForm.ClientHeight div H do
begin
Mrg := FMargins[Row mod 10];
for Col := 0 to (FForm.ClientWidth + Mrg) div W + 3 do
BitBlt(DC, Col * W - Mrg, Row * H, W, H,
FBitmap.Canvas.Handle,
0, 0, SRCCOPY);
end;
Result := 1;
end;
else
Result := CallWindowProc(FPrevClientProc,
FHandle, Msg, wParam, lParam);
end;
end;

procedure TCxWallpaper.SetBitmap(const Value: TBitmap);
var
i: Integer;
begin
FBitmap.Assign(Value);
if not FBitmap.Empty then
begin
Randomize;
for i := 0 to 9 do
FMargins[i] := Random(FBitmap.Width);
end;
end;

end.
Lin 2000-03-19
  • 打赏
  • 举报
回复
建议简单重载MainForm的Paint过程.
jll 2000-03-19
  • 打赏
  • 举报
回复
我想有一个简单的方法,在Form中放一个Image,设置Image的Align为Client,Strenth为true。

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧