德尔福:画选择和鼠标悬停效果上ListViewDrawItem [英] Delphi: draw selections and mouse-hover effects on ListViewDrawItem

查看:332
本文介绍了德尔福:画选择和鼠标悬停效果上ListViewDrawItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里code:德尔福:Canvas.FillRect名单中查看油漆行(我用的OwnerDraw)。

I have a code from here: Delphi: Canvas.FillRect in List View to paint rows (I use OwnerDraw).

因此​​,我需要绘制选择和鼠标悬停效果。并提请正常列大小调整的效果...

Therefore I need to draw selections and mouse-hover effects. And to draw a normal column resizing effect...

我有一个code向上和向下移动项目:

I have a code to move items up and down:

procedure TForm1.ListViewDragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  DragItem, DropItem, CurrentItem, NextItem: TListItem;
begin
  if Sender = Source then
    with TListView(Sender) do
    begin
      DropItem    := GetItemAt(X, Y);
      CurrentItem := Selected;
      while CurrentItem <> nil do
      begin
        NextItem := GetNextItem(CurrentItem, SdAll, [IsSelected]);
        if DropItem = nil then DragItem := Items.Add
        else
          DragItem := Items.Insert(DropItem.Index);
        DragItem.Assign(CurrentItem);
        CurrentItem.Free;
        CurrentItem := NextItem;
      end;
    end;
end;

procedure TForm1.ListViewDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
Accept := Sender = Update_ListBox;
end;

如何在ListViewDrawItem +列调整效果绘制选择和鼠标悬停效果?

how to draw selections and mouse-hover effects on ListViewDrawItem + column resizing effect?

谢谢!

推荐答案

如果你想画的选择和以其他方式控制的当前状态做出反应,你需要编写额外的code用于此目的,因为我们是所有者绘制控件。这意味着,我们到Windows说,哎,不画在客户端区域的任何东西,我会的。因此,Windows绘制任何东西,甚至没有选择,聚焦框和鼠标悬停效果。

If you want to draw selections and in other ways respond to the current state of the control, you need to write additional code for this purpose, because we are owner-drawing the control. This means that we say to Windows, "hey, don't draw anything in the client area, I'll do that". Therefore, Windows draws nothing, not even selections, focus rectangles, and mouse-hover effects.

幸运的是,这是相当容易手动实现此行为。事实上,在 OnCustomDraw 事件处理程序,您将得到一个国家参数,你可以阅读。这是一款集,以及一些可能的因素包括: odSelected odHotLight odFocused

Fortunately, it is rather easy to implement this behaviour manually. Indeed, in the OnCustomDraw event handler, you are given a State parameter, which you can read. This is a set, and some of the possible elements include odSelected, odHotLight, and odFocused.

在我们previous code栋,只增加了一些新的线路,我们到达

Building upon our previous code, adding only a few new lines, we arrive at

procedure TForm1.ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
  Rect: TRect; State: TOwnerDrawState);
var
  i: Integer;
  x1, x2: integer;
  r: TRect;
  S: string;
const
  DT_ALIGN: array[TAlignment] of integer = (DT_LEFT, DT_RIGHT, DT_CENTER);
begin
  if SameText(Item.SubItems[1], 'done') then
  begin
      Sender.Canvas.Font.Color := clWhite;
      Sender.Canvas.Brush.Color := clGreen;
  end
  else
    if Odd(Item.Index) then
    begin
      Sender.Canvas.Font.Color := clBlack;
      Sender.Canvas.Brush.Color := $F6F6F6;
    end
    else
    begin
      Sender.Canvas.Font.Color := clBlack;
      Sender.Canvas.Brush.Color := clWhite;
    end;
  if odSelected in State then                                                    // NEW!
  begin                                                                          // NEW!
    Sender.Canvas.Font.Color := clWhite;                                         // NEW!
    Sender.Canvas.Brush.Color := clNavy;                                         // NEW!
  end;                                                                           // NEW!
  Sender.Canvas.Brush.Style := bsSolid;
  Sender.Canvas.FillRect(Rect);
  x1 := 0;
  x2 := 0;
  r := Rect;
  Sender.Canvas.Brush.Style := bsClear;
  Sender.Canvas.Draw(3, r.Top + (r.Bottom - r.Top - bm.Height) div 2, bm);
  for i := 0 to ListView1.Columns.Count - 1 do
  begin
    inc(x2, ListView1.Columns[i].Width);
    r.Left := x1;
    r.Right := x2;
    if i = 0 then
    begin
      S := Item.Caption;
      r.Left := bm.Width + 6;
    end
    else
      S := Item.SubItems[i - 1];
    DrawText(Sender.Canvas.Handle,
      S,
      length(S),
      r,
      DT_SINGLELINE or DT_ALIGN[ListView1.Columns[i].Alignment] or
        DT_VCENTER or DT_END_ELLIPSIS);
    x1 := x2;
  end;
  if odFocused in State then                                                     // NEW!
    DrawFocusRect(Sender.Canvas.Handle, Rect);                                   // NEW!
end;

请注意,马行被选中并有键盘焦点。

Notice that the 'horse' line is selected and has keyboard focus.

这篇关于德尔福:画选择和鼠标悬停效果上ListViewDrawItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆