[Mono-list] System.Graphics and GTK# applications

Stifu stifu at free.fr
Mon Nov 15 15:08:00 EST 2010


I'm clueless about GTK#, but I know the Graphics object should not be cached,
and you should use the one supplied in the Paint event parameter for each
repaint. That's how it is with WinForms and GDI+, and it seems like it's the
same logic here.
So I think you're doing it just right, now.

Here's a link that says so without explaining anything. :)
http://bobpowelldotnet.blogspot.com/2005/03/does-there-have-to-be-reason.html

Another link: http://www.dotnet247.com/247reference/msgs/67/336551.aspx

"Firstly, and most important, it's a very bad idea to obtain the Graphics
object through GetGraphics and store it. Firstly the Graphics objects can
have DC's associated with them. DC's are limited resources so all the time
you have hold of one you're potentially locking a resource that should be
released immediately. You must only ever draw on the Graphics object passed
to you via the OnPaint or OnPaintBackground overrides and the Paint event.
The Graphics object passed will differ depending on whether the form or
control is double buffered. If it is, the Graphics object will refer to an
in-memory bitmap used as a backbuffer for the object. You must never draw on
any Graphics object when you're in a button click handler or indeed any
other hadler that wasn't initiated using the WM_PAINT or WM_PAINTBKGND
messages. This fundamentally breaks the event driven architecture and leaves
you prone to suffering the effects of the #1 most asked GDI+ FAQ question
(ie "Where did my graphics go???")"


fran wrote:
> 
> Hello,
> 
> I've found that if I create the Graphics.FromDrawable within the Expose
> event function, it works as expected. I.e. move this code:
> 
> g = Gtk.DotNet.Graphics.FromDrawable (drawingarea1.GdkWindow);
> 
>>From MainWindow constructor to Redraw function (that's called on
> ExposeEvent of drawing area).
> 
> Two misteries remain undisclosed for me anyway:
> 
> First is why the graphics instance must be recreated in each expose
> event? and second, probably related with the first, is why when Graphics
> instance is get just one time on MainWindow constructor I can see the
> rectagle flickering from time to time while I change the window size...
> 
> Who knows... O_o
> 
> Anyway having it working is enough for now for me.
> 
> Thanks a lot everyone,
> 
> 
> El 14/11/10 14:50, Francisco M. Marzoa escribió:
>> Hello,
>>
>> Thanks for your help, Dimitar.
>>
>> This wants to work, but it didn't fully. It seem that I can draw an
>> square on a DrawingArea, but it's erased just after drawing it. :-?
>>
>> Please, take a look at this code:
>>
>> using System;
>> using Gtk;
>> using Gtk.DotNet;
>> using System.Drawing;
>>
>> public partial class MainWindow : Gtk.Window
>> {
>>     System.Drawing.Graphics g;
>>        
>>     public MainWindow () : base(Gtk.WindowType.Toplevel)
>>     {
>>         Build ();
>>         g = Gtk.DotNet.Graphics.FromDrawable (drawingarea1.GdkWindow);
>>     }
>>
>>     protected void OnDeleteEvent (object sender, DeleteEventArgs a)
>>     {
>>         Application.Quit ();
>>         a.RetVal = true;
>>     }
>>    
>>     protected virtual void Redraw (object o, Gtk.ExposeEventArgs args)
>>     {
>>         Pen p = new Pen (Color.Red);
>>         g.DrawRectangle (p, 10, 10, 100, 100);
>>     }
>>    
>>    
>> }
>>
>> Is a GTK# solution I've created with Monodevelop: it has just a
>> MainWindow, within which there's a VBox container with just one row,
>> within which there's a DrawingArea named as default "drawingarea1".
>>
>> Also the Redraw method is connected to the Expose signal on
>> drawingarea1, so it's redrawn everytime the MainWindow resizes (Miguel
>> de Icaza told me to use that event for this task in a previous message.
>> Thanks, Miguel).
>>
>> Now, when I run the application there's nothing drawn on the window, but
>> if I drag a corner of the mainwindow resizing it continously, I can see
>> from time to time a flickering red square within, that dissapears when I
>> stop to drag the corner.
>>
>> So it seems like the rectangle where drawn, but it's "erased" just after
>> drawn.
>>
>> Do you know where the problem may be??
>>
>> Thanks a million in advance,
>>
>>
>> El 13/11/10 18:47, Dimitar Dobrev escribió:
>>   
>>> Add a reference to gtkdotnet and use Graphics.FormDrawable, which
>>> returns a
>>> Graphics object for drawing on the widget with the specified GdkWindow.
>>> This
>>> works on Windows and Linux but not on Mac OS X. I'm looking for a way to
>>> use
>>> it on the latter as well.
>>>   
>>>     
>> _______________________________________________
>> Mono-list maillist  -  Mono-list at lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/mono-list
>>
>>   
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
> 

-- 
View this message in context: http://mono.1490590.n4.nabble.com/System-Graphics-and-GTK-applications-tp3040851p3043703.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list