解决方法:
设置iDrawStyle为0,1或2可以以平铺,拉伸和居中显示图片
unit MDIMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms;
type
TMDIMainForm = class(TForm)
imgMain: TImage;
private
FOldClientProc,
FNewClientProc: TFarProc;
FDrawDC: hDC;
procedure ClientWndProc(var Msg: TMessage);
protected
public
iDrawStyle:integer;
procedure DrawImage(Style:integer);
end;
var
MDIMainForm: TMDIMainForm;
implementation
{$R *.dfm}
procedure TMDIMainForm.CreateWnd;
begin
inherited CreateWnd;
FNewClientProc := MakeObjectInstance(ClientWndProc);
FOldClientProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FNewClientProc));
end;
procedure TMDIMainForm.DrawImage(Style:integer);
var
Row, Col: Integer;
CR, IR: TRect;
NumRows, NumCols: Integer;
begin
if not FDrawImage then
exit;
GetWindowRect(ClientHandle, CR);
case Style of
0:with imgMain do
BitBlt(FDrawDC, ((CR.Right - CR.Left) - Picture.Width) div 2,
((CR.Bottom - CR.Top) - Picture.Height) div 2,
Picture.Graphic.Width, Picture.Graphic.Height,
Picture.Bitmap.Canvas.Handle, 0, 0, SRCCOPY);
1:StretchBlt(FDrawDC, 0, 0, CR.Right, CR.Bottom,
imgMain.Picture.Bitmap.Canvas.Handle, 0, 0,
imgMain.Picture.Width, imgMain.Picture.Height, SRCCOPY);
2:begin
IR := imgMain.ClientRect;
NumRows := CR.Bottom div IR.Bottom;
NumCols := CR.Right div IR.Right;
with imgMain do
for Row := 0 to NumRows+1 do
for Col := 0 to NumCols+1 do
BitBlt(FDrawDC, Col * Picture.Width, Row * Picture.Height,
Picture.Width, Picture.Height, Picture.Bitmap.Canvas.Handle,
0, 0, SRCCOPY);
end;
end;
end;
procedure TMDIMainForm.ClientWndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_ERASEBKGND:
begin
CallWindowProc(FOldClientProc, ClientHandle, Msg.Msg, Msg.wParam,
Msg.lParam);
FDrawDC := TWMEraseBkGnd(Msg).DC;
DrawImage(iDrawStyle);
Msg.Result := 1;
end;
WM_VSCROLL, WM_HSCROLL:
begin
Msg.Result := CallWindowProc(FOldClientProc, ClientHandle, Msg.Msg,
Msg.wParam, Msg.lParam);
InvalidateRect(ClientHandle, nil, True);
end;
else
Msg.Result := CallWindowProc(FOldClientProc, ClientHandle, Msg.Msg,
Msg.wParam, Msg.lParam);
end;
end;
procedure TMDIMainForm.FormResize(Sender: TObject);
begin
InvalidateRect(ClientHandle, nil, True);
end;
end.