Recent

Author Topic: [SOLVED] ComboBox Appearance with Wide Bitmaps  (Read 6457 times)

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
[SOLVED] ComboBox Appearance with Wide Bitmaps
« on: April 22, 2014, 05:13:14 pm »
I am using a combobox to display bitmap images rather than text. (This is in a context where pictures are more useful than text.) the bitmaps are read from a laz resource.

Using qt and GTK2 the combo box appears as in the attachment. (This is qt. GTK2 is slightly different.)

Should the slider disappear?

I am using lazarus 1.2 with Linux Mint 16 on a PC. All is 64 bit.

« Last Edit: April 23, 2014, 06:24:40 pm by Windsurfer »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ComboBox Appearance with Wide Bitmaps
« Reply #1 on: April 23, 2014, 12:29:44 am »
what slider?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: ComboBox Appearance with Wide Bitmaps
« Reply #2 on: April 23, 2014, 10:50:43 am »
The slider or thumb that usually appears to the right of the combobox. However, I just noticed up/down arrows appearing at the top and bottom of the control, when I click on it.

So, it appears to be working, but not as I expected.

Pic1 shows the initial appearance,
Pic2 shows the appearance when first selected,
Pic3 shows the appearance when an item is selected.

Pic2 is the one that now puzzles me. I can see no reason for the blank items at the top. I would appreciate any comments that help me correct the blank items in Pic2.

The code is:
Code: [Select]
Procedure TfrmMainScreen.cbSymbolsDrawItem(Control: TWinControl;
  Index: Integer; ARect: TRect; State: TOwnerDrawState);
var
  ComboBox: TComboBox;
  symBitmap: TBitmap;
begin
  ComboBox := (Control as TComboBox);
  symBitmap := TBitmap.Create;
  try
    symBitmap.LoadFromLazarusResource(MySymbols[index]);
    with ComboBox.Canvas do
    begin
      ARect := Bounds(ARect.Left, Arect.Top, Arect.Left + 50, Arect.Top + 15);
      FillRect(ARect);
      if symBitmap.Handle <> 0 then Draw(ARect.Left, ARect.Top, symBitmap);
    end;
  finally
    symBitmap.Free;
  end;
End;

The initialization is:
Code: [Select]
MySymbols :=  TStringList.Create;
  MySymbols.Add('linessquares');
  MySymbols.Add('linescircles');
  MySymbols.Add('line');
  MySymbols.Add('squares');
  MySymbols.Add('circles');
  MySymbols.Add('points');

 {$I ../WindSirf64/WindSirf64.lrs}

The MySymbols string list is freed when the form is closed.

Edit: The FormActivate event should, I believe, clear the combobox items.

Code: [Select]
Procedure TfrmMainScreen.FormActivate(Sender: TObject);
Var
  I: Integer;
Begin
  cbSymbols.Clear;
  For I := 0 To 5 Do
    cbSymbols.Items.Add('');
  cbSymbols.ItemIndex := 0;
End;
« Last Edit: April 23, 2014, 11:09:31 am by Windsurfer »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: ComboBox Appearance with Wide Bitmaps
« Reply #3 on: April 23, 2014, 11:51:43 am »
Pic2 is the one that now puzzles me. I can see no reason for the blank items at the top.
Code: [Select]
  ...
    with ComboBox.Canvas do
    begin
      ARect := Bounds(ARect.Left, Arect.Top, Arect.Left + 50, Arect.Top + 15);

Why do you need to alter the ARect parameters you are given?
You need to ensure that you draw the top of each bitmap at the correct Y offset. If the bitmaps were stored in an imagelist, you would write code something like this:
Code: [Select]
  procedure TForm1.cbSymbolsDrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
var
  cnv: TCanvas;
  itemHeight: integer;
begin
  if not (Control is TComboBox) then Exit;
  cnv:=TComboBox(Control).Canvas;
  itemHeight:=Size(ARect).cy;
  cnv.FillRect(ARect);
  ImageList1.Draw(cnv, 0, index*itemHeight, index);
end;

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: ComboBox Appearance with Wide Bitmaps
« Reply #4 on: April 23, 2014, 01:59:50 pm »
Thanks HowardPC

I will try as you suggest, and report back.

I did try an ImageList, but had problems with png files, so changed to bitmaps and a resource file.

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: ComboBox Appearance with Wide Bitmaps
« Reply #5 on: April 23, 2014, 02:36:30 pm »
Hi HowardPC,

I need to play with the code a little more. It displays an image, but not all of them. When I store the png images in the Imagelist, the images only appear as 16x16 px images. This is why I redefined the size of ARect and used a resource file.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: ComboBox Appearance with Wide Bitmaps
« Reply #6 on: April 23, 2014, 02:59:09 pm »
TImageList has writeable Height and Width properties. You are by no means restricted to only 16x16 pixel images.
The only restriction is that all images in the list must share the same dimensions. So if you want to store images of various sizes you need more than one imagelist.

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: [SOLVED] ComboBox Appearance with Wide Bitmaps
« Reply #7 on: April 23, 2014, 06:24:00 pm »
Thanks HowardPC, this is so simple!

It appears that the image size should be set in the ImageList before graphics are loaded, or maybe I did not click 'Apply'.

I ended with this code in FormCreate:
Code: [Select]
  cbSymbols.Items.Clear;
  For I := 0 To 5 Do
    cbSymbols.Items.Add('');
  cbSymbols.ItemIndex := 0; 

and this code in the DrawItem event:
Code: [Select]
Procedure TfrmMainScreen.cbSymbolsDrawItem(Control: TWinControl;
  Index: Integer; ARect: TRect; State: TOwnerDrawState);
var
  cnv: TCanvas;
begin
  if not (Control is TComboBox) then Exit;
  cnv:=TComboBox(Control).Canvas;
  //Add 2 to Left and Top to centre 50 x18 image
  Imagelist1.Draw(cnv, ARect.Left+2, ARect.Top+2, index);
End;

 

TinyPortal © 2005-2018