Lazarus

Programming => General => Topic started by: 1HuntnMan on November 12, 2024, 07:17:14 pm

Title: URL Link from a Label Caption
Post by: 1HuntnMan on November 12, 2024, 07:17:14 pm
I thought this wouldn't be a complicated issue but can't find the equivalent in Laz for ShellExecute like used to be in Delphi 7. I think I used to use this but it's been a long time ago. See the Click Procedure code below which Laz errors: Identifier not found "ShellExecute" and also on the same line Error: Identifier not found "SW_SHOWNORMAL":

Code: Pascal  [Select][+][-]
  1. procedure TFrmAboutPMS.LblNFPAClick(Sender: TObject);
  2. var
  3.   strURL  : string;
  4. begin
  5.   strURL:= LblNFPA.Caption;
  6.   if Pos('https:\\', strURL) = 0 then
  7.     strURL:='https:\\'+strURL;
  8.   ShellExecute(handle,'open','extract strURL',nil,nil,SW_SHOWNORMAL);
  9.   //ShellExecute(0,nil,PChar(strURL),nil,nil,SW_SHOWNORMAL);
  10. end;
  11.  
Title: Re: URL Link from a Label Caption
Post by: BSaidus on November 12, 2024, 07:21:06 pm
Did you added windows in uses clause

Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.   Windows, ...
  4.  
Title: Re: URL Link from a Label Caption
Post by: Thaddy on November 12, 2024, 07:30:01 pm
It is also one of the first examples given in Delphi in 1996... How long is probably Chinese? Without disrespect to Chinese users, it it common to to refer to Chinese beijng complex, beijng is an intentional joke  :-X )
Title: Re: URL Link from a Label Caption
Post by: 1HuntnMan on November 12, 2024, 07:43:35 pm
Woops, yup, forgot to use Winders.  Added that and compiles but the Label doesn't react to the OnClick. I wonder if I should use TStaticText instead?  I'm trying that...
Code: Pascal  [Select][+][-]
  1. procedure TFrmAboutPMS.StaticTxtNFPAClick(Sender: TObject);
  2. var
  3.   strURL  : string;
  4. begin
  5.   strURL:= StaticTxtNFPA.Caption;
  6.   if Pos('https:\\', strURL) = 0 then
  7.     strURL:='https:\\'+strURL;
  8.   ShellExecute(handle,'open','extract strURL',nil,nil,SW_SHOWNORMAL);
  9.   //ShellExecute(0,nil,PChar(strURL),nil,nil,SW_SHOWNORMAL);
  10.   //ShellExecute(handle,'open','extract DBEditClientURL',nil,nil,SW_SHOWNORMAL);}
  11. end;
  12.  

Compiles but doesn't react to OnClick
Title: Re: URL Link from a Label Caption
Post by: BSaidus on November 12, 2024, 08:06:32 pm
Use a simple label,
it should work,
just do
'http://' 
  rather then
'http:\\'

Code: Pascal  [Select][+][-]
  1.   ShellExecute(0, 'open', PChar(label1.Caption), Nil, Nil, SW_SHOWNORMAL);
  2.  
Title: Re: URL Link from a Label Caption
Post by: Bart on November 12, 2024, 10:49:19 pm
Why don't you use OpenURL()?

Bart
Title: Re: URL Link from a Label Caption
Post by: TRon on November 13, 2024, 12:55:24 am
@Bart:
Must be a firm decision as something similar was asked (and same openURL suggested by me) a couple of days ago by TS here (https://forum.lazarus.freepascal.org/index.php/topic,69220.0.html).

@1HuntnMan:
I see you do exactly the same as in the other thread.

Could you enlighten us (or at least me because I am truly puzzled) , what in earth does 'extract strURL' actually do ?

Is that a tool registered to the URL mimetype on your system or something ?

edit:
Just make a simple test to check if things work as expected
Code: Pascal  [Select][+][-]
  1. procedure TFrmAboutPMS.StaticTxtNFPAClick(Sender: TObject);
  2. var
  3.   strURL  : string;
  4. begin
  5.   strURL:= 'https://www.google.com';
  6.   ShellExecute(0,nil,PChar(strURL),nil,nil,SW_SHOWNORMAL);
  7. end;
  8.  
If that works for you then work on obtaining the URL from the caption and modifying it. If the text in the string isn't an actual URL then shellexecute does not know what to actually do with that string either.

Title: Re: URL Link from a Label Caption
Post by: Remy Lebeau on November 13, 2024, 03:38:44 am
the Label doesn't react to the OnClick.

Did you check to make sure you have your handler assigned to the OnClick event? Have you put a breakpoint in it and try to debug it?

I wonder if I should use TStaticText instead?

I wouldn't. TStaticText is a windowed control, so it uses more resources than a TLabel does.

Compiles but doesn't react to OnClick

How do you know? Your code is not doing anything if ShellExecute() fails. Like passing a malformed url to it...
TinyPortal © 2005-2018