国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

發(fā)掘ListBox的潛力(二):鼠標(biāo)拖放插入點(diǎn)提示

2019-11-18 18:05:59
字體:
供稿:網(wǎng)友
 

鼠標(biāo)拖放插入點(diǎn)提示

  鼠標(biāo)拖放是Windows常見的操作,比如拷貝文件就可用拖放方式進(jìn)行。在我們編寫的應(yīng)用程序中,有時(shí)為了方便用戶操作需要支持鼠標(biāo)拖放。對于大部分的VCL控件只要鼠標(biāo)將DragMode設(shè)為dmAutomatic,就可以在OnDragDrop、OnDragOver和OnEndDrag中處理拖放事件。與Drag類似的還有一個(gè)Dock方式用于支持控件懸浮,控件在懸浮時(shí)會(huì)顯示一個(gè)虛線框來表示懸浮位置,而Drag方式卻沒有這功能。現(xiàn)在讓我們嘗試在Listbox中顯示拖放插入點(diǎn)。
  上面提及的三個(gè)事件中OnDragOver是用來拖放鼠標(biāo)經(jīng)過控件上面時(shí)產(chǎn)生的,要顯示插入點(diǎn)提示當(dāng)然是在這里進(jìn)行處理了。事件中先用Listbox.ItemAtPos(Point(X, Y) , true)取鼠標(biāo)所有在的打目Index,再用Listbox.ItemRect(Index)取得作圖區(qū)域,最后在區(qū)域中畫出提示線框。下面給出代碼:

Unit1.pas內(nèi)容
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由多個(gè)Listbox共享,這里記錄當(dāng)前那個(gè)Listbox接受鼠標(biāo)拖放
    FDragOverItemIndex: Integer;  //記錄鼠標(biāo)所在條目的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

  文章為作者原創(chuàng),轉(zhuǎn)載前請先與本人聯(lián)系,轉(zhuǎn)載請注明文章出處、保留作者信息,謝謝支持!
=========================================================================}


procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
    i: integer;
begin
  //拖放完成,將內(nèi)容從原來的Listbox讀到目標(biāo)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的內(nèi)容
  if not Accept then Exit;
  if (FDragOverObject<>nil) and (Sender<>FDragOverObject) then
    DrawInsertLine; //鼠標(biāo)離開Listbox時(shí),擦除插入位置提示線框
  Index := TListBox(Sender).ItemAtPos(Point(X, Y) , true);
  if (FDragOverObject = Sender) and (FDragOverItemIndex = Index) then Exit; //當(dāng)鼠標(biāo)在同一條目上移動(dòng)時(shí),只畫一次即可
  if (FDragOverObject = Sender) and (FDragOverItemIndex <> Index) then
    DrawInsertLine; //鼠標(biāo)移到新位置,擦除舊的插入位置提示線框
  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.


上一篇:中港臺(tái)譯名對照表簡易語法版

下一篇:如何使程序在運(yùn)行時(shí)自動(dòng)注冊ActiveX控件

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點(diǎn)

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 吴旗县| 河北区| 长丰县| 黔西| 望谟县| 城固县| 额尔古纳市| 恩施市| 江西省| 黎川县| 元谋县| 洞头县| 景东| 香港| 邻水| 措美县| 祁连县| 腾冲县| 青铜峡市| 唐海县| 南充市| 交城县| 邢台市| 牡丹江市| 温宿县| 阿克苏市| 灵璧县| 永川市| 丰台区| 榆林市| 蒙自县| 蓬溪县| 嵊泗县| 永清县| 宜良县| 旌德县| 清新县| 云和县| 东乌珠穆沁旗| 佳木斯市| 长宁县|