透明的TEdit(0分)

  • 主题发起人 主题发起人 taozhiyu
  • 开始时间 开始时间
T

taozhiyu

Unregistered / Unconfirmed
GUEST, unregistred user!
发信人: ffan (舟~扬帆~远航), 信区: Delphi <br><br>标 &nbsp;题: 使用透明的TEdit <br><br>发信站: BBS 水木清华站 (Sun Mar 25 01:06:28 2001) <br><br>&nbsp; <br><br>&nbsp; &nbsp; 后来仔细用spy++看了看TEdit的消息流,又查了查MSDN, <br><br>找到了使用透明TEdit的方法,请大伙指正。 <br><br>&nbsp; &nbsp; 基本原理是这样的,当TEdit需要重画的时候,它会给它的parent发送一个 <br><br>WM_CTLCOLOREDIT消息,参数为ChildDC与ChildHwnd,分别是TEdit重化使用 <br><br>的HDC和TEdit的handle。返回值是擦除TEdit背景的HBRUSH。 <br><br>&nbsp; &nbsp; 假设Edit1的parent是Form1,它的位置在Image1上(这个也可以是Form1,无所谓) <br><br>&nbsp; &nbsp; 首先在Form1.onCreate中进行一些准备工作: <br><br>&nbsp; &nbsp; procedure TForm1.FormCreate(Sender: TObject); <br><br>&nbsp; &nbsp; var <br><br>&nbsp; &nbsp; &nbsp; Bmp: TBitmap; <br><br>&nbsp; &nbsp; begin <br><br>&nbsp; &nbsp; &nbsp; Bmp := TBitmap.Create; <br><br>&nbsp; &nbsp; &nbsp; Bmp.Width := Edit1.Width; <br><br>&nbsp; &nbsp; &nbsp; Bmp.Height := Edit1.Height; <br><br>&nbsp; &nbsp; &nbsp; BitBlt(Bmp.Canvas.Handle, 0, 0, Edit1.Width, Edit1.Height, <br><br>&nbsp; &nbsp; &nbsp; &nbsp; Image1.Canvas.Handle, Edit1.Left - Image1.Left, Edit1.Top - Image1. <br><br>&nbsp; &nbsp; &nbsp; &nbsp; Top, SRCCOPY); <br><br>&nbsp; &nbsp; &nbsp; Edit1.Brush.Handle := CreatePatternBrush(Bmp.Handle); <br><br>&nbsp; &nbsp; &nbsp; Bmp.Free; <br><br>&nbsp; &nbsp; end; <br><br>&nbsp; <br><br>&nbsp; &nbsp; 然后拦截WM_CTLCOLOREDIT消息(声明就省了): <br><br>&nbsp; &nbsp; procedure TForm1.HandleCTLColorEdit(var Msg: TWMCTLCOLOREDIT); <br><br>&nbsp; &nbsp; begin <br><br>&nbsp; &nbsp; &nbsp; if Msg.ChildWnd = Edit1.Handle then <br><br>&nbsp; &nbsp; &nbsp; begin <br><br>&nbsp; &nbsp; &nbsp; &nbsp; SetBkMode(Msg.ChildDC, TRANSPARENT); <br><br>&nbsp; &nbsp; &nbsp; &nbsp; Msg.Result := Edit1.Brush.Handle; <br><br>&nbsp; &nbsp; &nbsp; end; <br><br>&nbsp; &nbsp; end; <br><br>&nbsp; <br><br>&nbsp; &nbsp; 最后是释放Brush: <br><br>&nbsp; &nbsp; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); <br><br>&nbsp; &nbsp; begin <br><br>&nbsp; &nbsp; &nbsp; DeleteObject(Edit1.Brush.Handle); <br><br>&nbsp; &nbsp; end; <br><br>&nbsp; <br><br>好了,就是这样。以上代码在2000下调试通过。 <br><br>谁发现了更好的方法,可别忘了贴一把,呵呵。 <br><br>-- <br><br><br><br>&nbsp;He, just do it!
 
好事,提一把!
 
接受答案了.
 
后退
顶部