GDI programming question (from a newbie)

J

Jeremy Bradshaw

I'm trying to paint rectangles using GDI (I'm implementing drag and drop and
want to paint a dashed rectangle as I drag a control to a new position). I
instantiate a Rectangle object ok and handle its painting in the form's
Paint event. This works fine, but the rectangle always appears "underneath"
other objects on the form - I want it to always appear over the top of them.
How can I achieve this ?
 
G

Gerben van Loon

maybe it's better to ask your question in
microsoft.public.dotnet.framework.drawing

greets
 
B

Bob Powell [MVP]

Probably the easiest is to import the GetDC method, get the desktop DC and
then get a Graphics object for it so you can draw on that using
ControlPaint.

using System.Runtime.InteropServices;


[DllImport("user32.dll")]
static extern System.IntPtr GetDC(System.IntPtr hWnd);

[DllImport("user32.dll")]
static extern int ReleaseDC(System.IntPtr hWnd, System.IntPtr hDC);

//in the draw routine...

System.IntPtr hDCDeskTop=GetDC(System.IntPtr.Zero);

Graphics g1=Graphics.FromHdc(hDCDeskTop);

//draw on the desktop here....

g1.DrawLine(Pens.Red,0,0,1024,768);

g1.DrawLine(Pens.Red,0,768,1024,0);

//clean up

g1.Dispose();

ReleaseDC(System.IntPtr.Zero,hDCDeskTop);

--
Bob Powell [MVP]
C#, System.Drawing

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm

New Tips and Tricks include creating transparent controls
and how to do double buffering.
 

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