c# - How to paint additional things on a drawn panel? -


i'm reading lot c# drawing , reading msdn tutorial on gdi+ using graphics handlers.

i want able paint graph, nodes have in list can't use auto-placing, need nodes in specified place , have specific appearance , on, changing on time, that's why stopped looking graph libraries.

it works great when painted first time when want painted after else happens in code (not after clicking control), can't it. example:

if (somethinghappens) {   // repaint panel adding things } 

all got either nothing new happens or earlier painting disappears.

i found examples on onpaint overriding , drawings disappearing when minimalized. when need paint additional when happens in application, have override or different?

i looked q&a include information need. frankly, it's fundamental question, how deal drawing forms control. msdn topics overriding onpaint method , custom control painting , rendering provide detail on right way this. i'm surprised wasn't able find stackoverflow q&a @ least addresses basic question directly (there many address tangentially of course).

anyway…

this basic sequence drawing in forms code:

  1. generate data drawn
  2. invalidate control data drawn
  3. handle paint event drawing data

repeat above necessary, i.e. time data changes (such "something happens", in question) move step #1, adding whatever new data want existing collection of data, invalidating control, , responding paint event in event handler.

in case of drawing graph, might this:

private list<point> _points = new list<point>();  void addpoint(point point) {     _points.add(point);     panel1.invalidate(); }  void panel1_paint(object sender, painteventargs e) {     if (_points.count < 2)     {         return;     }      point previouspoint = _points[0];      (int = 1; < _points.count; i++)     {         currentpoint = _points[i];          e.graphics.drawline(pens.black, previouspoint, currentpoint);         previouspoint = currentpoint;     } } 

note panel1_paint() event event handler. create via designer selecting panel object, switching "events" list in "properties" window control, , double-clicking in edit field paint event.

that of course simplest example. can add things updating data in batched manner (i.e. don't invalidate control until you've added group of points), use different colors or line styles draw, draw other elements of graph axes, ticks, legend, etc. above meant illustrate basic technique.

one final note: depending on how many points in graph need draw, above may or may not fast enough. should fine thousands of points or so, if start getting tens or hundreds of thousands or more, you'll find it's useful cache drawing bitmap , draw bitmap. that's whole separate question. first, need make sure understand forms drawing model , using correctly.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -