in reply to Tk MListbox widget and Win32 clipboard
If you don't care about platform portability, you could try the Win32::GUI module instead of using TK, as well as the Win32::Clipboard module.
Here is some sample code to demonstrate how to do what you are looking for using Win32::GUI and Win32::Clipboard:
#!perl use strict; use warnings; use Win32::Clipboard; use Win32::GUI qw(); my $clipboard = Win32::Clipboard->new(); # Create an accelerator table to map keyboard shortcuts to subs my $accel = Win32::GUI::AcceleratorTable->new( 'Ctrl-C' => \&CopyListItems, ); # Create the main window my $window = Win32::GUI::Window->new( -name => 'mainwindow', -text => 'Win32::GUI Listview Sample', -size => [320, 240], -accel => $accel, # Associate accelerator table with window ); # Create a listview control with the main window as the parent my $listview = $window->AddListView( -name => 'listview', -pos => [5, 5], -size => [$window->ScaleWidth() - 10, $window->ScaleHeight() - 10] +, -fullrowselect => 1, ); # Insert 2 columns $listview->InsertColumn( -text => 'Column 1', -width => $listview->ScaleWidth() / 2, ); $listview->InsertColumn( -text => 'Column 2', -width => $listview->ScaleWidth() / 2, ); # Insert some data $listview->InsertItem(-text => [1, 'a']); $listview->InsertItem(-text => [2, 'b']); $listview->InsertItem(-text => [3, 'c']); $listview->InsertItem(-text => [4, 'd']); $listview->InsertItem(-text => [5, 'e']); # Show main window $window->Show(); # This is our dialog loop Win32::GUI::Dialog(); # This is the sub called when user presses Ctrl-C sub CopyListItems { # Get a list of indices of selected items my @items = $listview->SelectedItems(); # Will return an empty list if none selected, so we just return return unless @items; # Empty the clipboard $clipboard->Empty(); my $text; # Loop though each item # The text copied to the clipboard has each selected row on a # new line, with each item seperated by a tab foreach(@items){ my @text; my $item = $listview->Item($_); push @text, $item->Text(); my $subitem = $item->SubItem(1); push @text, $subitem->Text(); $text .= sprintf qq(%s\n), join qq(\t), @text; } # Set the clipboard $clipboard->Set($text); return; } __END__
Update: Fixed links.
|
|---|