At 21:40 2006-01-17, Brian Millham wrote:
The VB portion shows a "Rolling Line Graph" of data. Is there an
easy way to do this in Perl and Win32::GUI? I'm currently using MSCHART.OCX
to make the graph, but I'd like something that I have more control over.
You can paint a graph yourself on a Graphic control.
I posted this a few years back as an example of how to paint stuff on a
Graphic:
----------
This is one way. Consider it a proof-of-concept rather than best-practice
given the load of globals.
Basically, the _Paint event handler is used to always plot first the mouse
lines and then the actual graph. This happens when the the Graphic control
is invalidated, which can be triggered by three things; either Windows
itself notices the need for it, or the graph changes, or the mouse moves.
(This method decouples the cause of the event from the action to be
performed when it happens, which is a Good Thing)
The #### comments explain the important concepts.
#!/usr/local/bin/perl -w
use strict;
use Win32::GUI;
my $wWinPlot = 400;
my $hWinPlot = 250;
my ($xMouse, $yMouse) = (100, 100);
my $xBar = 100;
my $winPlot = winCreate();
sub winCreate {
my $winPlot = Win32::GUI::Window->new(
-left => 100,
-top => 100,
-width => $wWinPlot,
-height => $hWinPlot,
-name => "winPlot",
-text => "Plot test",
-minheight => 10,
-minwidth => 10,
);
my $grCanvas = Win32::GUI::Graphic->new($winPlot,
-left => 0,
-top => 0,
-width => $wWinPlot,
-height => $hWinPlot,
-name => "grCanvas",
-interactive => 1,
);
my $tim = $winPlot->AddTimer("timTimer", 100);
$winPlot->Show();
return($winPlot);
}
#### Will be called when the window needs to be painted,
#### caused either by Windows, or the InvalidateRect of the entire window
sub grCanvas_Paint { my $win = $winPlot;
my($dcDev) = @_;
return(0) if(!$dcDev);
$dcDev->TextColor([0, 0, 0]); #Black
#The cross-hair
$dcDev->Rectangle($yMouse, 0, $yMouse + 1, $hWinPlot);
$dcDev->Rectangle(0, $xMouse, $wWinPlot, $xMouse + 1);
#The bar
$dcDev->Rectangle(50, 0, 120, $xBar);
#### Tell Windows we're satisfied with the way the area looks like
$dcDev->Validate();
return(1);
}
#### Trigger on the mouse move
sub grCanvas_MouseMove { my $win = $winPlot;
my ($dummy, @aPos) = @_;
($yMouse, $xMouse) = @aPos;
#### Tell Windows the window needs to be repainted,
#### will call the _Paint event handler
#### The 1 wipes the area before repainting it,
#### changing it to 0 will leave trails
$win->InvalidateRect(1);
return(1);
}
my $timerCount = 0;
sub timTimer_Timer { my $win = $winPlot;
$timerCount += .1;
$xBar = (int(cos($timerCount) * 50)) + 70;
#### Tell Windows the window needs to be repainted,
#### will call the _Paint event handler
#### The 1 wipes the area before repainting it,
#### but since we may know exactly what needs to be repainted
#### when $xBar changed, that may not be necessary
$win->InvalidateRect(1);
return(1);
}
Win32::GUI::Dialog();
__END__
/J