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

x
?
Solved

change pictures (newbie...)

Posted on 2001-09-01
12
Medium Priority
?
228 Views
Last Modified: 2010-04-06
How am I to acomplish this (Just a beginner...):
Have some *.bmp pictures (in my exe folder- subfolder  ...\Pictures) and change them in 7 second intervals and when clicked on (the picture) it goes hyperlink i.e triggers my default browser to specified adress that is linked to spec. picture???
0
Comment
Question by:Bosanac
[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
12 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6448216
I would do it like this:

My example finds all bmp's in the ../Pictures/ dir and shows them in the order found after building up a list of them.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Running : Boolean;
  I : Integer;

implementation

{$R *.DFM}

procedure Delay(Ms : Longint);
var
  TickCount : LongInt;
  Msg : TMsg;
begin
  TickCount := GetTickCount;
  while GetTickCount - TickCount < Ms do
  begin
    if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
    begin
      TranslateMessage(Msg);
      DispatchMessage(Msg);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Could be set in Object Inspector
  Button2.Enabled := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  SearchRec : TSearchRec;
  StringList : TStringList;
begin
  Running := True;
  Button1.Enabled := False;
  I := -1;
  StringList := TStringList.Create;
  FindFirst('Pictures\*.bmp', faAnyFile, SearchRec);
  StringList.Add(SearchRec.Name);
  while FindNext(SearchRec) = 0 do
    StringList.Add(SearchRec.Name);
  FindClose(SearchRec);
  Button2.Enabled := True;
  while Running = True do
  begin
    Inc(I);
    if I = StringList.Count then
      I := 0;
    Image1.Picture.LoadFromFile('Pictures\' + StringList[I]);
    Delay(1000); // Change to however many millisecs between image changes
  end;
  StringList.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Running := False;
  Button1.Enabled := True;
  Button2.Enabled := False;
end;

procedure TForm1.Image1Click(Sender: TObject);
var
  S : string;
begin
  case I of
    0: S := 'www.test1.com';
    1: S := 'www.test2.com';
    2: S := 'www.test3.com';
    3: S := 'www.test4.com';
    4: S := 'www.test5.com';
  end;
  if S <> '' then
    ShellExecute(Handle, 'Open', PChar(S), nil, nil, SW_SHOWNORMAL);
end;

end.

Cheers,

Alan
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6448219
In my example ive got 2 TButtons, and a TImage. One Button is to start the SlideShow style thing and the other is to stop it. If you want a delay of 7 secs then just change the line "Delay(1000);" to "Delay(7000);". I've created my own procedure to delay the change between pictures instead of just using "Sleep(7000);" because my procedure processes other Application Messages when a Delay is in process.
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6448223
Also in my last procedure (Image1Click) you could get rid of the line if "S <> '' then" as it doesnt actually do anything.
0
Industry Leaders: 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!

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6448327
Hello

  I don't if you mean by the defualt broswers, but if you mean the defualt browers of the images, try this code


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Image1: TImage;
    Timer1: TTimer;

    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Image1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Path :string;
implementation

{$R *.DFM}
uses shellapi;



procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if ListBox1.Items.Count >= 0 then
  begin
   ListBox1.ItemIndex := ListBox1.ItemIndex + 1;
    Image1.Picture.LoadFromFile(Path + ListBox1.Items[ListBox1.ItemIndex]);
   if ListBox1.ItemIndex >= ListBox1.Items.Count -1 then
    ListBox1.ItemIndex := -1;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
 I: Integer;
 SearchRec: TSearchRec;
begin
  Path := ExtractFilePath(Application.ExeName) + 'Picture\';
 try
   I := FindFirst(path+ '*.bmp', faAnyFile, SearchRec);
   while I = 0 do
   begin
     listbox1.items.Add(SearchRec.Name);
     I := FindNext(SearchRec);
   end;
   FindClose(SearchRec);
 except
   raise;
 end;
  Timer1.Interval := 7000; // for 7 seconds
  ListBox1.ItemIndex := -1;
end;

procedure TForm1.Image1Click(Sender: TObject);
begin
  ShellExecute(handle, 'open',Pchar(Path + ListBox1.items[ListBox1.ItemIndex]),nil,nil,sw_shownormal);
end;

end.
0
 

Author Comment

by:Bosanac
ID: 6448728
Alan:
I have just compiled your example and something I do not understand here:
How do you know which picture is hyperlinked to which internet adress??
Example:
I have a banner (exchange.bmp) and is in my Pictures folder.Now when I click on it the hyperlink is supposed to take me to http://www.experts-exchange.com.
Now your app. loads random from my picture folder.How does it know the right h-link?There is danger also that user messes with order of pictures then I get all the wrong h-links...???
Also,when I click on the picture nothing happens.It is supposed to trigger my default browser and open the corresponding picture hyperlink....
MNAS...I mean triggering the default browser...
 
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6448964
My code makes a list of the bmp's in a TStringList named StringList in my code. The list is obtained by some code which finds bmp files in the ../Pictures/ directory. When finding the bmp files, it finds them in Alpha-Numerical order and puts them in a list from the first one it finds to the last. The Interger, I, which is referred to in the Image1Click procedure is the position of the image when listed in the StringList (0 being first in the list). All you have to do is determine a list of all your bmp's in alpha-numerical order and then just list assign a url to the correct Integer position in the Image1Click procedure.

If you know what the images are called and they wont be changing names or anything then this would be better because the user wont beable to tamper around so easily.
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 400 total points
ID: 6448990
This example may be better for your needs:

In this example you will probably see better how each image relates to its hyper link.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Running : Boolean;
  I : Integer;

const
  Pics : array[1..5] of string = ('exchange', 'delphi', 'test', 'global', 'hello');
  //List of images. Just the names, the extension is added on later as it is the same for each one.

implementation

{$R *.DFM}

procedure Delay(Ms : Longint);
var
  TickCount : LongInt;
  Msg : TMsg;
begin
  TickCount := GetTickCount;
  while GetTickCount - TickCount < Ms do
  begin
    if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
    begin
      TranslateMessage(Msg);
      DispatchMessage(Msg);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Could be set in Object Inspector
  Button2.Enabled := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Running := True;
  Button1.Enabled := False;
  I := 0;
  Button2.Enabled := True;
  while Running = True do
  begin
    Inc(I);
    if I = 5 then
      I := 1;
    if FileExists('Pictures\' + Pics[I] + '.bmp') then
      Image1.Picture.LoadFromFile('Pictures\' + Pics[I] + '.bmp')
    else
      ShowMessage('Image "' + Pics[I] + '.bmp" does not exist.');
      //Report an error if file isnt there if you want to....
    Delay(7000);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Running := False;
  Button1.Enabled := True;
  Button2.Enabled := False;
end;

procedure TForm1.Image1Click(Sender: TObject);
var
  S : string;
begin
  case I of
    1: S := 'www.experts-exchange.com';
    2: S := 'www.delphi.com';
    3: S := 'www.test.com';
    4: S := 'www.global.com';
    5: S := 'www.hello.com';
  end;
  ShellExecute(Handle, 'Open', PChar(S), nil, nil, SW_SHOWNORMAL);
end;

end.
0
 

Author Comment

by:Bosanac
ID: 6449000
Why is it that when I click on an image my browser stays dead???
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6449006
In my above example the images are defined in an array rather than being searched for. This will ensure that only the images with thoose names will be loaded. The array is the following line:

const
 Pics : array[1..5] of string = ('exchange', 'delphi', 'test', 'global', 'hello');

If you look at the above code you'll see that the [1..5} indicates how many strings are in the array and defines an integer number for each string. In the Image1Click procedure you will see the code "case I of" and theen a list of numbers with a hyperlink... I have coded this so that the number in I corresponds to the position of the string in the array. For example when I is 1 it will mean 'exchange' in the array pics so i have made it load the hyperlink 'www.experts-exchange.com' and so on....
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6449009
What browser and OS do you have???
0
 
LVL 34

Expert Comment

by:Slick812
ID: 6449592
if your images do NOT change then it might be best to put them in a .RES file and compile it with your app. You could just load the bmps from the resource and the user could not change them on disk.
0
 

Author Comment

by:Bosanac
ID: 6449673
I use IE5.
I think that using resource would be a good idea...!
I shall first try sugg. above...
0

Featured Post

Industry Leaders: 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

609 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