Solved

Combobox with png files

Posted on 2012-03-21
7
818 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 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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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