通过滚动不移动Ownerdraw TListBox子控件

1 delphi tlistbox

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  inherited;
  TListBox(Control).Canvas.FillRect(Rect);
  TListBox(Control).Canvas.TextOut(Rect.Left+5, Rect.Top+8, TListBox(Control).Items[Index]);
  if odSelected in State then
  begin
    Button.Left:=Rect.Right-80;
    Button.Top:=Rect.Top+4;
    Button.Visible:=true;
    Button.Invalidate;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.DoubleBuffered:=true;
  ListBox1.ItemHeight:=30;
  ListBox1.Style:=lbOwnerDrawFixed;
  Button:=TButton.Create(ListBox1);
  Button.Parent:=ListBox1;
  Button.DoubleBuffered:=true;
  Button.Visible:=false;
  Button.Width:=50;
  Button.Height:=20;
  Button.Caption:='BTN';
end;
Run Code Online (Sandbox Code Playgroud)

截图1

截图2

只有在使用ScrollBar或向我的ListBox发送WM_VSCROLL消息时才会出现重绘问题.当我通过使用键盘箭头或鼠标点击更改选择时,通常都会绘制.当通过滚动显示所选项目而不留下可见区域时,问题也不存在.

我认为Button.Top属性在DrawItem调用之前仍然具有旧值,并且稍后更改(例如为-30px).

Rem*_*eau 5

问题是您正在使用OnDrawItem事件来更改UI(在这种情况下,定位按钮).不要这样做,事件仅限于绘图.

我建议你要么:

  1. 继承ListBox以处理WM_VSCROLL消息,并让消息处理程序根据需要重新定位按钮.

    var
      PrevListBoxWndProc: TWndMethod;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      PrevListBoxWndProc := ListBox1.WindowProc;
      ListBox1.WindowProc := ListBoxWndProc;
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      ListBox1.WindowProc := PrevListBoxWndProc;
    end;
    
    procedure TForm1.PositionButton(Index: Integer);
    var
      R: TRect;
    begin
      if Index <= -1 then
        Button.Visible := False
      else
      begin 
        R := ListBox1.ItemRect(Index);
        Button.Left := R.Right - 80;
        Button.Top := R.Top + 4;
        Button.Visible := True;
      end;
    end;
    
    var
      LastIndex: Integer = -1;
    
    procedure TForm1.ListBox1Click(Sender: TObject);
    var
      Index: Integer;
    begin
      Index := ListBox1.ItemIndex;
      if Index <> LastIndex then
      begin
        LastIndex := Index;
        PositionButton(Index);
      end;
    end;
    
    procedure TForm1.ListBoxWndProc(var Message: TMessage);
    begin
      PrevListBoxWndProc(Message);
      if Message.Msg = WM_VSCROLL then
        PositionButton(ListBox1.ItemIndex);
    end;
    
    Run Code Online (Sandbox Code Playgroud)
  2. TButton完全摆脱了.用于OnDrawItem将按钮的图像(您可以使用DrawFrameControl()DrawThemeBackground()为此)直接绘制到ListBox上,然后使用OnMouseDown/Upor OnClick事件检查鼠标是否在"按钮"上,如果是,则根据需要进行相应操作.

    var
      MouseX: Integer = -1;
      MouseY: Integer = -1;
    
    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
        Rect: TRect; State: TOwnerDrawState);
    var
      R: TRect;
      P: TPoint;
      BtnState: UINT;
    begin
      TListBox(Control).Canvas.FillRect(Rect);
      TListBox(Control).Canvas.TextOut(Rect.Left+5, Rect.Top+8, TListBox(Control).Items[Index]);
      if not (odSelected in State) then Exit;
      R := Rect(Rect.Right-80, Rect.Top+4, Rect.Right-30, Rect.Top+24);
      P := Point(MouseX, MouseY);
      BtnState := DFCS_BUTTONPUSH;
      if PtInRect(R, P) then BtnState := BtnState or DFCS_PUSHED;
      DrawFrameControl(TListBox(Control).Canvas.Handle, R, DFC_BUTTON, BtnState);
      InflateRect(R, -4, -4);
      DrawText(TListBox(Control).Canvas.Handle, 'BTN', 3, R, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
    end;
    
    procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if Button <> mbLeft then Exit;
      MouseX := X;
      MouseY := Y;
      ListBox1.Invalidate;
    end;
    
    procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if Button <> mbLeft then Exit;
      MouseX := -1;
      MouseY := -1;
      ListBox1.Invalidate;
    end;
    
    procedure TForm1.ListBox1Click(Sender: TObject);
    var
      P: TPoint;
      R: TRect;
      Index: Integer;
    begin
      P := Point(MouseX, MouseY);
      Index := ListBox1.ItemAtPos(P, True);
      if (Index = -1) or (Index <> ListBox1.ItemIndex) then Exit;
      R := ListBox1.ItemRect(Index);
      R := Rect(R.Right-80, R.Top+4, R.Right-30, R.Top+24);
      if not PtInRect(R, P) then Exit;
      // click is on selected item's "button", do something...
    end;
    
    Run Code Online (Sandbox Code Playgroud)