?
Solved

Combobox with png files

Posted on 2012-03-21
7
Medium Priority
?
831 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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