Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 6664|回复: 3

[界面] Lazarus中的透明Panel组件

[复制链接]

该用户从未签到

发表于 2010-12-22 11:15:43 | 显示全部楼层 |阅读模式
Lazarus中没有透明Panel组件,就自己写了一个,经过一番折腾,总算是设计期和运行期都可以正确的透明了。

注意,透明Panel是通过抓取Parent图片的方式实现的,因此Panel下不能放置别的组件,否则会被背景图挡住。

我写了一个具有背景图片功能的Panel组件,可以结合透明Panel一起使用,效果非常不错,详情参考《Lazarus 的具有背景图片的Panel组件》

下面是组件源码
  1. unit uTransparentPanel;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, LMessages, Forms, Controls, Graphics, LCLType, types, Windows;
  6. type
  7.   { TTransparentPanel }
  8.   TTransparentPanel = class(TCustomControl)
  9.   private
  10.     fBuffer: Graphics.TBitmap;
  11.     fBufferChanged : boolean;
  12.     procedure SetColor(Value: TColor); override;
  13.   protected
  14.     function getBuffer : Graphics.TBitmap; virtual;
  15.     procedure WMWindowPosChanged(var Message: TLMWindowPosChanged); message LM_WINDOWPOSCHANGED;
  16.     procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
  17.     procedure CreateParams(var Params: TCreateParams);
  18.     procedure Paint; override;
  19.     procedure Resize; override;
  20.     procedure redrawBackgroundBuffer(var buffer : Graphics.TBitmap); virtual;
  21.     function getBufferChanged : boolean; virtual;
  22.     procedure setBufferChanged(val : boolean); virtual;
  23.     procedure Invalidate; override;
  24.   public
  25.     constructor Create(AOwner : TComponent); override;
  26.     destructor Destroy; override;
  27.   published
  28.     property OnPaint;
  29.     property Color;
  30.     property Align;
  31.     property Height;
  32.     property Cursor;
  33.     property HelpContext;
  34.     property HelpType;
  35.     property Hint;
  36.     property Left;
  37.     property Name;
  38.     property Tag;
  39.     property Top;
  40.     property Width;
  41.     property Anchors;
  42.     property Constraints;
  43.   end;
  44. procedure Register;
  45. implementation
  46. procedure Register;
  47. begin
  48.   RegisterComponents('aess',[TTransparentPanel]);
  49. end;
  50. { TTransparentPanel }
  51. procedure TTransparentPanel.SetColor(Value: TColor);
  52. begin
  53.   inherited SetColor(Value);
  54.   RecreateWnd(Self);
  55. end;
  56. function TTransparentPanel.getBuffer: Graphics.TBitmap;
  57. begin
  58.   Result := fBuffer;
  59. end;
  60. procedure TTransparentPanel.WMWindowPosChanged(var Message: TLMWindowPosChanged);
  61. begin
  62.   setBufferChanged(true);
  63.   Invalidate;
  64.   inherited;
  65. end;
  66. procedure TTransparentPanel.WMEraseBkgnd(var Message: TLMEraseBkgnd);
  67. begin
  68.   Message.Result := 1;
  69. end;
  70. procedure TTransparentPanel.CreateParams(var Params: TCreateParams);
  71. begin
  72.   inherited CreateParams(Params);
  73.   params.exstyle := params.exstyle or WS_EX_TRANSPARENT;
  74. end;
  75. procedure TTransparentPanel.Paint;
  76. begin
  77.   if getBufferChanged then
  78.   begin
  79.     redrawBackgroundBuffer(fBuffer);
  80.     setBufferChanged(false);
  81.   end;
  82.   Canvas.Draw(0, 0, fBuffer);
  83.   if assigned(OnPaint) then
  84.     OnPaint(Self);
  85. end;
  86. procedure TTransparentPanel.Resize;
  87. begin
  88.   setBufferChanged(true);
  89.   Invalidate;
  90.   inherited Resize;
  91. end;
  92. procedure TTransparentPanel.redrawBackgroundBuffer(var buffer : Graphics.TBitmap);
  93. var
  94.   rDest : TRect;
  95.   bmp : Graphics.TBitmap;
  96. begin
  97.   bmp := Graphics.TBitmap.Create;
  98.   try
  99.     bmp.PixelFormat := pf24bit;
  100.     bmp.Width := Parent.Width;
  101.     bmp.Height := Parent.Height;
  102.     bmp.TransparentColor:= Self.Color;
  103.     bmp.Canvas.brush.Color:=TCustomForm(parent).Color;
  104.     bmp.Canvas.FillRect(types.rect(0,0,bmp.width,bmp.height));
  105.     SendMessage(parent.Handle, WM_PAINT, bmp.Canvas.handle, 0);
  106.     Application.ProcessMessages;
  107.     buffer.Width:= Self.Width;
  108.     buffer.Height := Self.Height;
  109.     rDest := types.Rect(0,0,Width, Height);
  110.     buffer.Canvas.CopyRect(rDest, bmp.Canvas, BoundsRect);
  111.   finally
  112.     freeandnil(bmp);
  113.   end;//fianlly
  114. end;
  115. function TTransparentPanel.getBufferChanged: boolean;
  116. begin
  117.   Result := fBufferChanged;
  118. end;
  119. procedure TTransparentPanel.setBufferChanged(val: boolean);
  120. begin
  121.   fBufferChanged := val;
  122. end;
  123. procedure TTransparentPanel.Invalidate;
  124. begin
  125.   if assigned(parent) and parent.HandleAllocated then
  126.   begin
  127.     InvalidateRect(parent.Handle, BoundsRect, true);
  128.     inherited Invalidate;
  129.   end
  130.   else
  131.     inherited Invalidate;
  132. end;
  133. constructor TTransparentPanel.Create(AOwner: TComponent);
  134. begin
  135.   inherited Create(AOwner);
  136.   fBuffer := Graphics.TBitmap.Create;
  137.   ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
  138.   csDoubleClicks, csReplicatable];
  139.   Width := 200;
  140.   Height := 150;
  141.   ParentCtl3d := False;
  142.   Ctl3D := False;
  143.   ParentColor := False;
  144.   fBufferChanged:= false;
  145.   inherited Color := clWindow;
  146. end;
  147. destructor TTransparentPanel.Destroy;
  148. begin
  149.   fBuffer.Free;
  150.   inherited Destroy;
  151. end;
  152. end.
复制代码


转自:http://lc51746.blog.163.com/blog ... romSearchEngine=yes
回复

使用道具 举报

该用户从未签到

发表于 2010-12-22 11:46:28 | 显示全部楼层
貓工哥若是能貼一張圖來展示透明效果, 豈不更好 ?
常常看到大陸技術文章都不喜歡貼圖
殊不知千行萬碼都抵不過一張圖的效果
去 google 搜尋, 相同或類似標題的技術文件就有幾十篇
不可能每一篇都下載下來研究測試
甚至很多文章內容跟標題講的不大一樣
(例如標題叫 "黃河之水天上來" ,
內容可能是一篇探討黃河水質的科學研究,
而不是詩詞文學欣賞, 作者跟讀者解讀就不同)
若讀者能看到圖, 就直接知道是不是自己需要的東西

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册(注册审核可向QQ群索取)

x
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-12-22 14:37:15 | 显示全部楼层
好东西啊  !!
辛苦楼主了,收藏。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-12-23 10:39:56 | 显示全部楼层
Linux下是不透明的
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2026-1-7 16:11 , Processed in 0.073519 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表