发掘ListBox的潜力(二):鼠标拖放插入点提示

2016-02-19 18:24 11 1 收藏

下面是个简单易学的发掘ListBox的潜力(二):鼠标拖放插入点提示教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - 编程语言 】

 

鼠标拖放插入点提示

  鼠标拖放是Windows常见的操作,比如拷贝文件就可用拖放方式进行。在我们编写的应用程序中,有时为了方便用户操作需要支持鼠标拖放。对于大部分的VCL控件只要鼠标将DragMode设为dmAutomatic,就可以在OnDragDrop、OnDragOver和OnEndDrag中处理拖放事件。与Drag类似的还有一个Dock方式用于支持控件悬浮,控件在悬浮时会显示一个虚线框来表示悬浮位置,而Drag方式却没有这功能。现在让我们尝试在Listbox中显示拖放插入点。
  上面提及的三个事件中OnDragOver是用来拖放鼠标经过控件上面时产生的,要显示插入点提示当然是在这里进行处理了。事件中先用Listbox.ItemAtPos(Point(X, Y) , true)取鼠标所有在的打目Index,再用Listbox.ItemRect(Index)取得作图区域,最后在区域中画出提示线框。下面给出代码:
  
  Unit1.pas内容unit Unit1;

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  interface

  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

  type
    TForm1 = class(TForm)
      ListBox1: TListBox;
      ListBox2: TListBox;
      procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
      procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
        State: TDragState; var Accept: Boolean);
    private
      FDragOverObject: TObject;    //ListBox1DragDrop、ListBox1DragOver由多个Listbox共享,这里记录当前那个Listbox接受鼠标拖放
  
    FDragOverItemIndex: Integer;  //记录鼠标所在条目的Index
      procedure DrawInsertLine;
    public
      { Public declarations }
    end;

  var
    Form1: TForm1;

  implementation

  {$R *.dfm}
  

  {========================================================================
    DESIGN BY :  彭国辉
    DATE:        2004-12-24
    SITE:       
http://kacarton.yeah.net/
    BLOG:        http://blog.csdn.net/nhconch
    EMAIL:       kacarton#sohu.com

    文章为作者原创,转载前请先与本人联系,转载请注明文章出处、保留作者信息,谢谢支持!
  =========================================================================}

  
  procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
  var
      i: integer;
  begin
    //拖放完成,将内容从原来的Listbox读到目标Listbox
    with TListBox(Source) do begin
  
    i := TListBox(Sender).ItemAtPos(Point(X, Y) , true);
      if i-1 then
        TListBox(Sender).Items.InsertObject(i, Items[ItemIndex], Items.Objects[ItemIndex])
      else
        i := TListBox(Sender).Items.AddObject(Items[ItemIndex], Items.Objects[ItemIndex]);
      if (Sender=Source) and (iItemIndex) then i := i-1;
      DeleteSelected;
      if (Sender=Source) then ItemIndex := i;
    end;
    FDragOverObject := nil;
    FDragOverItemIndex := -1;
  end;

  procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
    State: TDragState; var Accept: Boolean);
  var
    Index: Integer;
  begin
    Accept := (Source is TListBox) and (TListBox(Source).ItemIndex-1);  //只接受来自Listbox的内容
  
  if not Accept then Exit;
    if (FDragOverObjectnil) and (SenderFDragOverObject) then
      DrawInsertLine; //鼠标离开Listbox时,擦除插入位置提示线框
  
  Index := TListBox(Sender).ItemAtPos(Point(X, Y) , true);
    if (FDragOverObject = Sender) and (FDragOverItemIndex = Index) then Exit; //当鼠标在同一条目上移动时,只画一次即可
    if (FDragOverObject = Sender) and (FDragOverItemIndex Index) then
      DrawInsertLine; //鼠标移到新位置,擦除旧的插入位置提示线框
  
  FDragOverObject := Sender;
    FDragOverItemIndex := Index;
    DrawInsertLine;   //画出插入位置提示线框
  end;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  procedure TForm1.DrawInsertLine;
  var
    R: TRect;
  begin
    if FDragOverObject = nil then Exit;
    with TListBox(FDragOverObject) do begin
  
    if FDragOverItemIndex -1 then begin
  
      R := ItemRect(FDragOverItemIndex);
        R.Bottom := R.Top + 4;
      end else if Items.Count0 then begin
        R := ItemRect(Items.Count-1);
        R.Top := R.Bottom - 4;
      end else begin
  
      windows.GetClientRect(Handle, R);
        R.Bottom := R.Top + 4;
      end;
  
    DrawFocusRect(Canvas.Handle, R);
      InflateRect(R, -1, -1);
      DrawFocusRect(Canvas.Handle, R);
    end;
  end;

  end.
  

来源:https://www.tulaoshi.com/n/20160219/1617562.html

延伸阅读
喜爱各种手工是一种赏心悦目的事情。它可以装饰家中的细节,这里为大家精心分享一份精致舒适的波点图案 布艺 鼠标垫DIY制作方法图解,有创意的人们不能错过哦!如果喜欢就马上来浏览这些DIY带来给大家的美好吧。 这款绿色波点图案的鼠标垫绝对符合你的胃口哦! DIY手工鼠标垫准备工具:垫子、胶、刷子、针线 DIY手工...
标签: FLASH flash教程
本文由 清风掠影 原创,转载请保留此信息! 明亮的屋子忽然暗了下来,原来是停电了,这时第一件事就是拿出一根蜡烛,tulaoShi.com点上~~~ 一、打开FLASH,新建一个影片剪辑元件,命名为“火柴”,绘制一根火柴(如图1),在41帧插入关键帧,在动作面版中输入gotoAndPlay(35);,插入一层,在第35帧插入关键帧,绘制火焰,在38帧插入关键帧,把火...
拖放(DragDrop)是Windows提供的一种快捷的操作方式。作为基于Windows的开发工具,Delphi同样支持拖放操作,而且开发应用系统的拖放功能十分方便,真正体现了Delphi的强大功能和方便性。 Delphi提供的所有控件(Control,即能获得输入焦点的部件)都支持拖放操作,并有相应的拖放属性、拖放事件和拖放方法。下面我们先介绍控件的拖放支持...
标签: Delphi
  拖放 (DragDrop)是 Windows 提供的一种快捷的操作方式。作为基于 Windows 的开发工具, Delphi同样支持拖放操作,而且开发应用系统的拖放功能十分方便,真正体现了 Delphi 的强大功能和方便性。 Delphi提供的所有控件 (Control ,即能获得输入焦点的部件 ) 都支持拖放操作,并有相应的拖放属性、拖放事件和拖放方法。下面我...
一.通过鼠标在屏幕上的移动来控件程序界面 本例通过鼠标在屏幕上的移动来控制程序窗体的显示与隐藏:当鼠标移动到窗体所在区域时窗体显示,反之隐藏起来。仅需一条API函数:GetCursorPos。注意:如果需要将API函数置于模块中请对代码作相应修改。要尝试本例,需给标准EXE工程缺省添加一个Timer控件。 PrivateTypePOINTAPI ...

经验教程

990

收藏

89
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部