Re: Painting Over Controls
I'm reactivating this thread because i have the same problem.
I have some controls on a form. And i want to paint some lines *over* the controls.
If i use this.CreateGraphics, everything is drawn behind those controls. How can it be resolved?
Re: Painting Over Controls
okay both of you need to bow down in front of me:D (jk hehe )
I feel l33t. What you need to do is to create your graphics object using the API I'm using.
this.Handle refers to the window handle of the form. You give the window handle to the GetWindowDC and it returns a device context that can be used to draw over the window. Using CreateGraphics() would do the same as calling the GetDC() api with the window handle
Code:
// you need this namespace to use dllImport
// using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr dc = GetWindowDC (this.Handle);
Graphics gr = Graphics.FromHdc (dc);
gr.DrawLine (Pens.Red, new Point(0,0), new Point (this.Right, this.Bottom));
gr.Dispose();
}
the actual code's inside a button click.
So basically create your graphics object like that and the rest is that same
Re: Painting Over Controls
umm there is a ReleaseDC api also. I'm no API expert (have very minimal experience actually:D ) but I "think" that using gr.Dispose() releases it.
you might want to double check on that
Re: Painting Over Controls
yes It does release it even I think after testing that code...
But,Mr.Polite,This also only draws a line over the form...and not over the screen in windows ..
So,Is there any API that can help drawing a line over the whole screen?
Re: Painting Over Controls
yeah the GetDC api called with a 0 param
ie (haven't tested this, but it should work)
Code:
using System.Runtime.Interopservices...
[DllImport("user32")]
public static extern IntPtr GetDC(IntPtr hwnd)
// retrieve the desktop handle
IntPtr desktopDC = GetDC(IntPtr.Zero);
then use the DC the same as you used it above :)
Re: Painting Over Controls
Yikes,This is crazyyyy..
Awesomeee..Amazinggg..woww
I am just drawing around on my desktop and everywhere on the screen..
This is just crazy Mr Polite..
You're wayy too cool.
Thanks :D
Re: Painting Over Controls
hehehehe
you're welcome :D
if you really care about doing it properly, you might wanna drop a post in the api forum and ask them if calling GetDC(0) is the proper way of retreiving the dektop handle, and also you might want to use ReleaseDC when you're done with your drawing....
won't matter much if you're just messing around though hehehe
Re: Painting Over Controls
It gives me System.OutofMemory exception after a few minutes and my computer becomes slow too :ehh:
Re: Painting Over Controls
Oh yes Mr Polite,Ill do that :)
Re: Painting Over Controls
Quote:
Originally Posted by uniquegodwin
It gives me System.OutofMemory exception after a few minutes and my computer becomes slow too :ehh:
lol wow
:D
try calling GetDC once and then saving the value.... use the saved value in your paint event. That might make things smoother also
Re: Painting Over Controls
oh,Roger that...
Ill do it now :)