Mike Singleton wrote:
> How would I make a GUI perhaps using the code below to enable the end user to select 
>a file from a drop down list and have it then subsequently parse it? Thanks.
>  
> == Start ==
> use strict;
> my @files = glob('3D*.log');
> foreach(@files){
>   open(INFILE,$_) || die "Could not open file $_!  $!";
> while (<@files>) {
>       my @files = split /\s+/, $_, 9;
>       print join ',', @files;
> }
> 
> }
> ==== End =====

use Win32::GUI;
my $file = Win32::GUI::GetOpenFileName(
        -title => "Select an Input File",
        -directory => 'D:\\perl\\Site\\lib\\Auto\\Win32\\GUI',
        -file => "GUI.dll"
);
print $file, "\n";

__END__

or using Win32::API:

#!perl -w --

# uses a selection box to allow you to pick a file using wilcards
# specify a dir and a glob and the selection box will list the files
# and you can select/pick from them.

$| = 1;

use strict;
use Win32::API;
use vars qw(%A);

BEGIN { # get command line switches (normally in def module)
        for (my $ii = 0; $ii < @ARGV; ) {
                last if $ARGV[$ii] =~ /^--$/;
                if ($ARGV[$ii] =~ /^-{1,2}(.*)$/) {
                        splice @ARGV, $ii, 1;   # remove arg
                        my $tmp = $1;
                        if ($tmp =~ /^([\w]+)=\(([^\)]*)\)$/) {
                                $main::A{$1} = $2;
                        } elsif ($tmp =~ /^([\w]+)=(.*)$/) {
                                $main::A{$1} = $2;
                        } else {
                                $main::A{$1}++;
                        }
                } else { $ii++; }
        }
}

sub GetOpenFileName ($$);
sub dumpit ($;*);
my $debug = $A{d} || 0;

my $initdir = 'D:/docs';        # default start dir
my $filemask = '*.txt';         # default file mask

(my $prog = $0) =~ s|^.*[\\\/]||;
my $usage = <<EOD;

Usage: $prog [-d] [<start_dir> [<file_mask>]]
        -d              debug
        <start_dir>     initial directory for file selection (def: $initdir)
        <file_mask>     globbing mask for file selection (def: $filemask);

EOD

die $usage if $A{h} or $A{help};

# if command line args, get dir and mask

$initdir = $ARGV[0] if @ARGV;
$filemask = $ARGV[1] if @ARGV > 1;

# create popup for file selection

my $paths = GetOpenFileName ($initdir, $filemask);
if (not $paths) {
        print "Selection failed\n";
        exit;
}
if (@$paths > 1) {
        for (my $ii = 1; $ii < @$paths; $ii++) {
                print "Path: $paths->[0]\\$paths->[$ii]\n";
        }
} else {
        print "Path: $paths->[0]\n";
}

exit 0;

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub GetOpenFileName ($$) {
        my $dir = shift;
        my $file = shift;
        
        use constant OFN_ALLOWMULTISELECT => 0x00000200;
        use constant OFN_CREATEPROMPT => 0x00002000;
        use constant OFN_EXPLORER => 0x00080000;
        use constant OFN_HIDEREADONLY => 0x00000004;
        use constant OFN_NODEREFERENCELINKS => 0x00100000;
        use constant OFN_NOREADONLYRETURN => 0x00008000;
        use constant OFN_OVERWRITEPROMPT=> 0x00000002;

$dir =~ s|\/|\\|g;      # reverse slashes
my $GetActiveWindow = new Win32::API('user32', 'GetActiveWindow',
   ['V'], 'N') or die "GetActiveWindow:",
   Win32::FormatMessage(Win32::GetLastError());

my $GetOpenFileName = new Win32::API('comdlg32', 'GetOpenFileName', ['P'], 'N')
   or die "GetOpenFileName Call: ", Win32::FormatMessage (Win32::GetLastError);

my $lStructSize = 76;
my $hwnd = $GetActiveWindow->Call();
my $hInstance = 0;
# modify this to pick your own exts.
my $lpstrFilter = pack 'A*', "All Files\0*.*\0Text Files\0*.TXT;*.BAK\0" .
   "Perl Files\0*.pl;*.pm;*.cgi\0\0";
# modify this - keep it at least 40 long
my $lpstrCustomFilter = pack 'A*', "Fubar Files\0*.TMP\0\0" , ' ' x 256;
my $nFilterIndex = 1;   # base-1 index into lpStrFilter for default filter
my $lpstrFile = pack 'A*', $file . "\0" x (8192 - length $file);
my $lpstrFileTitle = pack 'A*', "\0" x 8192;
my $lpstrInitialDir = pack 'A*', "$dir\0\0";
my $lpstrTitle = pack 'A*', "Select Your File\0\0";
my $Flags = OFN_CREATEPROMPT | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT |
   OFN_EXPLORER | OFN_NODEREFERENCELINKS | OFN_NOREADONLYRETURN |
   OFN_OVERWRITEPROMPT;
my $nFileOffset = 0;
my $nFileExtension = 0;
my $lpstrDefExt = pack 'A*', "\0\0";
my $lCustData = 0;
my $lpfnHook = 0;
my $lpTemplateName = pack 'A*', "\0\0";

my @openfilename = (
        $lStructSize,           # 'lStructSize'         # DWORD (I)
        $hwnd,                  # 'hwndOwner'           # HWND (I)
        $hInstance,             # 'hInstance'           # HINSTANCE (I)
        $lpstrFilter,           # 'lpstrFilter'         # LPCTSTR (P)
        $lpstrCustomFilter,     # 'lpstrCustomFilter'   # LPTSTR (P)
        # item below should be 40 or more according to docs
        length $lpstrCustomFilter, # 'nMaxCustFilter'   # DWORD (I)
        $nFilterIndex,          # 'nFilterIndex'        # DWORD (I)
        $lpstrFile,             # 'lpstrFile'           # LPTSTR (P)
        length $lpstrFile,      # 'nMaxFile'            # DWORD (I)
        $lpstrFileTitle,        # 'lpstrFileTitle'      # LPTSTR (P)
        length $lpstrFileTitle, # 'nMaxFileTitle'       # DWORD (I)
        $lpstrInitialDir,       # 'lpstrInitialDir'     # LPCTSTR (P)
        $lpstrTitle,            # 'lpstrTitle'          # LPCTSTR (P)
        $Flags,                 # 'Flags'               # DWORD (I)
        $nFileOffset,           # 'nFileOffset'         # WORD (I)
        $nFileExtension,        # 'nFileExtension'      # WORD (I)
        $lpstrDefExt,           # 'lpstrDefExt'         # LPCTSTR (P)
        $lCustData,             # 'lCustData'           # DWORD (I)
        $lpfnHook,              # 'lpfnHook'            # LPOFNHOOKPROC (P)
        $lpTemplateName,        # 'lpTemplateName'      # LPCTSTR (P)
);
my $struct = pack "LLLppLLpLpLppLLLpLpp", @openfilename;

my $result = $GetOpenFileName->Call($struct);
print "RES: $result\n" if $debug;
return undef if not $result;

my $path = unpack 'A*', $openfilename[7];
my @paths = split "\0", $path;

return \@paths;

}

__END__


-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to