想做一个透明的控件,是带焦点的,比如button, 是透明的,但也可以有焦点,用键盘也可以操作. 网上找的都是基于TGraphiccontrol的,不能有焦点.
我要的是基于TWinControl或TCustomControl的.  给出源码, 测试能用立即给分

解决方案 »

  1.   

    没事件写,找个资料给你看看:
    透明的控件, 一般继承自TGraphicControl的
    (就是那些没有handle属性, 不能有focus的控件, 如image)
    都有Transparent属性. 对TWinControl类的控件, 要实现透明只要完成以下
    四步基本上就成了.1.在Create中设定ControlStyle :=ControlStyle - [csOpaque];2. override 它的CreateParams方法, exstyle 里加上WS_EX_TRANSPARENT.3. 修改它的parent的window style, 去掉WS_CLIPCHILDREN.
    inherited CreateParams(Params);
    with Params do
    begin
    { 完全重画 }
    Style := Style and not WS_CLIPCHILDREN;
    Style := Style and not WS_CLIPSIBLINGS;
    { 增加透明 }
    ExStyle := ExStyle or WS_EX_TRANSPARENT;
    end;4. 截获WM_ERASEBKGND, 什么都不做直接返回1.(不搽除背景)一般有上面3步能成. 有些控件比如TPanel, 在它的paint中用了fillrect,
    所以要实现透明的话还要override 它的paint方法, 自己画.
    按钮透明需要进一步处理.
    createparams里加上style := style or BS_OWNERDRAW;
    然后在WM_DRAWITEM中自己画吧
      

  2.   

    to hotzhu:
      这个我也看到过,可实际上根本不行的,你自己试试就知道了. 
      

  3.   

    参考:Type
      TCustomTransparentControl = class(TCustomControl)
      private
        FInterceptMouse: Boolean;
      protected
        procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
        procedure CreateParams(var Params: TCreateParams); override;
        procedure InvalidateControlsUnderneath;
      public
        constructor Create(AOwner: TComponent); override;
        procedure Invalidate; override;
        property InterceptMouse: Boolean read FInterceptMouse write FInterceptMouse default False;
      end;implementation
    { TCustomTransparentControl }constructor TCustomTransparentControl.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      ControlStyle := ControlStyle + [csOpaque];
      Brush.Style := bsClear;
    end;procedure TCustomTransparentControl.InvalidateControlsUnderneath;
    var
      I: Integer;
      Invalidating: Boolean;
      Control: TControl;  procedure DoInvalidate(AControl: TControl);
      var
        I: Integer;
        Control: TControl;
      begin
        if AControl is TWinControl then
        begin
          if TWinControl(AControl).HandleAllocated then
            with TWinControl(AControl) do
            begin
              RedrawWindow(Handle, nil, 0, RDW_INVALIDATE or RDW_FRAME);
              InvalidateRect(Handle, nil, True);
            end;
          if (csAcceptsControls in AControl.ControlStyle) then
            for I := 0 to TWinControl(AControl).ControlCount - 1 do
            begin
              Control := TWinControl(AControl).Controls[I];
              DoInvalidate(Control);
            end;
        end else
          AControl.Invalidate;
      end;begin
      Invalidating := False;
      if HandleAllocated then
      begin
        for I := Parent.ControlCount - 1 downto 0 do
        begin
          Control := Parent.Controls[I];
          if Invalidating then
            DoInvalidate(Control)
          else if Control = Self then
            Invalidating := True;
        end;
        InvalidateRect(Parent.Handle, nil, True);
      end;
    end;procedure TCustomTransparentControl.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
    end;procedure TCustomTransparentControl.WMNCHitTest(var Message: TWMNCHitTest);
    begin
      if not FInterceptMouse then
        Message.Result := HTTRANSPARENT;
    end;procedure TCustomTransparentControl.Invalidate;
    begin
      InvalidateControlsUnderneath;
      inherited Invalidate;
    end;
      

  4.   


    http://bbs.yd153.com搜搜