Solved

change pictures (newbie...)

Posted on 2001-09-01
12
216 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
12 Comments
 
LVL 5

Expert Comment

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

Expert Comment

by:mnasman
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

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!

 
LVL 5

Accepted Solution

by:
alanwhincup earned 100 total points
Comment Utility
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
Comment Utility
Why is it that when I click on an image my browser stays dead???
0
 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
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
Comment Utility
What browser and OS do you have???
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
I use IE5.
I think that using resource would be a good idea...!
I shall first try sugg. above...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

12 Experts available now in Live!

Get 1:1 Help Now