免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5099 | 回复: 4
打印 上一主题 下一主题

perl Win32::GUI 中文显示为乱码,求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-14 01:44 |只看该作者 |倒序浏览
[ 本帖最后由 hmlijg 于 2014-06-14 02:00 编辑 ]

从一个网友的帖子里复制的perl代码,他提供的exe文件中文显示正常,我用他的代码重新生成exe文件后中文显示为乱码(图片在帖子末尾),请问各位高手,这是什么原因?



源代码:

#!/usr/bin/perl

# Written by Limeng
# April 1, 2012 Changsha
# 仅扫描大小相同的文件,大量节省扫描时间
# 改进耗时显示格式
# 改写GUI

use strict;
#use warnings;
use Win32::GUI qw( MB_ICONINFORMATION MB_OK );
use Win32::GUI::DropFiles();
use Encode qw/ encode /;
use Win32::OLE qw(in);
use Digest::MD5;
use Benchmark;

my $about= <<EOF;
/`\\ /`\\
(/\\ V /\\)        Limeng's Clone Files Finder V3.0
  /6 6\\          -- Written by Limeng o_*, April, 2012
>{= Y =}<
/'-^-'\\         All Rights Reserved 2010-3000 ChangSha
(_)""-(_)        Homepage: [url=http://limeng.class22.net]http://limeng.class22.net[/url]
                 E-mail: iamlimeng\@163.com
BlueIdea!
EOF

my $clone_report_file;
my %paths;
my @file_list;

my $w_m = 700;
my $h_m = 400;
my $icon = new Win32::GUI::Icon("icon.ico");
my $wm_class = new Win32::GUI::Class(
         -name => 'Limeng',
         -icon => $icon,
);

my $main = Win32::GUI::Window->new(
        -title => " Limeng's Clone Files Finder V3.0",
        -class => $wm_class,
        -size  => [$w_m,$h_m],
        -maximizebox        => 0,
        -dialogui        => 1,
        -noflicker        => 1,
        -onResize => \&mwResize,
);

$main->AddLabel(
        -name => 'L1',
        -pos  => [($w_m-190)/2,15],
        -size  => [$w_m,20],
        -text => "请将要查找的目录拖拽进下面的框!",
);

$main->AddButton(
        -name => 'B1',
        -pos  => [150,$h_m-70],
        -text => " 开 始 分 析 ",
        -default => 1,
        -ok      => 1,
        -visible => 1,
        -onClick => \&FindClone,
);

$main->AddButton(
        -name => 'B2',
        -pos  => [300,$h_m-70],
        -text => " 分 析 报 告 ",
        -default => 1,
        -ok      => 1,
        -visible => 1,
        -onClick => \&OpenReport,
);

$main->AddButton(
        -name => 'B3',
        -pos  => [450,$h_m-70],
        -text => " 关 于 程 序 ",
        -default => 1,
        -ok      => 1,
        -visible => 1,
        -onClick => \&About,
);

$main->AddListbox(
        -name => 'LB',
        -pos => [10,40],
        -vscroll => 1,
        -acceptfiles => 1,
        -onClick => \&changeTitle,
        -onDblClick => \&deleteSelect,
        -onDropFiles => \&gotDrop,
);

my ($DOS) = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($DOS);
$main->B2->Disable();
$main->Center();
#$main->Maximize();
$main->Show();
Win32::GUI::Dialog();
$main->Hide();
undef $main;
exit(0);

sub gotDrop {
        my ($self, $dropObj) = @_;
        my @files = $dropObj->GetDroppedFiles();
        foreach (sort @files) {
                 my $full_name = encode('gbk',$_);
                 if (-d $full_name) {
                          $self->Add($full_name) if (!$paths{$full_name});
                          $paths{$full_name} = 1;
                 }
        }
        return 0;
}

sub deleteSelect {
         my $self = shift;
         my $item = $self->GetText($self->GetCaretIndex());
         delete($paths{$item});
        $self->RemoveItem($self->GetCaretIndex());
        return 0;
}
sub changeTitle {
         my $self = shift;
         $main->L1->Change(-text => '双击选中的项,可将其从列表中删除!');
        return 0;
}

sub mwResize {
        my $self = shift;
        $self->LB->Resize($self->ScaleWidth()-20,280);
        return 1;
}

sub About {
        my $self = shift;
        $self->MessageBox("$about","关于本程序...",MB_ICONINFORMATION | MB_OK,);
        return 0;
}

sub OpenReport {
        my $self = shift;
        $main->B1->Change(-text => " 开 始 分 析 ");
        $main->B1->Enable();
        if (-e $clone_report_file) { system("$clone_report_file"); }
         else {
                 $self->MessageBox("指定的目录未发现重复文件!","分析报告...",MB_ICONINFORMATION | MB_OK,);
         }
        return 0;
}

sub FindClone {
        my $self = shift;
         my $time = &get_time;
        my $error_file = "错误信息报告_$time.txt";
        $clone_report_file = "重复文件报告_$time.txt";
        my $TT0 = new Benchmark;
        $main->B1->Disable();
        $main->B2->Disable();
        my @error;

         #获得目录清单
        my @path_list;
         my $list = $main->LB->GetCount();
         foreach (0 .. $list-1) {
                 push(@path_list,$main->LB->GetText($_));
         }

        # scan files from the given directories
         $main->B1->Change(-text => '扫描目录...');
        @file_list = ();
        foreach (@path_list) { readsub($_); }

        # search files with the same size
        my $n = 1;
        my %same_size;
        foreach my $file(@file_list) {
                 my $percent = int($n/($#file_list+1)*100);
                 $main->B1->Change(-text => "分析中...$percent %");
                 my @stat = stat($file);
                 push(@{$same_size{$stat[7]}},$file);
                 $n++;
        }
        my @files_with_same_size;
        foreach (keys %same_size) {
                 if (@{$same_size{$_}} > 1) {
                         push(@files_with_same_size,@{$same_size{$_}});
                 }
        }

        my $clone;
        if (@files_with_same_size) {
                 my $same_size_total = $#files_with_same_size+1;

                 # Compare files with the same size
                 my $n = 1;
                 my %clone;
                 foreach my $file(@files_with_same_size) {
                          my $percent = int($n/($same_size_total+1)*100);
                           $main->B1->Change(-text => "比较中...$percent %");
                          open(FILE, $file) or push(@error,"$file\t$!");
                          binmode(FILE);
                          my $md5 = Digest::MD5->new->addfile(*FILE)->hexdigest();
                          close FILE;
                          push(@{$clone{$md5}},$file);
                          $n++;
                 }

                 # generate clone report
                  my $check = 0;
                  foreach (keys %clone) {
                          if (@{$clone{$_}} > 1) {
                                 $check++;
                                 last;
                         }
                  }

                  if ($check) {
                          open FH, ">$clone_report_file";
                          print FH "已分析目录:\n";
                          foreach (@path_list) { print FH "$_\n"; }
                           print FH "\n";
                          print FH "创建时间        \t大小\t文件名\n";
                          print FH "="x100,"\n";
                          foreach (keys %clone) {
                                   if (@{$clone{$_}} > 1) {
                                           foreach(@{$clone{$_}}) {
                                                    my @stat = stat($_);
                                                    print FH time_transfer($stat[9]),"\t",int($stat[7]/1024+0.5)," KB\t$_\n";
                                           }
                                           print FH "\n";
                                           $clone++;
                                   }
                          }
                  }
        }
        $main->B1->Change(-text => " 分 析 结 束 ");

         # return analyze message
         my $message;
        if ($clone) {
                 $message = "共发现 $clone 组重复文件,详情请点击\"分析报告\"!";
                 print FH "共发现 $clone 组重复文件!";
                 close FH;
                 $main->B2->Enable();
        }
        else {
                $message = "指定的目录未发现重复文件!";
                 $main->B1->Enable();
                 $main->B2->Disable();
        }

        if (@error) {
                 open FH,">$error_file";
                 foreach (@error) { print FH "$_\n"; }
                 close FH;
                  my $errors = $#error+1;
                 $message .= "\n\n发生了 $errors 个错误,详见 \"$error_file\"!";
        }

        # display the time spent
        my $TT1 = new Benchmark;
        my $td = Benchmark::timediff($TT1, $TT0);
        $td = Benchmark::timestr($td);
        my ($sec) = ($td =~ /(\d+).*/);
        my $time_display;
        if ($sec >= 60) {
                 my $hour;
                 my $minute = int($sec/60);
                 if ($minute >= 60) {
                         $hour = int($minute/60);
                         $minute = $minute % 60;
                 }
                 my $second = $sec % 60;
                 $time_display .= " $hour 小时" if ($hour);
                 $time_display .= " $minute 分钟" if ($minute);
                 $time_display .= " $second 秒" if ($second);
        }
        else { $time_display = " $sec 秒"; }
        $message .= "\n\n分析耗时:$time_display";
        $self->MessageBox("$message","分析报告...",MB_ICONINFORMATION | MB_OK,);
        return 0;
}

sub readsub
{
        my $file_t = shift;
        if (-f $file_t) {
                push(@file_list,$file_t);
        }
        if (-d $file_t) {
                opendir(AA,$file_t);
                my @list = readdir(AA);
                close (AA);
                my $file_to_act;
                foreach $file_to_act (sort @list)
                {
                        if ($file_to_act =~ /^\.|\.$/) { next; }
                        else
                        {
                                readsub("$file_t/$file_to_act");
                        }
                }
        }
}

sub get_time {
        my ($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday) = (localtime(time()));
        #$sec = ($sec < 10)? "0$sec":$sec;
        $min = ($min < 10)? "0$min":$min;
        $hour = ($hour < 10)? "0$hour":$hour;
        $day = ($day < 10)? "0$day":$day;
        $mon = ($mon < 9)? "0".($mon+1):($mon+1);
        $year += 1900;
        return("$year-$mon-$day $hour.$min");
}

sub time_transfer {
        my $time_str = shift;
        my ($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday) = (localtime($time_str));
        #$sec = ($sec < 10)? "0$sec":$sec;
        $min = ($min < 10)? "0$min":$min;
        $hour = ($hour < 10)? "0$hour":$hour;
        $day = ($day < 10)? "0$day":$day;
        $mon = ($mon < 9)? "0".($mon+1):($mon+1);
        $year += 1900;
        return("$year-$mon-$day $hour:$min");
}

捕获.JPG (50.71 KB, 下载次数: 35)

捕获.JPG

论坛徽章:
0
2 [报告]
发表于 2014-06-18 15:28 |只看该作者
换换你源码的文件编码试试

论坛徽章:
0
3 [报告]
发表于 2014-07-07 09:14 |只看该作者
换为ANSI一切正常,谢谢回复 2# tmp


   

论坛徽章:
0
4 [报告]
发表于 2014-07-10 10:15 |只看该作者
这是我的代码啊

论坛徽章:
0
5 [报告]
发表于 2014-07-10 10:17 |只看该作者
是的,谢谢你
回复 4# iamlimeng


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP