麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

發掘ListBox的潛力(二):鼠標拖放插入點提示

2019-11-18 18:05:59
字體:
來源:轉載
供稿:網友
 

鼠標拖放插入點提示

  鼠標拖放是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;

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 (i>ItemIndex) 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 (FDragOverObject<>nil) and (Sender<>FDragOverObject) 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;

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.Count>0 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.


上一篇:中港臺譯名對照表簡易語法版

下一篇:如何使程序在運行時自動注冊ActiveX控件

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 色日本视频| 狠狠干91 | 日韩欧美电影一区二区三区 | 黑色丝袜美美女被躁视频 | av在线免费看网站 | 欧美一级黄色录像片 | 中国fx性欧美xxxx | 嫩呦国产一区二区三区av | 成人黄色小视频在线观看 | 成人啪啪色婷婷久 | 视频二区国产 | 亚洲最大的成人网 | 手机国产乱子伦精品视频 | 日朝毛片 | 黄色二区三区 | 在线播放av片 | 日韩欧美精品中文字幕 | 日韩激情一区 | 欧美三级短视频 | 国产成人综合在线观看 | 精品国产乱码久久久久久丨区2区 | 日本aaaa片毛片免费观蜜桃 | 成人免费观看在线视频 | 狠狠操精品视频 | 99国产精品欲a | 成人一区二区在线观看视频 | 在线天堂中文在线资源网 | 成人一级在线 | arabxxxxvideos| 久久国产夫妻视频 | 欧美爱爱视频 | 亚州精品在线视频 | 国产女同疯狂激烈互摸 | 一区二区三区日韩电影 | 91精品国产乱码久久久久 | 久久精品a一级国产免视看成人 | 久久久久一区二区三区四区五区 | 国产亚洲精品久久久久久大师 | 最新一区二区三区 | 欧美日本一 | 伊人一二三四区 |