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----------------------

  1. unit TranComp;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7. StdCtrls, ComCtrls;
  8.  
  9. type
  10. TCtrl = class(TWinControl);
  11.  
  12. TTransEdit = class(TEdit)
  13. private
  14. FAlignText: TAlignment;
  15. FTransparent: Boolean;
  16. FPainting: Boolean;
  17. procedure SetAlignText(Value: TAlignment);
  18. procedure SetTransparent(Value: Boolean);
  19. procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  20. procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
  21. procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
  22. procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
  23. procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
  24. procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
  25. procedure WMSize(var Message: TWMSize); message WM_SIZE;
  26. procedure WMMove(var Message: TWMMove); message WM_MOVE;
  27. protected
  28. procedure RepaintWindow;
  29. procedure CreateParams(var Params: TCreateParams); override;
  30. procedure Change; override;
  31. procedure SetParent(AParent: TWinControl); override;
  32. public
  33. constructor Create(AOwner: TComponent); override;
  34. destructor Destroy; override;
  35. published
  36. property AlignText: TAlignment read FAlignText write SetAlignText default taLeftJustify;
  37. property Transparent: Boolean read FTransparent write SetTransparent default false;
  38. end;
  39.  
  40. // Transparent Memo
  41. TTransMemo = class(TMemo)
  42. private
  43. FAlignText: TAlignment;
  44. FTransparent: Boolean;
  45. FPainting: Boolean;
  46. procedure SetAlignText(Value: TAlignment);
  47. procedure SetTransparent(Value: Boolean);
  48. procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  49. procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
  50. procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
  51. procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
  52. procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
  53. procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
  54. procedure WMSize(var Message: TWMSize); message WM_SIZE;
  55. procedure WMMove(var Message: TWMMove); message WM_MOVE;
  56. protected
  57. procedure RepaintWindow;
  58. procedure CreateParams(var Params: TCreateParams); override;
  59. procedure Change; override;
  60. procedure SetParent(AParent: TWinControl); override;
  61. public
  62. constructor Create(AOwner: TComponent); override;
  63. destructor Destroy; override;
  64. published
  65. property AlignText: TAlignment read FAlignText write SetAlignText default taLeftJustify;
  66. property Transparent: Boolean read FTransparent write SetTransparent default false;
  67. end;
  68.  
  69. procedure Register;
  70.  
  71. implementation
  72.  
  73. const
  74. BorderRec: array[TBorderStyle] of Integer = (, -);
  75.  
  76. procedure Register;
  77. begin
  78. RegisterComponents('Transparent Components', [TTransEdit, TTransMemo]);
  79. end;
  80.  
  81. function GetScreenClient(Control: TControl): TPoint;
  82. var
  83. p: TPoint;
  84. begin
  85. p := Control.ClientOrigin;
  86. ScreenToClient(Control.Parent.Handle, p);
  87. Result := p;
  88. end;
  89.  
  90. constructor TTransEdit.Create(AOwner: TComponent);
  91. begin
  92. inherited Create(AOwner);
  93. FAlignText := taLeftJustify;
  94. FTransparent := false;
  95. FPainting := false;
  96. end;
  97.  
  98. destructor TTransEdit.Destroy;
  99. begin
  100. inherited Destroy;
  101. end;
  102.  
  103. procedure TTransEdit.SetAlignText(Value: TAlignment);
  104. begin
  105. if FAlignText <> Value then
  106. begin
  107. FAlignText := Value;
  108. RecreateWnd;
  109. Invalidate;
  110. end;
  111. end;
  112.  
  113. procedure TTransEdit.SetTransparent(Value: Boolean);
  114. begin
  115. if FTransparent <> Value then
  116. begin
  117. FTransparent := Value;
  118. Invalidate;
  119. end;
  120. end;
  121.  
  122. procedure TTransEdit.WMEraseBkGnd(var Message: TWMEraseBkGnd);
  123. var
  124. DC: hDC;
  125. i: integer;
  126. p: TPoint;
  127. begin
  128. if FTransparent then
  129. begin
  130. if Assigned(Parent) then
  131. begin
  132. DC := Message.DC;
  133. i := SaveDC(DC);
  134. p := GetScreenClient(self);
  135. p.x := -p.x;
  136. p.y := -p.y;
  137. MoveWindowOrg(DC, p.x, p.y);
  138. SendMessage(Parent.Handle, $, DC, );
  139. TCtrl(Parent).PaintControls(DC, nil);
  140. RestoreDC(DC, i);
  141. end;
  142. end else inherited;
  143. end;
  144.  
  145. procedure TTransEdit.WMPaint(var Message: TWMPaint);
  146. begin
  147. inherited;
  148. if FTransparent then
  149. if not FPainting then
  150. RepaintWindow;
  151. end;
  152.  
  153. procedure TTransEdit.WMNCPaint(var Message: TMessage);
  154. begin
  155. inherited;
  156. end;
  157.  
  158. procedure TTransEdit.CNCtlColorEdit(var Message: TWMCtlColorEdit);
  159. begin
  160. inherited;
  161. if FTransparent then
  162. SetBkMode(Message.ChildDC, );
  163. end;
  164.  
  165. procedure TTransEdit.CNCtlColorStatic(var Message: TWMCtlColorStatic);
  166. begin
  167. inherited;
  168. if FTransparent then
  169. SetBkMode(Message.ChildDC, );
  170. end;
  171.  
  172. procedure TTransEdit.CMParentColorChanged(var Message: TMessage);
  173. begin
  174. inherited;
  175. if FTransparent then
  176. Invalidate;
  177. end;
  178.  
  179. procedure TTransEdit.WMSize(var Message: TWMSize);
  180. begin
  181. inherited;
  182. Invalidate;
  183. end;
  184.  
  185. procedure TTransEdit.WMMove(var Message: TWMMove);
  186. begin
  187. inherited;
  188. Invalidate;
  189. end;
  190.  
  191. procedure TTransEdit.RepaintWindow;
  192. var
  193. DC: hDC;
  194. TmpBitmap, Bitmap: hBitmap;
  195. begin
  196. if FTransparent then
  197. begin
  198. FPainting := true;
  199. HideCaret(Handle);
  200. DC := CreateCompatibleDC(GetDC(Handle));
  201. TmpBitmap := CreateCompatibleBitmap(GetDC(Handle), Succ(ClientWidth), Succ(ClientHeight));
  202. Bitmap := SelectObject(DC, TmpBitmap);
  203. PaintTo(DC, , );
  204. BitBlt(GetDC(Handle), BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, , , SRCCOPY);
  205. SelectObject(DC, Bitmap);
  206. DeleteDC(DC);
  207. ReleaseDC(Handle, GetDC(Handle));
  208. DeleteObject(TmpBitmap);
  209. ShowCaret(Handle);
  210. FPainting := false;
  211. end;
  212. end;
  213.  
  214. procedure TTransEdit.CreateParams(var Params: TCreateParams);
  215. const
  216. Alignments: array [TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
  217. begin
  218. inherited CreateParams(Params);
  219. Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
  220. end;
  221.  
  222. procedure TTransEdit.Change;
  223. begin
  224. RepaintWindow;
  225. inherited Change;
  226. end;
  227.  
  228. procedure TTransEdit.SetParent(AParent: TWinControl);
  229. begin
  230. inherited SetParent(AParent);
  231. end;
  232.  
  233. // Transparent Memo
  234. constructor TTransMemo.Create(AOwner: TComponent);
  235. begin
  236. inherited Create(AOwner);
  237. FAlignText := taLeftJustify;
  238. FTransparent := false;
  239. FPainting := false;
  240. end;
  241.  
  242. destructor TTransMemo.Destroy;
  243. begin
  244. inherited Destroy;
  245. end;
  246.  
  247. procedure TTransMemo.SetAlignText(Value: TAlignment);
  248. begin
  249. if FAlignText <> Value then
  250. begin
  251. FAlignText := Value;
  252. RecreateWnd;
  253. Invalidate;
  254. end;
  255. end;
  256.  
  257. procedure TTransMemo.SetTransparent(Value: Boolean);
  258. begin
  259. if FTransparent <> Value then
  260. begin
  261. FTransparent := Value;
  262. Invalidate;
  263. end;
  264. end;
  265.  
  266. procedure TTransMemo.WMEraseBkGnd(var Message: TWMEraseBkGnd);
  267. var
  268. DC: hDC;
  269. i: integer;
  270. p: TPoint;
  271. begin
  272. if FTransparent then
  273. begin
  274. if Assigned(Parent) then
  275. begin
  276. DC := Message.DC;
  277. i := SaveDC(DC);
  278. p := GetScreenClient(self);
  279. p.x := -p.x;
  280. p.y := -p.y;
  281. MoveWindowOrg(DC, p.x, p.y);
  282. SendMessage(Parent.Handle, $, DC, );
  283. TCtrl(Parent).PaintControls(DC, nil);
  284. RestoreDC(DC, i);
  285. end;
  286. end else inherited;
  287. end;
  288.  
  289. procedure TTransMemo.WMPaint(var Message: TWMPaint);
  290. begin
  291. inherited;
  292. if FTransparent then
  293. if not FPainting then
  294. RepaintWindow;
  295. end;
  296.  
  297. procedure TTransMemo.WMNCPaint(var Message: TMessage);
  298. begin
  299. inherited;
  300. end;
  301.  
  302. procedure TTransMemo.CNCtlColorEdit(var Message: TWMCtlColorEdit);
  303. begin
  304. inherited;
  305. if FTransparent then
  306. SetBkMode(Message.ChildDC, );
  307. end;
  308.  
  309. procedure TTransMemo.CNCtlColorStatic(var Message: TWMCtlColorStatic);
  310. begin
  311. inherited;
  312. if FTransparent then
  313. SetBkMode(Message.ChildDC, );
  314. end;
  315.  
  316. procedure TTransMemo.CMParentColorChanged(var Message: TMessage);
  317. begin
  318. inherited;
  319. if FTransparent then
  320. Invalidate;
  321. end;
  322.  
  323. procedure TTransMemo.WMSize(var Message: TWMSize);
  324. begin
  325. inherited;
  326. Invalidate;
  327. end;
  328.  
  329. procedure TTransMemo.WMMove(var Message: TWMMove);
  330. begin
  331. inherited;
  332. Invalidate;
  333. end;
  334.  
  335. procedure TTransMemo.RepaintWindow;
  336. var
  337. DC: hDC;
  338. TmpBitmap, Bitmap: hBitmap;
  339. begin
  340. if FTransparent then
  341. begin
  342. FPainting := true;
  343. HideCaret(Handle);
  344. DC := CreateCompatibleDC(GetDC(Handle));
  345. TmpBitmap := CreateCompatibleBitmap(GetDC(Handle), Succ(ClientWidth), Succ(ClientHeight));
  346. Bitmap := SelectObject(DC, TmpBitmap);
  347. PaintTo(DC, , );
  348. BitBlt(GetDC(Handle), BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, , , SRCCOPY);
  349. SelectObject(DC, Bitmap);
  350. DeleteDC(DC);
  351. ReleaseDC(Handle, GetDC(Handle));
  352. DeleteObject(TmpBitmap);
  353. ShowCaret(Handle);
  354. FPainting := false;
  355. end;
  356. end;
  357.  
  358. procedure TTransMemo.CreateParams(var Params: TCreateParams);
  359. const
  360. Alignments: array [TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
  361. begin
  362. inherited CreateParams(Params);
  363. Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
  364. end;
  365.  
  366. procedure TTransMemo.Change;
  367. begin
  368. RepaintWindow;
  369. inherited Change;
  370. end;
  371.  
  372. procedure TTransMemo.SetParent(AParent: TWinControl);
  373. begin
  374. inherited SetParent(AParent);
  375. end;
  376.  
  377. end.

下面的代码如果memo已有内容,回到已有内容中间处修改,旧的文本笔画会和新的文本笔画混合。文本笔画背景未完全檫除。效果不好

  1. type
  2. TForm1 =class(TForm)
  3. private
  4. { Private declarations }
  5. FBitmap: TBitmap;
  6. FBrush: HBRUSH;
  7. Edit1: TEdit;
  8. Memo1: TMemo;
  9. Image2:TImage;//背景图片
  10. protected
  11. procedure WndProc(var Message: TMessage); override;
  12.  
  13. end;
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. begin
  16. FBitmap := TBitmap.Create;
  17. FBitmap.SetSize(Memo1.Width,Memo1.Height);
  18. FBitMap.Canvas.CopyRect(
  19. types.Rect(,,FBitmap.Width,FBitmap.Height),
  20. Image2.Canvas,
  21. types.Rect(memo1.Left,memo1.Top,memo1.Left+Memo1.Width,memo1.Top+Memo1.Height));
  22. FBrush := CreateSolidBrush(FBitmap.Handle);
  23.  
  24. SetWindowLong(Edit1.Handle,GWL_EXSTYLE,GetWindowLong(Edit1.Handle,GWL_EXSTYLE) or WS_EX_TRANSPARENT); // 增加透明风格
  25. SetWindowLong(Memo1.Handle,GWL_EXSTYLE,GetWindowLong(Memo1.Handle,GWL_EXSTYLE) or WS_EX_TRANSPARENT);
  26. end;
  27.  
  28. procedure TForm1.FormPaint(Sender: TObject);
  29. begin
  30. Canvas.Brush.Handle:=FBrush;
  31. Canvas.Rectangle(,,width,height);
  32. end;
  33. procedure TForm1.WndProc(var Message: TMessage);
  34. begin
  35. inherited;
  36. case Message.Msg of
  37. WM_CTLCOLOREDIT, WM_CTLCOLORSTATIC:
  38. SetBkMode(Message.WParam, TRANSPARENT);
  39. Message.Result := FBrush;//GetStockObject(NULL_BRUSH);
  40. end;
  41. end;

TEdit,TMemo背景透明(SetWindowLong(WS_EX_TRANSPARENT)增加透明风格)的更多相关文章

  1. TEdit,TMemo背景透明

    The component below works perfectly, except for the following problem: 1) Saves the component below ...

  2. CSS实现背景透明而背景上的文字不透明完美解决

    在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...

  3. CSS实现背景透明而背景上的文字不透明

    在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...

  4. CSS实现DIV层背景透明而文字不透明

    在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...

  5. 【原】CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...

  6. CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...

  7. CSS实现背景透明,文字不透明(各浏览器兼容)

    /*CSS*/.waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center; ...

  8. CSS实现背景透明,文字不透明(各浏览器兼容) (转)

    /*CSS*/ .waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center ...

  9. css如何实现背景透明,文字不透明?

    之前做了个半透明弹层,但设置背景半透明时,子元素包含的字体及其它元素也都变成了半透明.对opacity这个属性认识的不透彻,在这里做一些总结,方便以后使用.   背景透明,文字不透明的解决方法:   ...

随机推荐

  1. Codeforces Round #450 (Div. 2) D.Unusual Sequences (数学)

    题目链接: http://codeforces.com/contest/900/problem/D 题意: 给你 \(x\) 和 \(y\),让你求同时满足这两个条件的序列的个数: \(a_1, a_ ...

  2. angular material dialog应用

    1. 打开弹窗的点击事件 project.component.html <button mat-icon-button class="action-button" (clic ...

  3. javascript进阶课程--第三章--匿名函数和闭包

    javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...

  4. TensorFlow 学习(十五)—— tensorflow.python.platform

    tensorflow.python.platform 下的常用工具类和工具函数:tensorflow/tensorflow/python/platform at master · tensorflow ...

  5. 利用Eclipse+openJTAG调试led.axf文件

    转自calvinlee1984 Subject:利用Eclipse+openJTAG调试led.axf文件 Date:     3-Mar-2011 By:         Calvinlee1984 ...

  6. 如何在vue项目中使用百度编辑器ueditor

    百度编辑器官方并没有提供vue项目使用说明,目前网上也有不少人实现了相关功能,这里就不再重复,只是针对自身项目碰到的情况做个记录,就当是熟悉了一遍富文本编辑器的代码结构. 按照网上的做法,基本可以实现 ...

  7. mina架构分析

    使用的版本号是2.0.9 IoService分析 AbstractIoAcceptor定义了全部的public接口,并定义了子类须要实现的bindInternal函数,AbstractPollingI ...

  8. hprof教程 分类: B1_JAVA 2015-03-02 12:18 444人阅读 评论(0) 收藏

    大部分内容参考http://www.linuxidc.com/Linux/2012-04/58178.htm J2SE中提供了一个简单的命令行工具来对java程序的cpu和heap进行 profili ...

  9. 在云平台上基于Go语言+Google图表API提供二维码生成应用

    二维码能够说已经深深的融入了我们的生活其中.到处可见它的身影:但通常我们都是去扫二维码, 曾经我们分享给朋友一个网址直接把Url发过去,如今我们能够把自己的信息生成二维码再分享给他人. 这里就分享一下 ...

  10. 使用Apache FtpServer搭建FTP服务器 [FlashFXP]

    <server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w ...