Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
the following code will draw a line from top left to bottom right in win32gui and TK, the condition is that i must draw it once only ie executing the plotting code once only.
win32gui :
use strict; use warnings; use Win32::GUI(); our $NumberOfLinesDrawn = 0; my $Win = new Win32::GUI::Window( -left => 0, -top => 0, -width => 500, -height => 400, -name => "Window", -text => "drawing a line", ##-onPaint => \&Draw, ); $Win->Show(); Draw(); Win32::GUI::Dialog(); sub Window_Terminate { return -1; } sub Draw { my $DC = $Win->GetDC; my $x = 0; my $y = 0; for(1..300) {$y +=1; $x +=1; $DC->SetPixel($x,$y); } ##$DC->Validate(); $NumberOfLinesDrawn +=1; print "$NumberOfLinesDrawn\n"; }
so in win32gui how to keep the plot from been erased when covered by other windows !!. the usual plan for this is to redraw the same line code again and again if you uncomment the lines:
-onPaint => \&Draw ##and $DC->Validate();
but i think this is not the same way other modules or languages deals with updating the window ie by updating the pixels of the screen, in comparison the same TK code is concise and working well:
use warnings; use strict; use Tk; my $mw = MainWindow->new; my $x = 0; my $y = 0; my $counter=0; our $NumberOfLinesDrawn = 0; my $c = $mw->Canvas(-width => 500, -height => 500); $c ->pack; for(1..300) {$y +=1; $x +=1; $c -> createText( $x, $y, -fill => 'red', -text => '.'); } $mw->update; $NumberOfLinesDrawn +=1; print "$NumberOfLinesDrawn\n"; MainLoop;

Replies are listed 'Best First'.
Re: updating the window in Win32 GUI
by BrowserUk (Patriarch) on Jan 11, 2010 at 14:38 UTC

    Win32::GUI operates at a much lower level than Tk. Hence the programmer is charged with doing much more of the housekeeping with the former, than with the latter.

    For simple things like text windows and gui controls, re-drawing the contents each time a portion of the window is exposed is a reasonable approach, but for complex graphics, repeatedly re-drawing the entire window each time gets costly. The first response to this is to confine drawing to the update region, but even this can be too costly for complex (or slow to calculate) graphics.

    The next step is to not draw directly to the window during the paint message, but rather create an off-screen buffer (device context) compatible with the window and draw into that. Then when paint messages are received, blit the appropriate portion (update region) of the off-screen DC into the window's DC.

    However, unless things have moved along markedly since I last looked at Win32::GUI, much of the underlying system API required to implement this schema, has never been exposed via the module. (And again, unless things have improved substantially, even that functionality that has been implemented is often scarsely documented, which makes working out how to do things quite hard.)

    Finally, I have my doubts whether Win32::GUI and Perl is an appropriate toolset for drawing complex graphics. I'd guess that the intent of the module was mostly to allow access to the pre-defined control types, not impement complex graphics.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: updating the window in Win32 GUI
by zentara (Cardinal) on Jan 11, 2010 at 16:20 UTC
    A similar idea involving the amount of low-level housekeeping needed, can be seen in the Gtk2::DrawingArea. See Drawing with Perl/Gtk2 (the code needs some cleanup but works) and compare the DrawingArea to the Gnome2Canvas, and/or GooCanvas. Compare in how they maintain persistence of screen elements, and the extra housekeeping required. The same comparison goes for comparing SDL to Tk or Gtk2.... the lower level toolkit of SDL is faster but requires more house keeping.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku