在Delphi7中实现停靠功能

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 blue_bat 的 blog

在delphi7中实现停靠功能
我们在使用delphi7开发应用系统过程中经常需要使用子窗口在主窗口上停靠的功能,如果对这一部分不熟练时,通常会到csdn等网站寻找各种相关的控件,或者参考delphi自带的例程docking,下面我给大家介绍一种能够凑乎用的简便方法。
1、在主窗口中添加四个panel和四个splitter,设置对齐上下左右四边。
2、设定四个panel的属性docksite属性为true。
3、左右panel添加 ondockdrop、ondockover、onundock事件如下:
procedure tfrmmain.pnlleftundock(sender: tobject; client: tcontrol;
newtarget: twincontrol; var allow: boolean);
begin
  if (sender as tpanel).visibledockclientcount = 1 then
  begin
    (sender as tpanel).width := 1;
  end;
end;
 
procedure tfrmmain.pnlleftdockdrop(sender: tobject;
  source: tdragdockobject; x, y: integer);
begin
  (sender as tpanel).width := max(source.control.undockwidth,(sender as tpanel).width);
end;
 
procedure tfrmmain.pnlleftdockover(sender: tobject;
  source: tdragdockobject; x, y: integer; state: tdragstate;
  var accept: boolean);
begin
  if state = dsdragenter then
  begin
    (sender as tpanel).width := max(source.control.undockwidth, (sender as tpanel).width);
  end
  else
  begin
    if state = dsdragleave then
    begin
      (sender as tpanel).width := 1;
    end;
  end;
end;

左右panel响应事件代码相同。
4、上下panel添加 ondockdrop、ondockover、onundock事件如下:
procedure tfrmmain.pnlbottomundock(sender: tobject; client: tcontrol;
  newtarget: twincontrol; var allow: boolean);
begin
  if (sender as tpanel).dockclientcount = 1 then
  begin
    (sender as tpanel).height := 1;
  end;
end;
 
procedure tfrmmain.pnlbottomdockdrop(sender: tobject;
  source: tdragdockobject; x, y: integer);
begin
  (sender as tpanel).height := max(source.control.undockheight,(sender as tpanel).height);
end;
 
procedure tfrmmain.pnlbottomdockover(sender: tobject;
  source: tdragdockobject; x, y: integer; state: tdragstate;
  var accept: boolean);
begin
  if state = dsdragenter then
  begin
    (sender as tpanel).height := max(source.control.undockheight, (sender as tpanel).height);
  end
  else
  begin

    if state = dsdragleave then
    begin
      (sender as tpanel).height := 1;
    end;
  end;
end;

上下两个panel响应事件代码相同。
5、创建新的窗体用于停靠到主窗体。
6、设置新窗体dragkind为dkdock,dragmode为dmautomatic。
7、在新窗体onclose事件中添加如下代码:
  if self.hostdocksite <> nil then
  begin
    self.manualdock(nil);
  end;
  action := cahide;
8、设置新窗体不自动创建。
9、在主创体中创建新窗体并显示。记住:用show,不要用showmodal。
这种方法是一个不很规范的方法,如果需要更细致的控制,最好参考delphi的docking示例。如果将此例中的某个panel更改为tabcontrol或者pageconrol,你可以得到更好的效果,不过代码需要稍微变化,有兴趣的哥们可以试试。

本文关键:停靠 Delphi Dock
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top