【问题标题】:How do I read a text file to display in richedit and show a message when this succeeds?如何读取文本文件以在 Richedit 中显示并在成功时显示消息?
【发布时间】:2011-01-16 15:08:54
【问题描述】:

下面是我的 Perl 代码。我需要帮助:

  1. 如何在加载包含 100 行名称列表的文本文件时将输出显示到 Richedit。

  2. 在 Richedit 中显示输出后,会弹出一个小消息窗口(弹出窗口)并告诉我“您的姓名列表已成功加载”。


use Win32::GUI;

$Win = new Win32::GUI::Window(
    -left   => 341,
    -top    => 218,
    -width  => 300,
    -height => 300,
    -name   => "Win",
    -text   => "Open File Read and Display Popup"
);

$Win->Show();

$Win->AddTextfield(
    -text   => "Load Your File TXT Here",
    -name   => "Textfield_1",
    -left   => 4,
    -top    => 24,
    -width  => 278,
    -height => 20,
);

$Win->AddButton(
    -text       => "Click Here To Load",
    -name       => "Button_1",
    -left       => 6,
    -top        => 48,
    -width      => 275,
    -height     => 21,
    -foreground => 0,
    -onClick    => \&Load1,

);

$Win->AddRichEdit(
    -text   => "",
    -name   => "RichEdit_1",
    -left   => 5,
    -top    => 72,
    -width  => 275,
    -height => 169,
);

$Win->AddStatusBar(
    -text   => "",
    -name   => "StatusBar_1",
    -left   => 0,
    -top    => 248,
    -width  => 290,
    -height => 17,
);

Win32::GUI::Dialog();

sub Win_Terminate {
    return -1;
}

sub Load1 {

    my $file1 = Win32::GUI::GetOpenFileName(
        -owner => $Win,                   # Main window for modal dialog
        -title => "Load Name List ...",

        # Dialog title
        -filter => [                      # Filter file
            'Name List(*.txt)' => '*.txt',
            'All files'        => '*.*',
        ],
        -directory => ".",                # Use current directory
    );

    # Have select a file ?
    if ($file1) {

        # Load file to animation control
        $Win->Textfield_1->Text(
            $file1);
    }

    # Or an error messagebox with error.
    elsif (Win32::GUI::CommDlgExtendedError()) {
        Win32::GUI::MessageBox(0, "ERROR : " . Win32::GUI::CommDlgExtendedError(), "GetOpenFileName Error");
    }
    $Win->{Button_1}->Text('Edit');
    $file1 = 1;
}

【问题讨论】:

    标签: perl winapi user-interface


    【解决方案1】:

    您可以使用其Text 方法设置richedit 的文本。在Load1 处理程序中添加以下内容以显示名称列表:

    my $data = do { local $/; open my $in,"<",$file1 or die "$file1: $!"; <$in> };
    $Win->RichEdit_1->Text($data);
    

    可以用Win32::GUI::MessageBox函数显示消息框:

    Win32::GUI::MessageBox($Win,"A message","App Title",MB_ICONINFORMATION);
    

    【讨论】:

      【解决方案2】:

      完整版答案

      use Win32::GUI;
      
      $Win = new Win32::GUI::Window(
          -left   => 341,
          -top    => 218,
          -width  => 300,
          -height => 300,
          -name   => "Win",
          -text   => "Open File Read and Display Popup"
      );
      
      $Win->Show();
      
      $Win->AddTextfield(
          -text   => "Load Your File TXT Here",
          -name   => "Textfield_1",
          -left   => 4,
          -top    => 24,
          -width  => 278,
          -height => 20,
          -prompt => "Input birthday:",
      );
      $Win->Textfield_1->SetFocus();
      
      $Win->AddButton(
          -text       => "Click Here To Load",
          -name       => "Button_1",
          -left       => 6,
          -top        => 48,
          -width      => 275,
          -height     => 21,
          -foreground => 0,
          -onClick    => \&Load1,
      
      );
      
      $Win->AddRichEdit(
          -text   => "",
          -name   => "RichEdit_1",
          -left   => 5,
          -top    => 72,
          -width  => 275,
          -height => 169,
      );
      
      $Win->AddStatusBar(
          -text   => "",
          -name   => "StatusBar_1",
          -left   => 0,
          -top    => 248,
          -width  => 290,
          -height => 17,
      );
      
      Win32::GUI::Dialog();
      
      sub Win_Terminate {
          return -1;
      }
      
      sub Load1 {
      
          my $file1 = Win32::GUI::GetOpenFileName(
              -owner => $Win,                   # Main window for modal dialog
              -title => "Load Name List ...",
      
              # Dialog title
              -filter => [                      # Filter file
                  'Name List(*.txt)' => '*.txt',
                  'All files'        => '*.*',
              ],
              -directory => ".",                # Use current directory
          );
      
          # Have select a file ?
          if ($file1) {
      
              # Load file to animation control
              $Win->Textfield_1->Text($file1);
              my $data = do {
                  local $/;
                  open my $in, "<", $file1 or die "$file1: $!";
                  <$in>
              };
              $Win->RichEdit_1->Text($data);
              Win32::GUI::MessageBox($Win, "A message", "App Title", MB_ICONINFORMATION);
          }
      
          # Or an error messagebox with error.
          elsif (Win32::GUI::CommDlgExtendedError()) {
              Win32::GUI::MessageBox(0, "ERROR : " . Win32::GUI::CommDlgExtendedError(), "GetOpenFileName Error");
          }
          $Win->{Button_1}->Text('Edit');
          $file1 = 1;
      
      }
      

      如何用进度条更改Win32::GUI::MessageBox($Win,"A message","App Title",MB_ICONINFORMATION);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-28
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多