Solved

Combobox with png files

Posted on 2012-03-21
7
811 Views
Last Modified: 2012-05-18
Hi,

I need a way to put png files (loaded in runtime from files) directly on combobox itemns.
Like: <png file> item text
         <png file> item text  
         etc

I've always a pngobject file that allows my delphy to handle png files.

How can I do that?
0
Comment
Question by:scoopbh
  • 5
7 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 37751376
Load your PNGs into an imagelist and try this...
How to place images in a Combo Box
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37756770
Have you already checked my post?

Versions prior to Delphi 2009 do not support PNG images!!!

Use extra components to add PNG support for these Delphi versions:
pngcomponents.zip
Delphi PNG and MNG libraries
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37756787
You can also have a look at sourceforge project pngDelphi
0
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 37757019
Just a little demo code for you, which I have programmed in Delphi XE...

ComboBox with PNG images...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,ExtCtrls, ImgList;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

uses pngImage;

var
  png1,png2,png3,png4: TPngImage;

{$R *.dfm}

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  png: TPngImage;
  bmp: TBitmap;
  Offset: Integer;
begin
  offset:=0;
  bmp:=TBitmap.Create;

  with ComboBox1.Canvas do begin
    FillRect(Rect);
    png := TPngImage(ComboBox1.Items.Objects[index]);
    if png <> nil then begin
      //copy png to combobox
      bmp.Assign(png);

      BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, bmp.Width,
      bmp.Height), bmp, Bounds(0, 0, bmp.Width,
      bmp.Height), clred);

      Offset := bmp.width + 8;
    end;
    //Set the caption for each item
    TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index]);
  end;
  bmp.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  png1.free;
  png2.free;
  png3.free;
  png4.free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Style:=csOwnerDrawVariable;

  ComboBox1.ItemHeight:=20;

  png1:=TPngImage.Create;
  png2:=TPngImage.Create;
  png3:=TPngImage.Create;
  png4:=TPngImage.Create;

  png1.LoadFromFile('f:\db\public\betz\test.png');
  png2.LoadFromFile('f:\db\public\betz\test.png');
  png3.LoadFromFile('f:\db\public\betz\test.png');
  png4.LoadFromFile('f:\db\public\betz\test.png');


  ComboBox1.Items.Clear;
  ComboBox1.Items.AddObject('PNG Image 1', png1);
  ComboBox1.Items.AddObject('PNG Image 2', png2);
  ComboBox1.Items.AddObject('PNG Image 3', png3);
  ComboBox1.Items.AddObject('PNG Image 4', png4);

end;

end.

Open in new window


You can use attached PNG file test.png
test.png
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37764534
Any problems with my little demo???
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question