TEdit,TMemo背景透明(SetWindowLong(WS_EX_TRANSPARENT)增加透明风格)
The component below works perfectly, except for the following problem:
1) Saves the component below in a file "trancomp.pas".
Then, install this component in Delphi;
2) Later, open Delphi and create a new project;
3) Adds a TImage and a TTransMemo to the form;
4) Opens any image in the "PICTURE" property of TIMAGE. Adjust the size of TIMAGE so that TTransMEMO stays on TIMAGE;
5) Changes the "TRANSPARENT" property of TTransMemo for "TRUE". Also change the "SCROLLBARS" property for "Vertical".
6) Now, executes the project and try to slide the scroll bar (Up/Down). See that the background image "shakes" when the text is rolled upward or down.
Please, could anybody repair this problem in the component so that it works correctly?
------------------START OF COMPONENT----------------------
- unit TranComp;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls;
- type
- TCtrl = class(TWinControl);
- TTransEdit = class(TEdit)
- private
- FAlignText: TAlignment;
- FTransparent: Boolean;
- FPainting: Boolean;
- procedure SetAlignText(Value: TAlignment);
- procedure SetTransparent(Value: Boolean);
- procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
- procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
- procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
- procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
- procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
- procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
- procedure WMSize(var Message: TWMSize); message WM_SIZE;
- procedure WMMove(var Message: TWMMove); message WM_MOVE;
- protected
- procedure RepaintWindow;
- procedure CreateParams(var Params: TCreateParams); override;
- procedure Change; override;
- procedure SetParent(AParent: TWinControl); override;
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- published
- property AlignText: TAlignment read FAlignText write SetAlignText default taLeftJustify;
- property Transparent: Boolean read FTransparent write SetTransparent default false;
- end;
- // Transparent Memo
- TTransMemo = class(TMemo)
- private
- FAlignText: TAlignment;
- FTransparent: Boolean;
- FPainting: Boolean;
- procedure SetAlignText(Value: TAlignment);
- procedure SetTransparent(Value: Boolean);
- procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
- procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
- procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
- procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
- procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
- procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
- procedure WMSize(var Message: TWMSize); message WM_SIZE;
- procedure WMMove(var Message: TWMMove); message WM_MOVE;
- protected
- procedure RepaintWindow;
- procedure CreateParams(var Params: TCreateParams); override;
- procedure Change; override;
- procedure SetParent(AParent: TWinControl); override;
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- published
- property AlignText: TAlignment read FAlignText write SetAlignText default taLeftJustify;
- property Transparent: Boolean read FTransparent write SetTransparent default false;
- end;
- procedure Register;
- implementation
- const
- BorderRec: array[TBorderStyle] of Integer = (, -);
- procedure Register;
- begin
- RegisterComponents('Transparent Components', [TTransEdit, TTransMemo]);
- end;
- function GetScreenClient(Control: TControl): TPoint;
- var
- p: TPoint;
- begin
- p := Control.ClientOrigin;
- ScreenToClient(Control.Parent.Handle, p);
- Result := p;
- end;
- constructor TTransEdit.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FAlignText := taLeftJustify;
- FTransparent := false;
- FPainting := false;
- end;
- destructor TTransEdit.Destroy;
- begin
- inherited Destroy;
- end;
- procedure TTransEdit.SetAlignText(Value: TAlignment);
- begin
- if FAlignText <> Value then
- begin
- FAlignText := Value;
- RecreateWnd;
- Invalidate;
- end;
- end;
- procedure TTransEdit.SetTransparent(Value: Boolean);
- begin
- if FTransparent <> Value then
- begin
- FTransparent := Value;
- Invalidate;
- end;
- end;
- procedure TTransEdit.WMEraseBkGnd(var Message: TWMEraseBkGnd);
- var
- DC: hDC;
- i: integer;
- p: TPoint;
- begin
- if FTransparent then
- begin
- if Assigned(Parent) then
- begin
- DC := Message.DC;
- i := SaveDC(DC);
- p := GetScreenClient(self);
- p.x := -p.x;
- p.y := -p.y;
- MoveWindowOrg(DC, p.x, p.y);
- SendMessage(Parent.Handle, $, DC, );
- TCtrl(Parent).PaintControls(DC, nil);
- RestoreDC(DC, i);
- end;
- end else inherited;
- end;
- procedure TTransEdit.WMPaint(var Message: TWMPaint);
- begin
- inherited;
- if FTransparent then
- if not FPainting then
- RepaintWindow;
- end;
- procedure TTransEdit.WMNCPaint(var Message: TMessage);
- begin
- inherited;
- end;
- procedure TTransEdit.CNCtlColorEdit(var Message: TWMCtlColorEdit);
- begin
- inherited;
- if FTransparent then
- SetBkMode(Message.ChildDC, );
- end;
- procedure TTransEdit.CNCtlColorStatic(var Message: TWMCtlColorStatic);
- begin
- inherited;
- if FTransparent then
- SetBkMode(Message.ChildDC, );
- end;
- procedure TTransEdit.CMParentColorChanged(var Message: TMessage);
- begin
- inherited;
- if FTransparent then
- Invalidate;
- end;
- procedure TTransEdit.WMSize(var Message: TWMSize);
- begin
- inherited;
- Invalidate;
- end;
- procedure TTransEdit.WMMove(var Message: TWMMove);
- begin
- inherited;
- Invalidate;
- end;
- procedure TTransEdit.RepaintWindow;
- var
- DC: hDC;
- TmpBitmap, Bitmap: hBitmap;
- begin
- if FTransparent then
- begin
- FPainting := true;
- HideCaret(Handle);
- DC := CreateCompatibleDC(GetDC(Handle));
- TmpBitmap := CreateCompatibleBitmap(GetDC(Handle), Succ(ClientWidth), Succ(ClientHeight));
- Bitmap := SelectObject(DC, TmpBitmap);
- PaintTo(DC, , );
- BitBlt(GetDC(Handle), BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, , , SRCCOPY);
- SelectObject(DC, Bitmap);
- DeleteDC(DC);
- ReleaseDC(Handle, GetDC(Handle));
- DeleteObject(TmpBitmap);
- ShowCaret(Handle);
- FPainting := false;
- end;
- end;
- procedure TTransEdit.CreateParams(var Params: TCreateParams);
- const
- Alignments: array [TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
- begin
- inherited CreateParams(Params);
- Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
- end;
- procedure TTransEdit.Change;
- begin
- RepaintWindow;
- inherited Change;
- end;
- procedure TTransEdit.SetParent(AParent: TWinControl);
- begin
- inherited SetParent(AParent);
- end;
- // Transparent Memo
- constructor TTransMemo.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FAlignText := taLeftJustify;
- FTransparent := false;
- FPainting := false;
- end;
- destructor TTransMemo.Destroy;
- begin
- inherited Destroy;
- end;
- procedure TTransMemo.SetAlignText(Value: TAlignment);
- begin
- if FAlignText <> Value then
- begin
- FAlignText := Value;
- RecreateWnd;
- Invalidate;
- end;
- end;
- procedure TTransMemo.SetTransparent(Value: Boolean);
- begin
- if FTransparent <> Value then
- begin
- FTransparent := Value;
- Invalidate;
- end;
- end;
- procedure TTransMemo.WMEraseBkGnd(var Message: TWMEraseBkGnd);
- var
- DC: hDC;
- i: integer;
- p: TPoint;
- begin
- if FTransparent then
- begin
- if Assigned(Parent) then
- begin
- DC := Message.DC;
- i := SaveDC(DC);
- p := GetScreenClient(self);
- p.x := -p.x;
- p.y := -p.y;
- MoveWindowOrg(DC, p.x, p.y);
- SendMessage(Parent.Handle, $, DC, );
- TCtrl(Parent).PaintControls(DC, nil);
- RestoreDC(DC, i);
- end;
- end else inherited;
- end;
- procedure TTransMemo.WMPaint(var Message: TWMPaint);
- begin
- inherited;
- if FTransparent then
- if not FPainting then
- RepaintWindow;
- end;
- procedure TTransMemo.WMNCPaint(var Message: TMessage);
- begin
- inherited;
- end;
- procedure TTransMemo.CNCtlColorEdit(var Message: TWMCtlColorEdit);
- begin
- inherited;
- if FTransparent then
- SetBkMode(Message.ChildDC, );
- end;
- procedure TTransMemo.CNCtlColorStatic(var Message: TWMCtlColorStatic);
- begin
- inherited;
- if FTransparent then
- SetBkMode(Message.ChildDC, );
- end;
- procedure TTransMemo.CMParentColorChanged(var Message: TMessage);
- begin
- inherited;
- if FTransparent then
- Invalidate;
- end;
- procedure TTransMemo.WMSize(var Message: TWMSize);
- begin
- inherited;
- Invalidate;
- end;
- procedure TTransMemo.WMMove(var Message: TWMMove);
- begin
- inherited;
- Invalidate;
- end;
- procedure TTransMemo.RepaintWindow;
- var
- DC: hDC;
- TmpBitmap, Bitmap: hBitmap;
- begin
- if FTransparent then
- begin
- FPainting := true;
- HideCaret(Handle);
- DC := CreateCompatibleDC(GetDC(Handle));
- TmpBitmap := CreateCompatibleBitmap(GetDC(Handle), Succ(ClientWidth), Succ(ClientHeight));
- Bitmap := SelectObject(DC, TmpBitmap);
- PaintTo(DC, , );
- BitBlt(GetDC(Handle), BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, , , SRCCOPY);
- SelectObject(DC, Bitmap);
- DeleteDC(DC);
- ReleaseDC(Handle, GetDC(Handle));
- DeleteObject(TmpBitmap);
- ShowCaret(Handle);
- FPainting := false;
- end;
- end;
- procedure TTransMemo.CreateParams(var Params: TCreateParams);
- const
- Alignments: array [TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
- begin
- inherited CreateParams(Params);
- Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
- end;
- procedure TTransMemo.Change;
- begin
- RepaintWindow;
- inherited Change;
- end;
- procedure TTransMemo.SetParent(AParent: TWinControl);
- begin
- inherited SetParent(AParent);
- end;
- end.
下面的代码如果memo已有内容,回到已有内容中间处修改,旧的文本笔画会和新的文本笔画混合。文本笔画背景未完全檫除。效果不好
- type
- TForm1 =class(TForm)
- private
- { Private declarations }
- FBitmap: TBitmap;
- FBrush: HBRUSH;
- Edit1: TEdit;
- Memo1: TMemo;
- Image2:TImage;//背景图片
- protected
- procedure WndProc(var Message: TMessage); override;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- FBitmap := TBitmap.Create;
- FBitmap.SetSize(Memo1.Width,Memo1.Height);
- FBitMap.Canvas.CopyRect(
- types.Rect(,,FBitmap.Width,FBitmap.Height),
- Image2.Canvas,
- types.Rect(memo1.Left,memo1.Top,memo1.Left+Memo1.Width,memo1.Top+Memo1.Height));
- FBrush := CreateSolidBrush(FBitmap.Handle);
- SetWindowLong(Edit1.Handle,GWL_EXSTYLE,GetWindowLong(Edit1.Handle,GWL_EXSTYLE) or WS_EX_TRANSPARENT); // 增加透明风格
- SetWindowLong(Memo1.Handle,GWL_EXSTYLE,GetWindowLong(Memo1.Handle,GWL_EXSTYLE) or WS_EX_TRANSPARENT);
- end;
- procedure TForm1.FormPaint(Sender: TObject);
- begin
- Canvas.Brush.Handle:=FBrush;
- Canvas.Rectangle(,,width,height);
- end;
- procedure TForm1.WndProc(var Message: TMessage);
- begin
- inherited;
- case Message.Msg of
- WM_CTLCOLOREDIT, WM_CTLCOLORSTATIC:
- SetBkMode(Message.WParam, TRANSPARENT);
- Message.Result := FBrush;//GetStockObject(NULL_BRUSH);
- end;
- end;
TEdit,TMemo背景透明(SetWindowLong(WS_EX_TRANSPARENT)增加透明风格)的更多相关文章
- TEdit,TMemo背景透明
The component below works perfectly, except for the following problem: 1) Saves the component below ...
- CSS实现背景透明而背景上的文字不透明完美解决
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- CSS实现背景透明而背景上的文字不透明
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- CSS实现DIV层背景透明而文字不透明
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- 【原】CSS实现背景透明,文字不透明,兼容所有浏览器
11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...
- CSS实现背景透明,文字不透明,兼容所有浏览器
11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...
- CSS实现背景透明,文字不透明(各浏览器兼容)
/*CSS*/.waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center; ...
- CSS实现背景透明,文字不透明(各浏览器兼容) (转)
/*CSS*/ .waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center ...
- css如何实现背景透明,文字不透明?
之前做了个半透明弹层,但设置背景半透明时,子元素包含的字体及其它元素也都变成了半透明.对opacity这个属性认识的不透彻,在这里做一些总结,方便以后使用. 背景透明,文字不透明的解决方法: ...
随机推荐
- Codeforces Round #450 (Div. 2) D.Unusual Sequences (数学)
题目链接: http://codeforces.com/contest/900/problem/D 题意: 给你 \(x\) 和 \(y\),让你求同时满足这两个条件的序列的个数: \(a_1, a_ ...
- angular material dialog应用
1. 打开弹窗的点击事件 project.component.html <button mat-icon-button class="action-button" (clic ...
- javascript进阶课程--第三章--匿名函数和闭包
javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...
- TensorFlow 学习(十五)—— tensorflow.python.platform
tensorflow.python.platform 下的常用工具类和工具函数:tensorflow/tensorflow/python/platform at master · tensorflow ...
- 利用Eclipse+openJTAG调试led.axf文件
转自calvinlee1984 Subject:利用Eclipse+openJTAG调试led.axf文件 Date: 3-Mar-2011 By: Calvinlee1984 ...
- 如何在vue项目中使用百度编辑器ueditor
百度编辑器官方并没有提供vue项目使用说明,目前网上也有不少人实现了相关功能,这里就不再重复,只是针对自身项目碰到的情况做个记录,就当是熟悉了一遍富文本编辑器的代码结构. 按照网上的做法,基本可以实现 ...
- mina架构分析
使用的版本号是2.0.9 IoService分析 AbstractIoAcceptor定义了全部的public接口,并定义了子类须要实现的bindInternal函数,AbstractPollingI ...
- hprof教程 分类: B1_JAVA 2015-03-02 12:18 444人阅读 评论(0) 收藏
大部分内容参考http://www.linuxidc.com/Linux/2012-04/58178.htm J2SE中提供了一个简单的命令行工具来对java程序的cpu和heap进行 profili ...
- 在云平台上基于Go语言+Google图表API提供二维码生成应用
二维码能够说已经深深的融入了我们的生活其中.到处可见它的身影:但通常我们都是去扫二维码, 曾经我们分享给朋友一个网址直接把Url发过去,如今我们能够把自己的信息生成二维码再分享给他人. 这里就分享一下 ...
- 使用Apache FtpServer搭建FTP服务器 [FlashFXP]
<server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w ...