首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

自定义标题栏组件有有关问题,大家进来了看看,见源码

2012-02-26 
自定义标题栏组件有问题,大家进来了看看,见源码unitUnit_CaptionBarinterfaceusesSysUtils,Windows,Forms

自定义标题栏组件有问题,大家进来了看看,见源码
unit   Unit_CaptionBar;

interface

uses
    SysUtils,Windows,Forms,Classes,Controls,ComCtrls,StdCtrls,ExtCtrls,Messages;

type
    TCaptionBar=class(TCustomPanel)
    private
        FOnMouseDown:   TMouseEvent;
        procedure   DoMouseDown(var   Message:   TWMMouse;   Button:   TMouseButton;
        Shift:   TShiftState);
        procedure   WMLButtonDown(var   Message:   TWMLButtonDown);   message   WM_LBUTTONDOWN;
    protected
        procedure   Notification(AComponent:TComponent;Operation:TOperation);   override;
        procedure   MouseDown(Button:   TMouseButton;   Shift:   TShiftState;
        X,   Y:   Integer);   override;
    public
        constructor   Create(AOwner:TComponent);override;
        property   OnMouseDown:   TMouseEvent   read   FOnMouseDown   write   FOnMouseDown;
    end;
{   TCaptionBar   }

procedure   Register;

implementation

procedure   Register;
begin
    RegisterComponents( 'CaptionBar ',[TCaptionBar]);
end;

constructor   TCaptionBar.Create(AOwner:   TComponent);
begin
    inherited;
    Top:=0;
    Align:=alTop;
    Height:=19;
end;

procedure   TCaptionBar.DoMouseDown(var   Message:   TWMMouse;
    Button:   TMouseButton;   Shift:   TShiftState);
begin
    if   not   (csNoStdEvents   in   ControlStyle)   then
        with   Message   do
            if   (Width   >   32768)   or   (Height   >   32768)   then
                with   CalcCursorPos   do
                    MouseDown(Button,   KeysToShiftState(Keys)   +   Shift,   X,   Y)
            else
                MouseDown(Button,   KeysToShiftState(Keys)   +   Shift,   Message.XPos,   Message.YPos);
end;

procedure   TCaptionBar.MouseDown(Button:   TMouseButton;   Shift:   TShiftState;
    X,   Y:   Integer);
begin
    inherited;
    if   (Self.Owner   is   TForm)   then
    begin
        ReleaseCapture;
        (Self.Owner   as   TForm).Perform(WM_SYSCOMMAND,$F012,0);
    end;
end;

procedure   TCaptionBar.Notification(AComponent:   TComponent;
    Operation:   TOperation);
var
    AForm:TForm;
begin
    inherited;
    if   not   (csDesigning   in   ComponentState)   then
    begin
        //这段代码有问题,大家帮帮忙啊,关闭窗体时报错
        if   (Self.Owner   is   TForm)   then
        begin
            AForm:=(Self.Owner   as   TForm);


            SetWindowLong(AForm.Handle,GWL_STYLE,GetWindowLong(AForm.Handle,   GWL_STYLE)   and   (not   WS_CAPTION));
            AForm.Width:=AForm.ClientWidth;
            AForm.Height:=AForm.ClientHeight;
        end;
        //先谢谢了
    end;
end;

procedure   TCaptionBar.WMLButtonDown(var   Message:   TWMLButtonDown);
begin
    SendCancelMode(Self);
    inherited;
    if   csCaptureMouse   in   ControlStyle   then   MouseCapture   :=   True;
    DoMouseDown(Message,   mbLeft,   []);
end;
end.

[解决办法]
楼主其实应该可以注意到,Operation属性标志着不同时机的调用。

最简单的就是在Operation为opRemove的时候,跳过就可以了。

if not (csDesigning in ComponentState) and (Operation <> opRemove) then {xiammy: ...}
begin
// ...
end;
[解决办法]
试试:

if (Self.Owner is TForm) and (Operation = opInsert) then
begin
AForm:=(Self.Owner as TForm);
SetWindowLong(AForm.Handle,GWL_STYLE,GetWindowLong(AForm.Handle, GWL_STYLE) and (not WS_CAPTION));
AForm.Width:=AForm.ClientWidth;
AForm.Height:=AForm.ClientHeight;
end;

热点排行