Draw on Screen in a Panel outside the OnPaint-Method

C

Carsten Marx

Hello,
is there a chance to draw in the CF directly on a Panel some lines and
rectangles outside the Onpaint-Method?
When i try to get a Graphics Object:
Graphics g = this.CreateGraphics()
i get an NotSupportedException


Regards
Carsten Marx
 
P

Paul G. Tobey [eMVP]

Why would you want to do that? Not a good idea... As far as
CreateGraphics(), you can search the newsgroup archives and you'll find some
information on what classes support that and what classes don't.

Paul T.
 
C

Carsten Marx

Paul said:
Why would you want to do that? Not a good idea... As far as
CreateGraphics(), you can search the newsgroup archives and you'll find some
information on what classes support that and what classes don't.

For organizing active and not active part of a complex visulisation it
is nice to create some Panles and put the related stuff on them. Then i
have to do calculation on data, after that i put some panels dynamically
on the panel (the panels are representing cluster of items) and to
create nice visual effects it would be great to draw some not-standard
rectangles with 3D-borders under that clusters to make "visual borders"
between different categories.....
these panels are generated in a own method with some tricky calculations
and now it's not very simple to get the rectangles at the right position ..
But i think the only way is thats i put all my stuff directly on a Form.
.... :-(

Regards
Carsten
 
P

Paul G. Tobey [eMVP]

Well, drawing other shapes is just fine; it's when you're drawing them that
is a problem. You can inherit from Control and draw pretty much whatever
you want, as I recall.

Paul T.
 
S

Sergey Bogdanov

You may use an existent one instead of creating new. See this code
snippet how to draw the border around a panel:

protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);

System.Drawing.Pen myPen = new System.Drawing.Pen(borderColor);
int w = this.ClientRectangle.Width - 2;
int h = this.ClientRectangle.Height - 2;
pe.Graphics.DrawRectangle(myPen, 1, 1, w , h );
myPen.Dispose();
}




Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
S

Sergey Bogdanov

.... sorry, just noticed that overlooked the last part of the question -
"outside the Onpaint-Method".
 
J

jmghost

Hi Carsten,

I agree with Paul. There is probably a better way to accomplish what
your trying, but you can draw directly to a control by getting a hwnd
to your control, getting a dc from your hwnd then pinvoking some gdi
functions and drawing to your dc.

Kind Regards,
Jerron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top