您当前的位置:delphi教程 > Delphi组件开发 > Edit透明在WIN XP下有问题!高手请进来看一下!分不够可再加!

Edit透明在WIN XP下有问题!高手请进来看一下!分不够可再加!

制作了一个透明的Edit控件,主要是用API函数,但是很奇怪的问题出现,在WIN98和WIN200下都可以透明,在WIN   XP下却不可以完全透明,有文字出现的地方不透明的!真纳闷啊!  
   
        哪位高手能不能帮我看看呢,或者有别的更好的方法,可以帮我解决一下吗,非常感谢!  
   
      我的控件代码如下:  
  unit   EditPro;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Classes,   Controls,   StdCtrls,   Graphics,Dialogs;  
   
  const  
      TWM_Invalidate   =   WM_USER+1;  
   
  type  
      TEditPro   =   class(TEdit)  
      private  
          FAlignment:   TAlignment;     //水平对齐  
          FTransparent:   Boolean;       //透明  
          procedure   SetAlignment(Value:   TAlignment);  
          procedure   TInvalidate(var   Message:   TMessage);   message   TWM_Invalidate;  
          procedure   CNCTLCOLOREDIT(var   Message:   TWMCTLCOLOREDIT);   message   CN_CTLCOLOREDIT;  
          procedure   WMEraseBkgnd(var   Message:   TWMEraseBkgnd);   message   WM_ERASEBKGND;  
          procedure   WMMove(var   Message:   TMessage);   message   WM_MOVE;  
          {   Private   declarations   }  
      protected  
          procedure   CreateWnd;   override;  
          procedure   CreateParams(var   Params:   TCreateParams);   override;  
          procedure   DoExit;   override;  
          procedure   DoEnter;   override;  
          {   Protected   declarations   }  
      public  
          constructor   Create(AOwner:   TComponent);   override;  
          procedure   Invalidate;   override;  
          {   Public   declarations   }  
      published  
          property   Alignment:   TAlignment   Read   FAlignment   Write   SetAlignment   Default   taLeftJustify;   //水平对齐属性  
          {   Published   declarations   }  
      end;  
   
  procedure   Register;  
   
  implementation  
   
  constructor   TEditPro.Create(AOwner:TComponent);  
  begin  
      inherited   Create(AOwner);  
      Width   :=   70;  
      Height   :=   25;  
      FTransparent   :=   True;  
  end;  
   
  procedure   TEditPro.CreateWnd;  
  begin  
      inherited   CreateWnd;  
      if   FTransparent   then  
      begin  
          SetWindowLong(Parent.Handle,   GWL_STYLE,  
          GetWindowLong(Parent.Handle,   GWL_STYLE)   and   not   WS_CLIPCHILDREN);  
      end;  
  end;  
   
  procedure   TEditPro.SetAlignment(Value:   TAlignment);  
  begin  
      if   Value   <>   FAlignment   then  
      begin  
          FAlignment   :=   Value;  
          RecreateWnd;  
      end;  
  end;  
   
  procedure   TEditPro.TInvalidate(var   Message:TMessage);  
  var  
      r:   TRect;  
  begin  
      if   (Parent   <>   nil)   and   FTransparent   then  
      begin  
          r   :=   ClientRect;  
          r.TopLeft   :=   Parent.ScreenToClient(ClientToScreen(r.TopLeft));  
          r.BottomRight   :=   Parent.ScreenToClient(ClientToScreen(r.BottomRight));  
          RedrawWindow(Handle,   nil,   0,   RDW_FRAME   +   RDW_INVALIDATE);  
      end;  
  end;  
   
  procedure   TEditPro.CNCTLCOLOREDIT(var   Message:   TWMCTLCOLOREDIT);  
  begin  
      if   FTransparent   then  
      with   Message   do  
      begin  
          SetBkMode(ChildDC,   Windows.TRANSPARENT);  
          Result   :=   GetStockObject(HOLLOW_BRUSH)  
      end  
      else  
          inherited;  
  end;  
   
  procedure   TEditPro.WMEraseBkgnd(var   Message:   TWMERASEBKGND);  
  begin  
      if   FTransparent   then  
          PostMessage(Handle,   TWM_Invalidate,   0,   0)  
      else  
          inherited;  
  end;  
   
  procedure   TEditPro.WMMove(var   message:   TMessage);  
  begin  
      inherited;  
      if   FTransparent   then  
          SendMessage(Handle,   TWM_Invalidate,   0,   0)  
      else  
          Invalidate;  
  end;  
   
  procedure   TEditPro.CreateParams(var   Params:   TCreateParams);  
  const  
      Alignments:   array[TAlignment]   of   word   =   (DT_LEFT,   DT_RIGHT,   DT_CENTER);  
  begin  
      inherited   CreateParams(Params);  
      with   Params   do  
      begin  
          Style   :=   Style   or   Alignments[FAlignment];  
          ExStyle   :=   ExStyle   or   WS_EX_TRANSPARENT;  
      end;  
  end;  
   
  procedure   TEditPro.DoExit;  
  begin  
      inherited;  
      FTransparent   :=   True;  
      SetCursor(0);  
      RecreateWnd;  
  end;  
   
  procedure   TEditPro.DoEnter;  
  var  
      exstyle,   stdstyle:   LongInt;  
  begin  
      inherited;  
      FTransparent   :=   False;  
      StdStyle:=   Windows.GetWindowLong(Parent.handle,   GWL_EXSTYLE);  
      exStyle:=   StdStyle   and   not   WS_EX_TRANSPARENT;  
      Windows.SetWindowLong(Parent.handle,   GWL_EXSTYLE,   exStyle);  
      invalidate;  
  end;  
   
  procedure   TEditPro.Invalidate;  
  begin  
      if   FTransparent   then  
          SendMessage(Handle,   TWM_Invalidate,   0,   0)  
      else  
          inherited;  
  end;  
   
  procedure   Register;  
  begin  
      RegisterComponents('Standard',   [TEditPro]);  
  end;  
   
  end.  
 


查看回复
关于我们 | 网站地图 | 广告刊登 | 友情链接
Copyright ©2008 - 2010  Delphi2007.net  ,All Rights Reserved  粤ICP备09038647号
业务联系:allceoad#gmail.com QQ:20008835 最佳分辨率 1024×768