Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Win32 Multiple Clipboards II

by Chady (Priest)
on Jun 06, 2004 at 18:41 UTC ( #361795=sourcecode: print w/ replies, xml ) Need Help??

Category: Win32 Stuff
Author/Contact Info Chady

Win32 Multiple Clipboards was implemented in a really silly way. I don't know how I missed the obvious. Here's a different implementation that eats a lot less memory, and doesn't use threads and sharing etc...

In fact, what I missed last time was the timer. And since there's no readable documentation for Win32::GUI I came across this by a combination of poking thourgh and luck. It worked out pretty well, and now I'm actually using this.

You will probably need Win32::GUI cause it's not with default ActiveState's installation, otherwise, this should work right out of the box.


Comments and suggestions please.


# copyright (c) Chady Kassouf 2004
# This program is free software, it is distributed under 
# the sames terms as Perl itself.

use strict;

use Win32::Clipboard;
use Win32::GUI;

my $empty = '--empty--';        # the empty string
# my $image = '[BITMAP]';
my $DEBUG = 1;                # set to 1 for debugging info.

my @boxes;                # holds the radiobuttons
my $CLIP;                # tied to the clipboard
my @board;                # holds the clipboard entries.

push @board, ['', $empty] for 0 .. 4;
tie $CLIP, 'Win32::Clipboard';

# GUI creation.

my $main = Win32::GUI::Window->new(
    -name    =>    'main',
    -text    =>    'Win32 Multiple Clipboards II',
    -width    =>    200,
    -height    =>    150

my $icon = Win32::GUI::Icon->new('Note.ico');
my $tray = $main->AddNotifyIcon(
    -name    =>    'tray',
    -icon    =>    $icon,
    -tip    =>    'Win32 Multiple Clipboards II'

# I found this handled nicely, 
# this isn't a busy loop, so no high CPU usage here
my $timer = $main->AddTimer('timer', 100);

foreach (0 .. 4) {
    $boxes[$_] = $main->AddRadioButton(
        -name => "board$_",
                -text => $board[$_]->[1],
                -pos  => [ 10, 10 + 20 * $_ ],
    $boxes[$_]->{-width} = 150;



sub timer_Timer {

    my $label;
    my $data;
    if (tied($CLIP)->IsBitmap()) {
        # I tried to handle bitmap data, but it 
        # gets ruined while copied back and forth;
        # when set back, the data become text data.
        debug('Bitmap data, skipping');
        # $label = $image;
        # $data = tied($CLIP)->GetBitmap();
    } else {
        $label = $CLIP;
        $label =~ s/^(.{20})(.*)$/$1... /;
        $data = $CLIP;

    return if $data eq $board[0]->[0];
    debug ('Got new clipboard.');

    unshift @board, [$data, $label];
    pop @board if @board == 4;


sub redraw {
    for (0..4) {
        $boxes[$_]->{-text} = $board[$_]->[1];

sub set_clip {
    my $which = shift;

    if ($which) {
        if ($board[$which]->[1] eq $empty) {
            debug('empty slot.');
        } else {
            debug('Setting clip to: ', $board[$which]->[1]);
            $CLIP = $board[$which]->[0];

# everything I tried doesn't work here.
sub board0_Click {set_clip(1);1;}
sub board1_Click {set_clip(2);1;}
sub board2_Click {set_clip(3);1;}
sub board3_Click {set_clip(4);1;}
sub board4_Click {set_clip(5);1;}

sub tray_Click {
    # move the window near the task bar
    my $desk = Win32::GUI::GetDesktopWindow();
    my $dw = Win32::GUI::Width($desk);
    my $dh = Win32::GUI::Height($desk);
    my $x = $dw - $main->Width();
    # the taskbar is 27 pixels on Xp (no Luna)
    my $y = $dh - $main->Height() - 27;
    $main->Move($x, $y);

sub main_Minimize {


sub main_Terminate {

sub debug {
    print @_, "\n" if $DEBUG;

Comment on Win32 Multiple Clipboards II
Download Code

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://361795]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2015-11-28 04:11 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (737 votes), past polls