Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combobox with png files

Posted on 2012-03-21
7
Medium Priority
?
848 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

610 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