Solved

Combobox with png files

Posted on 2012-03-21
7
791 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
Comment Utility
Load your PNGs into an imagelist and try this...
How to place images in a Combo Box
0
 
LVL 19

Expert Comment

by:Thommy
Comment Utility
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
Comment Utility
You can also have a look at sourceforge project pngDelphi
0
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
Comment Utility
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
Comment Utility
Any problems with my little demo???
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
System restore point 4 77
delphi parse string to params 3 79
How to debug For loops? 3 33
Create a path if not exists 7 40
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now