c# - How to call Invalidate not for the whole panel from another event/class -



c# - How to call Invalidate not for the whole panel from another event/class -

i have paint event looks this:

private void panel1_paint(object sender, painteventargs e) { rectangle rec = new rectangle(2, 2, 820, 620); pen pi = new pen(color.black, 2); e.graphics.drawrectangle(pi, rec); rectangle rec2 = new rectangle(convert.toint32((410 + 2500 * globalevariablen.istwerte[0])), convert.toint32(310 + 1875 * globalevariablen.istwerte[1]), 2, 2); e.graphics.drawrectangle(pi,rec2); }

i have datastream serialport, , everytime receive info want invalidate rec2 not whole form. able invalidate whole form within datareceived event with:

panel1.invalidate();

however not know how can create happen invalidate rec2, because if invalidate whole form time datastream blinks crazy , not good.

invalidate() has overload version rectangle want invalidate:

panel1.invalidate(getrect2());

where getrect2() (please pick improve name) like:

static rectangle getrect2() { int x convert.toint32((410 + 2500 * globalevariablen.istwerte[0])); int y = convert.toint32(310 + 1875 * globalevariablen.istwerte[1]); homecoming new rectangle(x, y, 2, 2); }

in paint event handler have first check if invalidated part intersects each object want write (example simple because you're working rectangles , not have slow expansive filling).

what wound performance more in code fact you're creating new pen each paint operation. it's have absolutely avoid: expansive native resources must reused. final code may similar to:

private pen _pen = new pen(color.black, 2); private void panel1_paint(object sender, painteventargs e) { var rec = new rectangle(2, 2, 820, 620); if (e.cliprectangle.intersectswith(rec)) e.graphics.drawrectangle(_pen, rec); var rec2 = getrect2(); if (e.cliprectangle.intersectswith(rec2)) e.graphics.drawrectangle(pi, rec2); }

now code more optimized may still blink. avoid have enable double buffering panel. derive own class panel , add together in constructor:

setstyle(controlstyles.optimizeddoublebuffer, true);

it may chance refactor code , move paint logic in separate class (but not panel itself). please refer msdn other flags may need utilize (such allpaintinginwmpaint).

final note: hard-coded coordinates, it's not practice unless have fixed size panel (with or without scrolling) because won't scale future changes , may broken in many circumstances (as code become more complex fictional example).

c# panel invalidate

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -