Len,

This should get you started. I've only tested on activestate perl 5.8.6, and win98. You'll need a Win32::GUI build from CVS if you want to be able to click on the links,as Hook()ing WM_NOTIFY messages is broken in Win32::GUI V1.0.

Let me know how you get on.

Regards,
Rob.

#!perl
use strict;
use warnings;

use Win32::GUI;

# some constants
sub WM_USER          {1024};
sub WM_NOTIFY        {78};
sub WM_LBUTTONDOWN   {513};
sub EM_AUTOURLDETECT {return(WM_USER()+91);};
sub ENM_LINK         {67108864};
sub EN_LINK          {1803};

my $mw = Win32::GUI::Window->new(
 -title => "Richedit control with URLs",
 -pos => [100, 100],
 -size => [400,300],
);

# Create a richedit 2.0 control (note change to class, as Win32::GUI natively only supports
# richedit 1.0)
my $re = $mw->AddRichEdit(
 -width => $mw->ScaleWidth(),
 -height => $mw->ScaleHeight(),
 -class => "RichEdit20A",
);

# Turn on Auto-highlighting of links
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_autourldetect.asp
$re->SendMessage(EM_AUTOURLDETECT, 1, 0);

# Set the richedit's event mask to pass us EN_LINK messages
my $em = $re->GetEventMask();
$re->SetEventMask($em | ENM_LINK);

# Hook the EN_LINK message
# NOTE: hooking WM_NOTIFY messages if broken in Win32::GUI V1.0 - you need the head revision from CVS
# for this to work
$re->Hook(EN_LINK, \&handleLink);

# Put some text in the richedit control
$re->Text("Some text with a link: http://www.robmay.me.uk/win32gui/\n"; .
         "Some more text with another link: file:///C:/\n");

$mw->Show();

Win32::GUI::Dialog();

# Avoid bug in Win32::GUI V1.0 causing crash on exit with Richedit - fixed in CVS head revision
undef $re;

exit(0);

# Handler for the EN_LINK messages
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditnotificationmessages/en_link.asp
sub handleLink
{
 my ($object, $wParam, $lParam, $type, $msgcode) = @_;

# return if it's not the message we wanted - see the documentation for Hook()
 # for why this is necessary
 return if $type != WM_NOTIFY;
 return if $msgcode != EN_LINK;

 # lParam is a pointer to an ENLINK structure
 # This is pretty tortuous, but perl really doesn't expect to be handling
# pointers to real memory. What it does is treat $lParam as a real pointer
 # to memory, grabs 32 bytes from that memory location, and unpacks the
 # bytes as sets of long (4-byte) numbers
my ($hwndFrom, $idFrom, $code, $orig_msg, $orig_wParam, $orig_lParam, $cpMin, $cpMax) =
         unpack('LLLLLLLL', unpack("P32", pack('L', $lParam)));

 # if message is a left mouse down, then extract the link text
 # and print it (or launch it in a browser, or whatever ... )
 # See link above for what messages $orig_msg may contain
 if($orig_msg == WM_LBUTTONDOWN) {
   my $text = $object->GetTextRange($cpMin, ($cpMax-$cpMin));
   print "Clicked on link: $text\n";
 }

 return;
}


[EMAIL PROTECTED] wrote:


Does anyone know if it is possible to put a Hyperlink in a RichEdit control, and in particular a "file://" hyperlink?

Thanks

Len.


Reply via email to