Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:20:59+00:00 2026-05-23T14:20:59+00:00

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

  • 0

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…

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;

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

Thanks!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:20:59+00:00Added an answer on May 23, 2026 at 2:20 pm

    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.

    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.

    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;
    

    Screenshot

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following code from here to read an Excel file using
Here's a snippet of code from a shell script I have written: for src
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
I have my code here, it works fine from my home, where my user
Here is the problem from code chef : A set of N dignitaries have
We have a bit of an issue here. We have upgraded from Delphi 2006
I have strings like follow field_zip_code:48103 taxonomy:88 field_zip_code:48103 taxonomy:88 field_state:MI field_zip_code:48103 From here i
I have code examples from some of my previous work that help me to
I have borrowed code from this link PHP regex templating - find all occurrences
A lot of time I have to code from home. Normally I just remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.