Recent

Author Topic: [Solved] Can a thumbnail view be added to the checklistbox?  (Read 733 times)

loaded

  • Hero Member
  • *****
  • Posts: 824
[Solved] Can a thumbnail view be added to the checklistbox?
« on: March 30, 2023, 03:12:25 pm »
Hi All,
I need a simple layer manager similar to paint.net's layer manager.
Can the following view be made with CheckListBox?
If not, should I design a sample layer view inside the panel and duplicate this template at runtime?
« Last Edit: March 30, 2023, 07:26:48 pm by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Can a thumbnail view be added to the checklistbox?
« Reply #1 on: March 30, 2023, 03:43:41 pm »
I am not that sure about CheckListBox and Bitmaps (as downscaled image) but VirtualStringTree can do it for sure.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Can a thumbnail view be added to the checklistbox?
« Reply #2 on: March 30, 2023, 05:21:57 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf, LCLType;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   i: Integer;
  7. begin
  8.   CheckListBox1.ItemHeight := 94;
  9.   CheckListBox1.Style := lbOwnerDrawFixed;
  10.   for i := 0 to ImageList1.Count - 1 do
  11.     CheckListBox1.Items.Add(Format('Image %2d', [i]));
  12. end;
  13.  
  14. procedure TForm1.RadioButton1Change(Sender: TObject);
  15. begin
  16.   CheckListBox1.Invalidate;
  17. end;
  18.  
  19. procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
  20. const
  21.   chbState: array [0..1] of Integer = (DFCS_FLAT or DFCS_BUTTONCHECK, DFCS_FLAT or DFCS_BUTTONCHECK or DFCS_CHECKED);
  22. var
  23.   bmp: TBitmap;
  24.   checkboxsize: TSize;
  25.   checkboxrect: TRect;
  26.   textpoint, imagepoint: TPoint;
  27.   th: Integer;
  28. begin
  29.   with TCheckListBox(Control) do
  30.   begin
  31.     if Checked[Index] then
  32.       Canvas.Font.Style := Canvas.Font.Style + [fsBold];
  33.     Canvas.FillRect(ARect);
  34.     if Index < Items.Count then
  35.     begin
  36.       checkboxsize.cx := GetSystemMetrics(SM_CXMENUCHECK);  //checkbox width
  37.       checkboxsize.cy := GetSystemMetrics(SM_CYMENUCHECK);  //checkbox height
  38.       th := Canvas.TextHeight(Items[Index]);
  39.       checkboxrect.Top := ARect.Top + (ARect.Height - checkboxsize.cy) div 2;
  40.       checkboxrect.Bottom := checkboxrect.Top + checkboxsize.cy;
  41.       textpoint.Y := ARect.Top + ((ARect.Height - th) div 2);
  42.       imagepoint.Y := ARect.Top + 2;
  43.       if RadioButton1.Checked then
  44.       begin
  45.         checkboxrect.Left := ARect.Left + 20;
  46.         textpoint.X := ARect.Left + 20 + checkboxsize.cx + 20;
  47.         imagepoint.X := Arect.Width - ImageList1.Width - 2;
  48.       end
  49.       else
  50.       begin
  51.         checkboxrect.Left := ARect.Right - 20 - checkboxsize.cx;
  52.         imagepoint.X := ARect.Left + 2;
  53.         textpoint.X := imagepoint.X + 160 {img width} + 20;
  54.       end;
  55.       checkboxrect.Right := checkboxrect.Left + checkboxsize.cx;
  56.       DrawFrameControl(Canvas.Handle, checkboxrect, DFC_BUTTON, chbState[Checked[Index].ToInteger]);   //checkbox
  57.       Canvas.TextRect(ARect, textpoint.X, textpoint.Y, Items[Index]);   //text
  58.       bmp := TBitmap.Create;
  59.       bmp.SetSize(ImageList1.Width, ImageList1.Height);
  60.       ImageList1.GetBitmap(Index, bmp);
  61.       Canvas.Draw(imagepoint.X, imagepoint.Y, bmp);  //image
  62.       bmp.Free;
  63.     end;
  64.   end;
  65. end;    
Best regards / Pozdrawiam
paweld

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: Can a thumbnail view be added to the checklistbox?
« Reply #3 on: March 30, 2023, 07:26:36 pm »
Special thanks to KodeZwerg and paweld for taking your precious time to reply.
In the solution you sent paweld, the checkboxes do not work when they move to the right. Is there a solution for this?
If there is no solution, it doesn't matter, I can also use it this way.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: [Solved] Can a thumbnail view be added to the checklistbox?
« Reply #4 on: March 30, 2023, 07:43:16 pm »
actually, I didn't check it. the following code solves this problem 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);    
  2. const  
  3.   chbState: array [0..1] of Integer = (DFCS_FLAT or DFCS_BUTTONCHECK, DFCS_FLAT or DFCS_BUTTONCHECK or DFCS_CHECKED);
  4. var
  5.   bmp: TBitmap;
  6.   checkboxsize: TSize;
  7.   checkboxrect: TRect;
  8.   textpoint, imagepoint: TPoint;
  9.   th: Integer;
  10. begin
  11.   with TCheckListBox(Control) do
  12.   begin
  13.     if Checked[Index] then
  14.       Canvas.Font.Style := Canvas.Font.Style + [fsBold];
  15.     Canvas.FillRect(ARect);
  16.     if Index < Items.Count then
  17.     begin
  18.       checkboxsize.cx := GetSystemMetrics(SM_CXMENUCHECK);  //checkbox width
  19.       checkboxsize.cy := GetSystemMetrics(SM_CYMENUCHECK);  //checkbox height
  20.       th := Canvas.TextHeight(Items[Index]);
  21.       checkboxrect.Top := ARect.Top + (ARect.Height - checkboxsize.cy) div 2;
  22.       checkboxrect.Bottom := checkboxrect.Top + checkboxsize.cy;
  23.       textpoint.Y := ARect.Top + ((ARect.Height - th) div 2);
  24.       imagepoint.Y := ARect.Top + 2;
  25.       if RadioButton1.Checked then
  26.       begin
  27.         CheckListBox1.BiDiMode := bdLeftToRight; // <-- Add this
  28.         checkboxrect.Left := ARect.Left + 20;
  29.         textpoint.X := ARect.Left + 20 + checkboxsize.cx + 20;
  30.         imagepoint.X := Arect.Width - ImageList1.Width - 2;
  31.       end
  32.       else
  33.       begin
  34.         CheckListBox1.BiDiMode := bdRightToLeft;  // <-- and this
  35.         checkboxrect.Left := ARect.Right - 20 - checkboxsize.cx;
  36.         imagepoint.X := ARect.Left + 2;
  37.         textpoint.X := imagepoint.X + 160 {img width} + 20;
  38.       end;
  39.       checkboxrect.Right := checkboxrect.Left + checkboxsize.cx;
  40.       DrawFrameControl(Canvas.Handle, checkboxrect, DFC_BUTTON, chbState[Checked[Index].ToInteger]);   //checkbox
  41.       Canvas.TextRect(ARect, textpoint.X, textpoint.Y, Items[Index]);   //text
  42.       bmp := TBitmap.Create;
  43.       bmp.SetSize(ImageList1.Width, ImageList1.Height);
  44.       ImageList1.GetBitmap(Index, bmp);
  45.       Canvas.Draw(imagepoint.X, imagepoint.Y, bmp);  //image
  46.       bmp.Free;
  47.     end;
  48.   end;
  49. end;      
Best regards / Pozdrawiam
paweld

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: [Solved] Can a thumbnail view be added to the checklistbox?
« Reply #5 on: March 30, 2023, 08:49:10 pm »
actually, I didn't check it. the following code solves this problem 
Yes it solved.
Thank you for every character you write. Respects.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: [Solved] Can a thumbnail view be added to the checklistbox?
« Reply #6 on: March 31, 2023, 02:47:30 am »
Interesting, my old install I like using 2.0.4 laz, the OnMeasureItem isn't published in this control..

Guess it wouldn't really matter in this case.

btw, what does the Virtual style do?
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018