PDA

Click to See Complete Forum and Search --> : Painting Over Controls


Bitem2k
Apr 11th, 2005, 05:24 PM
Does anyone know of a way to paint a rectangle similar to the type thats drawn when you reposition a tool window in windows.

My problem is that you need to know which graphics context to draw it on, and what do you do if it spans multiple hdc's?

thanks

monstrosity
Sep 7th, 2005, 10:24 AM
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?

MrPolite
Sep 7th, 2005, 02:57 PM
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

// 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

MrPolite
Sep 7th, 2005, 03:02 PM
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

uniquegodwin
Sep 11th, 2005, 04:18 AM
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?

MrPolite
Sep 11th, 2005, 06:02 PM
yeah the GetDC api called with a 0 param
ie (haven't tested this, but it should work)


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 :)

uniquegodwin
Sep 12th, 2005, 01:37 AM
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

MrPolite
Sep 12th, 2005, 01:47 AM
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

uniquegodwin
Sep 12th, 2005, 01:48 AM
It gives me System.OutofMemory exception after a few minutes and my computer becomes slow too :ehh:

uniquegodwin
Sep 12th, 2005, 01:49 AM
Oh yes Mr Polite,Ill do that :)

MrPolite
Sep 12th, 2005, 01:54 AM
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

uniquegodwin
Sep 12th, 2005, 01:55 AM
oh,Roger that...
Ill do it now :)