On 2019-11-23 23:59, ToddAndMargo via perl6-users wrote:
Hi All,

In windows "msg" only works on the pro version .  Do we
have some way of popping up messages to the user?

Many thanks,
-T

Hi All,

With the help of the nice folks over on Stack Overflow,
I wrote a module.

<WinMsg.pm6>
# unit module WinMsg;
# WinMsg.pm6

#`{
Reference: https://stackoverflow.com/questions/59105696/how-can-i-create-pop-up-windows-for-perl6-in-windows
}

use NativeCall;

sub WinMsg( Str $TitleStr, Str $MessageStr ) is export( :WinMsg ) {

   constant WCHAR              = uint16;
   constant INT                = int32;
   constant UINT               = uint32;
   constant HANDLE             = Pointer[void];
   constant LPWCTSTR           = CArray[WCHAR];
   constant MB_ICONEXCLAMATION = 0x00000030;

   # Note: the following two subs have to be embedded

sub MessageBoxW( HANDLE, LPWCTSTR, LPWCTSTR, UINT ) is native('user32') returns INT { * };

   sub to-c-str( Str $str ) returns CArray[WCHAR]  {
      my @str := CArray[WCHAR].new;
      for ( $str.comb ).kv -> $i, $char { @str[$i] = $char.ord; }
      @str[ $str.chars ] = 0;
      @str;
   }


# MessageBoxW( my $handle, to-c-str("๘❤ Raku is awesome ❤๖"), to-c-str("Hellö Wαrld"), MB_ICONEXCLAMATION );

MessageBoxW( my $handle, to-c-str( $MessageStr ), to-c-str( $TitleStr ), MB_ICONEXCLAMATION );
}
</WinMsg.pm6>


Test run line:

>perl6 -e "use lib '.'; use WinMsg :WinMsg; WinMsg( 'Super Duper Title', 'What? You were expecting something witty?' );"

-T

Reply via email to