比较难的问题:我想在WebBrowser上面盖一个透明的Panel,然后在这个Panel上面画出一个矩形框,也是透明的。 ( 积分: 200 )

  • 主题发起人 主题发起人 还是朋友
  • 开始时间 开始时间

还是朋友

Unregistered / Unconfirmed
GUEST, unregistred user!
比较难的问题:我想在WebBrowser上面盖一个透明的Panel,
然后在这个Panel上面画出一个矩形框,也是透明的。
此时也就是说完全可以看到WebBorwser的内容。

在这里找一个透明Panel的源码,能实现了。
但是问题来了。
如果网页里有flash,或动态图片,我画上去的矩形就没有了。
在Panel的OnPaint事件里写代码,也不行。

大家帮忙看看。下面是透明Panel的源码。
 
unit TransPanel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TTransPnlStyle = (gsBlackness, gsDstInvert, gsMergeCopy, gsMergePaint, gsNotSrcCopy,
gsNotSrcErase, gsPatCopy, gsPatInvert, gsPatPaint, gsSrcAnd,
gsSrcCopy, gsSrcErase, gsSrcInvert, gsSrcPaint, gsWhiteness);
TTransPnl = class(TCustomControl)
private
FColor: TColor;
FStyle: TTransPnlStyle;
FOnPaint: TNotifyEvent;
procedure SetColor(Value: TColor);
procedure SetStyle(Value: TTransPnlStyle);
procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
procedure WMEraseBkgnd(var Message: TMessage); message WM_ERASEBKGND;
procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
protected
Buffer: TBitmap;
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure Resize; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Canvas;
published
property Align;
property Anchors;
property AutoSize;
property BiDiMode;
property BorderWidth;
property Color: TColor read FColor write SetColor;
property Ctl3D;
property Enabled;
property Style: TTransPnlStyle read FStyle write SetStyle default gsSrcAnd;
property Visible;
property OnClick;
property OnDblClick;
property OnEnter;
property OnExit;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
end;

procedure Register;

implementation

uses GMPub;

procedure Register;
begin
RegisterComponents('Samples', [TTransPnl]);
end;

function GlassStyleToInt(gs: TTransPnlStyle): LongInt;
begin
Result := 0 ;
case gs of
gsBlackness : Result := cmBlackness;
gsDstInvert : Result := cmDstInvert;
gsMergeCopy : Result := cmMergeCopy;
gsMergePaint : Result := cmMergePaint;
gsNotSrcCopy : Result := cmNotSrcCopy;
gsNotSrcErase: Result := cmNotSrcErase;
gsPatCopy : Result := cmPatCopy;
gsPatInvert : Result := cmPatInvert;
gsPatPaint : Result := cmPatPaint;
gsSrcAnd : Result := cmSrcAnd;
gsSrcCopy : Result := cmSrcCopy;
gsSrcErase : Result := cmSrcErase;
gsSrcInvert : Result := cmSrcInvert;
gsSrcPaint : Result := cmSrcPaint;
gsWhiteness : Result := cmWhiteness;
else Assert(True, 'Error parameter in function GlassStyleToInt');
end;
end;

constructor TTransPnl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Buffer := TBitmap.Create;
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csDoubleClicks, csReplicatable];
Width := 100;
Height := 100;
FStyle := gsSrcAnd;
ParentCtl3d := False;
Ctl3D := False;
ParentColor := False;
FColor := clWhite;
end;

destructor TTransPnl.Destroy;
begin
Buffer.Free;
inherited Destroy;
end;

procedure TTransPnl.Paint;
var
R: TRect;
rop: LongInt;
begin
if DesignTime(Self) then inherited else
begin
R := Rect(0, 0, Width, Height);
Buffer.Width := Width;
Buffer.Height := Height;
Buffer.Canvas.Brush.Style := bsSolid;
Buffer.Canvas.Brush.Color := FColor;
Buffer.Canvas.FillRect(Rect(0, 0, Width, Height));
rop := GlassStyleToInt(FStyle);
StretchBlt(Buffer.Canvas.Handle, 0, 0, Width, Height,
Canvas.Handle, 0, 0, Width, Height, rop);
if Ctl3D then DrawEdge(Buffer.Canvas.Handle, R, BDR_RAISEDINNER, BF_RECT);
Buffer.Canvas.Pen.Mode := pmCopy;
Buffer.Canvas.Pen.Style := psSolid;
Canvas.Draw(0, 0, Buffer);
if Assigned(FOnPaint) then FOnPaint(Self);
end;
end;

procedure TTransPnl.SetColor(Value: TColor);
begin
if Value <> FColor then
begin
FColor := Value;
RecreateWnd;
end;
end;

procedure TTransPnl.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
end;

procedure TTransPnl.WMWindowPosChanging(var Message: TWMWindowPosChanging);
begin
Invalidate;
inherited;
end;

procedure TTransPnl.WMEraseBkgnd(var Message: TMessage);
begin
Message.Result := 0;
end;

procedure TTransPnl.Resize;
begin
Invalidate;
inherited;
end;

procedure TTransPnl.CMCtl3DChanged(var Message: TMessage);
begin
inherited;
RecreateWnd;
end;

procedure TTransPnl.SetStyle(Value: TTransPnlStyle);
begin
if Value <> FStyle then
begin
FStyle := Value;
RecreateWnd;
end;
end;
procedure TTransPnl.WMNCPaint(var Message: TMessage);
var
DC: HDC;
RC, RW: TRect;
begin
DC := GetWindowDC(Handle);
SetBkMode(DC,TRANSPARENT);
if (BorderWidth > 0) then
begin
try
Windows.GetClientRect(Handle, RC);
GetWindowRect(Handle, RW);
MapWindowPoints(0, Handle, RW, 2);
OffsetRect(RC, -RW.Left, -RW.Top);
InflateRect(RC, BorderWidth, BorderWidth);
if not DesignTime(self) then PaintBgrnd(Self, DC, False) ;
finally
ReleaseDC(Handle, DC);
end;
end;
end;

end.
 
你要的结果是什么?
 
我的要求就是在这个透明的Panel上画出一个透明的矩形框,
框线是不透明的,框里面是透明的。。

现在的问题是能画出来,但只要后面的WebBrowser一刷新,我画出来的框就没有了。
 
更新时重画框
 

Similar threads

D
回复
0
查看
867
DelphiTeacher的专栏
D
D
回复
0
查看
836
DelphiTeacher的专栏
D
D
回复
0
查看
785
DelphiTeacher的专栏
D
后退
顶部