The problem with using the CreateGraphics call for any purpose other than
obtaining data about the Graphics object of a particular window is that it
breaks the fundamental principle of event driven programming.
Windows is an event driven system in which things happen in order and for a
purpose. When you write code that refuses to follow the order of events as
dictated by the system you will have your own misguided efforts walked upon
by the *correct functioning* of the system.
Painting using CreateGraphics will almost inevitably be overprinted by the
OnPaint method and causes huge numbers of questions from inexperienced
programmers who think that there is some sort of system error when it's
their own code that's at fault. This is why it's still the GDI+ FAQ most
asked question despite three years of the answer being available for all to
see with a simple google search.
It's also why I generally answer such questions with a brusque "RTFM" style
answer so, after you've "Read That Fine Material" ;-) you'll understand
fully.
Microsoft's operating systems, including Longhorn, will continue to be event
driven and will exhibit the same behaviours if event order is ignored
whether it's GDI, GDI+ or Avalon doing the drawing.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Dennis" <(E-Mail Removed)> wrote in message
news:75502B37-55A1-44C0-ACF3-(E-Mail Removed)...
> Bob, I've seen a lot of posts saying CreateGraphics is a bad Idea, mostly
> they say because of encapuslation and it doesn't work with double
> buffering.
> I've yet to really understand why it is a bad to use it so long as one
> doesn't use double buffering...is there some fear that this won't work in
> future releases of VB.Net and if so, why aren't we worried about several
> other things the probably will change in future releases? Why the
> CreateGraphics worry..do you have some insight as to what Microsoft will
> do?
>
> "Bob Powell [MVP]" wrote:
>
>> See the GDI+ FAQ for explanations of why CreateGraphics is a bad idea.
>>
>> --
>> Bob Powell [MVP]
>> Visual C#, System.Drawing
>>
>> Find great Windows Forms articles in Windows Forms Tips and Tricks
>> http://www.bobpowell.net/tipstricks.htm
>>
>> Answer those GDI+ questions with the GDI+ FAQ
>> http://www.bobpowell.net/faqmain.htm
>>
>> All new articles provide code in C# and VB.NET.
>> Subscribe to the RSS feeds provided and never miss a new article.
>>
>>
>>
>>
>>
>> "Ed Bitzer" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > Can draw a line on my form with a button click event but cannot upon
>> > the
>> > Load event when I wish. No more line object so used the following:
>> > Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height)
>> > Dim g As Graphics = Graphics.FromImage(bit)
>> > Dim myPen As Pen = New Pen(Color.Blue, 1)
>> > Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5)
>> > Appreciate some help.
>> >
>> > Ed
>> >
>>
>>
>>