# This script may be a better example. It loads a textfile
# to an Access Database. The user selects the file to load
# through the Open File Dialog. Notice that -owner it set to the
# $W window so that the user must exit the Open file dialog
# before allowing them back to the window it was called from.
# Script shows how to use the Status and Progress bar jointly.
# Progress bar shows progress of the file load based on the size
# of the file in bytes and the position of the record pointer in bytes.
use Win32::GUI;
use Win32::ODBC;
# Hide the Dos Window
($DOShwnd, $DOShinstance) = GUI::GetPerlWindow();
GUI::Hide($DOShwnd);
$M = GUI::MakeMenu(
"&File" => "File",
" > &Open" => "Open",
" > E&xit" => "Exit",
"&Help" => "File",
" > &About - Load EDI/ASN File" => "About",
);
$W = new GUI::Window(
-title => "Load EDI/ASN File",
-left => 100,
-top => 100,
-width => 500,
-height => 200,
-menu => $M,
-style => ws_sysmenu,
-name => "Window",
);
$B = new GUI::Bitmap('MCLogo.bmp');
$BITMAP = $W->AddLabel(
-left => 0,
-top => 0,
-style => 14 | WS_VISIBLE,
-name => "Bitmap",
);
$BITMAP->SetImage($B);
$SFont = new Win32::GUI::Font(
-name => "Tahoma",
-size => 8,
-weight => 700,
-height => -11,
);
$Status = $W->AddStatusBar(
-text => " ",
-font => $SFont,
);
$Progress = new GUI::ProgressBar(
$Status,
-width => $Status->Width/2,
-height => $Status->Height-3,
-left => $Status->Width/2,
-top => 2,
);
$Progress->Hide;
$W->Show;
GUI::Dialog();
sub About_Click {
$msg="";
$msg=$msg . "Program: Load EDI/ASN File\n";
$msg=$msg . " Version: r2.1\n";
$msg=$msg . " Date: June 1999\n";
$msg=$msg . " By: Eric Hansen\n";
$msg=$msg . " Information Technology Services, Inc.\n";
$msg=$msg . " Dallas, TX\n";
GUI::MessageBox($W,"$msg","About - Load EDI/ASN File",64,);
}
sub Open_Click {
# Open File Dialog Box
# show files with *.txt extension starting in C:\ directory
my $file = "*.txt\0" . " " x 256;
$file = GUI::GetOpenFileName(
-owner => $W,
-directory => "C:\\",
-title => "Load EDI/ASN File",
-file => $file,
);
# if the EDI file selected exists and is not empty, process it!
if (-s $file) {
# open the database connection
$DSN="EDIdsn";
$db = new Win32::ODBC($DSN);
if (! $db) {
GUI::MessageBox($W,
"Can't Establish Database Connection to DSN '$DSN'",
"Load EDI/ASN File - Error",16,);
return;
}
# we will commit or rollback based on whether an error occurs or not
$db->SetConnectOption($db->SQL_AUTOCOMMIT, $db->SQL_AUTOCOMMIT_OFF);
# setup the Status/Progress Bar and start timing the load process
$ticks=Win32::GetTickCount();
$size = -s $file;
$Status->Text("Loading $file...");
$Status->Update;
$Progress->SetPos(0);
$Progress->Show;
# open the EDI/ASN File Selected by the User
$ret="Y";
open(EDI,$file) || do {$ret="N";};
if ($ret eq "Y") {
$cnt=0;
# process each record in the EDI file
while ($rec=<EDI>) {
chomp($rec); # remove linefeed from end of input record
@fields=split(/,/,$rec); # parse the input record by delimiter into
an array
# ship_date logic for Y2K compliance to ensure a 4 digit year
$len=length($fields[1]);
if ($len == 6) {
$yr=(substr($fields[1],0,2) + 0);
$mody=substr($fields[1],2,4);
if ($yr < 80) {$yr+=2000;}
else {$yr+=1900;}
$fields[1]=$yr . $mody;
}
$sqltxt="INSERT INTO EDItbl VALUES (";
for ($i=0;$i<=17;$i++) {
if ($i == 0) {$sqltxt=$sqltxt . "'" . $fields[$i] . "'";}
else {$sqltxt=$sqltxt . ",'" . $fields[$i] . "'";}
}
$sqltxt=$sqltxt . ",'N')"; # deleted_flag set to "N" for "No"
$ret=$db->Sql($sqltxt); # execute the insert statement and get return
status
if ($ret) {
$error=$db->Error(); # capture the sql error message
$db->Transact($db->SQL_ROLLBACK); # rollback the data load
close(EDI); # close the EDI load file
$db->Close(); # close the database connection
$Progress->Hide;
$Status->Text(" ");
GUI::MessageBox($W,$error,
"Load EDI/ASN File - Error",16,);
GUI::MessageBox($W,$sqltxt,
"Load EDI/ASN File - SQL",16,);
GUI::MessageBox($W,"Database Rolled Back",
"Load EDI/ASN File - Status",64,);
return;
}
$cnt++;
$Progress->SetPos(tell(EDI)*100/$size);
$Progress->Update;
}
close(EDI); # close the EDI File
$db->Transact($db->SQL_COMMIT); # commit the transactions
$db->Close(); # close the database connection
$elapsed=Win32::GetTickCount();
$elapsed -= $ticks;
$Status->Text($cnt . " rows loaded in " . ($elapsed/1000) . " secs");
GUI::MessageBox($W,"$cnt rows loaded from $file",
"Load EDI/ASN File - Successful Run",64,);
$Progress->Hide;
$Status->Text(" ");
}
else {
$db->Close(); # close the database connection
$Progress->Hide;
$Status->Text(" ");
GUI::MessageBox($W,"Can't open file $file",
"Load EDI/ASN File - Error",16,);
}
}
}
sub Exit_Click {
Window_Terminate();
}
sub Window_Terminate {
if ($db) {$db->Close();}
GUI::Show($DOShwnd);
exit;
}
END {
if ($db) {$db->Close();}
GUI::Show($DOShwnd);
}
# End Script
-----Original Message-----
From: Moore, Paul [SMTP:[EMAIL PROTECTED]]
Sent: Friday, July 16, 1999 3:28 AM
To: '[EMAIL PROTECTED]'; '[EMAIL PROTECTED]'
Subject: RE: [perl-win32-gui] Win32::GUI Tutorial, part 1
> From: christopher paul [mailto:[EMAIL PROTECTED]]
>
> would be cool to see some things like "how to call up a color
> picker" a "file-open/save dialog" .. also eric's dbgrid would
> make a great chapter, if he's keen.
>
Thanks for the encouragement. I'll certainly look at colour pickers and
file dialogs - but I haven't got to the point where *I* know how to use
them yet, so I'll take things in the order I discover them for now.
I'll look at the dbgrid, but I was also thinking of using (when I get a
bit more advanced!) listviews and DBI to do a SQL query viewer (my job
is as a DBA, so it's relevant to me), as a way of learning list views.
Maybe I could include a comparison with the dbgrid approach...
Anyway, it's good to know the stuff is useful.
Paul.