[英]How do I tile MDI children and maintain BorderStyle = bsNone?
[英]How can I do an image tile in an MDI application?
我在一年中嘗試了很多代碼,但沒有任何工作100%我只需要能夠將圖像作為我的主要表單的背景,並能夠平鋪它。
我正在使用DELPHI 2007。
我有一個我多年前寫過的組件,作為我的免費軟件組件集合的一部分,名為TrmMDIBackground。 rmControls v1.92或D2009版本 。
它可以將圖像顯示為Tiled,Stretched,Centered或顯示單一純色。 添加對漸變顏色的支持是很容易的,我還沒有這樣做。
我在這里提供了代碼的大部分重要部分,但是查看它中的組件代碼會更好,因為鈎子窗口會查找特定的消息和所有的粘合代碼以使其工作。
至於繪圖是如何進行的,我相信它的閃爍也不錯(甚至可見)。 它目前僅支持位圖圖像。
我在這里添加了整個組件單元:
{================================================================================
Copyright (C) 1997-2002 Mills Enterprise
Unit : rmMDIBackground
Purpose : To allow an image to be placed with in the workspace area of an
MDI Form. Background colors are also available.
Date : 04-24-2000
Author : Ryan J. Mills
Version : 1.93
================================================================================}
unit rmMDIBackground;
interface
{$I CompilerDefines.INC}
uses
Windows, Messages, Classes, Forms, graphics;
type
TrmBMPDisplayStyle = (dsTiled, dsStretched, dsCentered, dsNone) ;
TrmMDIBackground = class(TComponent)
private
OldWndProc: TFarProc;
NewWndProc: Pointer;
OldMDIWndProc: TFarProc;
NewMDIWndProc: Pointer;
fBitmap: TBitmap;
fstyle: TrmBMPDisplayStyle;
fColor: TColor;
fBuffer: TBitmap;
fLastRect: TRect;
procedure SetBitmap(const Value: tBitmap) ;
procedure SetDStyle(const Value: TrmBMPDisplayStyle) ;
procedure SetMDIColor(const Value: TColor) ;
{ Private declarations }
protected
{ Protected declarations }
procedure HookWndProc(var AMsg: TMessage) ;
procedure HookWnd;
procedure UnHookWnd;
procedure HookMDIWndProc(var AMsg: TMessage) ;
procedure HookMDIWin;
procedure UnhookMDIWin;
procedure PaintImage;
public
{ Public declarations }
constructor Create(AOwner: TComponent) ; override;
destructor Destroy; override;
published
{ Published declarations }
property Bitmap: tBitmap read fBitmap write SetBitmap;
property DisplayStyle: TrmBMPDisplayStyle read fstyle write SetDStyle default dsNone;
property Color: TColor read fColor write SetMDIColor default clappWorkspace;
end;
implementation
uses rmGlobalComponentHook;
{ TrmMDIBackground }
constructor TrmMDIBackground.create(AOwner: TComponent) ;
begin
inherited;
NewWndProc := nil;
OldWndProc := nil;
OldMDIWndProc := nil;
NewMDIWndProc := nil;
fBitmap := tBitmap.create;
fbuffer := tbitmap.create;
fColor := clAppWorkSpace;
fstyle := dsNone;
fLastRect := rect(0, 0, 0, 0) ;
HookWnd;
end;
destructor TrmMDIBackground.destroy;
begin
UnHookWnd;
fBitmap.free;
fbuffer.free;
inherited;
end;
procedure TrmMDIBackground.HookMDIWin;
begin
if csdesigning in componentstate then exit;
if not assigned(NewMDIWndProc) then
begin
OldMDIWndProc := TFarProc(GetWindowLong(TForm(Owner) .ClientHandle, GWL_WNDPROC) ) ;
{$ifdef D6_or_higher}
NewMDIWndProc := Classes.MakeObjectInstance(HookMDIWndProc) ;
{$else}
NewMDIWndProc := MakeObjectInstance(HookMDIWndProc) ;
{$endif}
SetWindowLong(TForm(Owner) .ClientHandle, GWL_WNDPROC, LongInt(NewMDIWndProc) ) ;
end;
end;
procedure TrmMDIBackground.HookMDIWndProc(var AMsg: TMessage) ;
begin
with AMsg do
begin
if msg <> WM_ERASEBKGND then
Result := CallWindowProc(OldMDIWndProc, TForm(Owner) .ClientHandle, Msg, wParam, lParam)
else
result := 1;
if (msg = WM_NCPaint) or (msg = wm_Paint) then
PaintImage;
end;
end;
procedure TrmMDIBackground.HookWnd;
begin
if csdesigning in componentstate then exit;
if TForm(Owner) .formstyle <> fsMDIForm then exit;
if not assigned(NewWndProc) then
begin
OldWndProc := TFarProc(GetWindowLong(TForm(Owner) .handle, GWL_WNDPROC) ) ;
{$ifdef D6_or_higher}
NewWndProc := Classes.MakeObjectInstance(HookWndProc) ;
{$else}
NewWndProc := MakeObjectInstance(HookWndProc) ;
{$endif}
SetWindowLong(TForm(Owner) .handle, GWL_WNDPROC, LongInt(NewWndProc) ) ;
PushOldProc(TForm(Owner) , OldWndProc) ;
HookMDIWin;
end;
end;
procedure TrmMDIBackground.HookWndProc(var AMsg: TMessage) ;
begin
case AMsg.msg of
WM_DESTROY:
begin
AMsg.Result := CallWindowProc(OldWndProc, Tform(Owner) .handle, AMsg.Msg, AMsg.wParam, AMsg.lParam) ;
UnHookWnd;
exit;
end;
wm_EraseBKGND:
begin
aMsg.Result := 1;
exit;
end;
end;
AMsg.Result := CallWindowProc(OldWndProc, Tform(Owner) .handle, AMsg.Msg, AMsg.wParam, AMsg.lParam) ;
case aMsg.Msg of
WM_PAINT, // WM_ERASEBKGND,
WM_NCPaint: PaintImage;
end;
end;
procedure TrmMDIBackground.PaintImage;
var
DC: HDC;
Brush: HBrush;
cx, cy: integer;
wRect: TRect;
x, y: integer;
begin
if csdesigning in componentstate then exit;
if TForm(Owner) .FormStyle <> fsMDIForm then exit;
GetWindowRect(TForm(Owner) .ClientHandle, wRect) ;
DC := GetDC(TForm(Owner) .clienthandle) ;
try
case fstyle of
dsTiled, dsStretched, dsCentered:
begin
case fStyle of
dsTiled:
begin
cx := (wRect.right - wRect.left) ;
cy := (wRect.bottom - wRect.top) ;
y := 0;
while y < cy do
begin
x := 0;
while x < cx do
begin
bitBlt(DC, x, y, fBitmap.width, fBitmap.height,
fBitmap.canvas.Handle, 0, 0, srccopy) ;
inc(x, fBitmap.width) ;
end;
inc(y, fBitmap.Height) ;
end;
end;
dsStretched:
begin
cx := (wRect.right - wRect.left) ;
cy := (wRect.bottom - wRect.top) ;
StretchBlt(DC, 0, 0, cx, cy, fBitmap.Canvas.Handle, 0, 0, fBitmap.width, fBitmap.height, srccopy) ;
end;
dsCentered:
begin
fBuffer.width := wRect.right - wRect.left;
fBuffer.height := wRect.bottom - wRect.top;
Brush := CreateSolidBrush(ColorToRGB(fcolor) ) ;
try
FillRect(fBuffer.canvas.handle, rect(0, 0, fBuffer.width, fBuffer.height) , brush) ;
finally
DeleteObject(Brush) ;
end;
cx := (fBuffer.width div 2) - (fBitmap.width div 2) ;
cy := (fBuffer.height div 2) - (fbitmap.height div 2) ;
bitBlt(fBuffer.Canvas.handle, cx, cy, fBitmap.width, fBitmap.height,
fBitmap.Canvas.Handle, 0, 0, srccopy) ;
bitBlt(DC, 0, 0, fBuffer.width, fBuffer.height,
fBuffer.Canvas.Handle, 0, 0, srccopy) ;
end;
end;
end;
dsNone:
begin
Brush := CreateSolidBrush(ColorToRGB(fcolor) ) ;
try
FillRect(DC, TForm(Owner) .ClientRect, brush) ;
finally
DeleteObject(Brush) ;
end;
end;
end;
fLastRect := wRect;
finally
ReleaseDC(TForm(Owner) .clienthandle, DC) ;
end;
end;
procedure TrmMDIBackground.SetBitmap(const Value: tBitmap) ;
begin
fBitmap.assign(Value) ;
end;
procedure TrmMDIBackground.SetDStyle(const Value: TrmBMPDisplayStyle) ;
begin
if fstyle <> Value then
begin
fstyle := Value;
PaintImage;
end;
end;
procedure TrmMDIBackground.SetMDIColor(const Value: TColor) ;
begin
if fColor <> Value then
begin
fColor := Value;
PaintImage;
end;
end;
procedure TrmMDIBackground.UnhookMDIWin;
begin
if csdesigning in componentstate then exit;
if assigned(NewMDIWndProc) then
begin
SetWindowLong(TForm(Owner) .ClientHandle, GWL_WNDPROC, LongInt(OldMDIWndProc) ) ;
if assigned(NewMDIWndProc) then
{$ifdef D6_or_higher}
Classes.FreeObjectInstance(NewMDIWndProc) ;
{$else}
FreeObjectInstance(NewMDIWndProc) ;
{$endif}
NewMDIWndProc := nil;
OldMDIWndProc := nil;
end;
end;
procedure TrmMDIBackground.UnHookWnd;
begin
if csdesigning in componentstate then exit;
if assigned(NewWndProc) then
begin
SetWindowLong(TForm(Owner) .handle, GWL_WNDPROC, LongInt(PopOldProc(TForm(Owner) ) ) ) ;
if assigned(NewWndProc) then
{$ifdef D6_or_higher}
Classes.FreeObjectInstance(NewWndProc) ;
{$else}
FreeObjectInstance(NewWndProc) ;
{$endif}
NewWndProc := nil;
OldWndProc := nil;
end;
UnHookMDIWin;
end;
end.
編輯:添加了圖像繪制代碼。
編輯:修復了第一個WindProc處理程序中閃爍的刷新問題
編輯:在此處添加了更正的單位代碼
我不確定它是否會起作用,但我發現了改變MDI的父背景 。
決定在於截取
WM_ERASEBKGND
,WM_VSCROLL
和WM_HSCROLL
消息,並使用DrawImage
過程或InvalidateRect
過程執行區域繪制。CreateWnd
過程使用SetWindowLong
過程來安裝窗口的新過程。 不要忘記從項目文件中刪除Application.CreateForm(TForm2, Form2)
行Application.CreateForm(TForm2, Form2)
並從unit2.pas文件中var Form2: TForm
行var Form2: TForm
2。
// This procedure tiles the image on the form's client area
procedure TForm1.DrawImage;
var
i, j: Integer;
WndRect, ImageRect: TRect;
Rows, Cols: Integer;
begin
GetWindowRect(ClientHandle, WndRect);
ImageRect:=Image1.ClientRect;
Rows:=WndRect.Bottom div ImageRect.Bottom;
Cols:=WndRect.Right div ImageRect.Right;
with Image1 do
for i:=0 to Rows+1 do
for j:=0 to Cols+1 do
BitBlt(MyDC, j*Picture.Width, i*Picture.Height, Picture.Width,
Picture.Height, Picture.Bitmap.Canvas.Handle, 0, 0, SRCCOPY);
end;
您可以在MDI表單中執行以下操作OnPaint過程添加以下內容
Canvas.Lock;
try
Canvas.Brush.Bitmap := MyImg.Picture.Bitmap;
Canvas.FillRect(Rect(0,0,ClientWidth,ClientHeight));
finally
Canvas.Unlock;
end;
但是,由於過度重新繪制,當您手動重新調整表單大小時,它仍然會閃爍。 有一些窗口消息說明表單已調整大小,您可以掛鈎,並且在表單完成大小調整之前不會更新。
這些窗口消息可以解決這個問題:
通過背景我假設你的意思是主MDI框架窗口的客戶區域 。
屏幕的這個區域由MDI Client窗口處理,因此一種方法是子類化MDI Client窗口,然后處理WM_PAINT消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.