Results 1 to 9 of 9

Thread: [2.0] Drawing lines/etc outside of a form's coords?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    [2.0] Drawing lines/etc outside of a form's coords?

    I'm wondering what's required to draw lines/etc outside of a form's coords.

    An easy example would be, let's say... drawing a ruler over the desktop (which is on top of every window), but without having to rig it with a full screen transparent form.

    Is this possible?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    No, that's not possible in C# or any .NET Language
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    Ah, bummer.

    I figured out a work around for what I want to do, but sadly .NET is such a pain to visually tweak things. It's getting ridiculous.

    I have this form, the form is 1 pixel wide and 5 pixels in height. I'm using the pen tool to paint a 1 pixel thick black line on the form.

    Then I did an override to OnPaint to set a borderless form to 1 width / 5 height (since the IDE doesn't let you create forms this small). All is well, except .NET decided it's necessary to create a solid dropshadow of the form's backcolor, extending past the width of the form.

    Result is, the line doesn't look like it's 1 pixel. If I change the back color of the form to the same color as the pen then it just looks like an ugly 2-3 pixel thick line. Retarded if you ask me.

    The effect can be gotten by using the transparency key, but I refuse to use that. There's no reason why .NET shouldn't be able to draw a 1 pixel thick form.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    You can use P/Invoke to grab the HDC of the desktop, and then create a Graphics object using Graphics.FromHDC.

    You would call CreateDC ("DISPLAY", 0, 0, 0, 0);

    use the return value as the argument to Graphics.FromHdc, and finally call DeleteDC() on the hdc when you're done with it.

    HTH!
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    Quote Originally Posted by ComputerJy
    No, that's not possible in C# or any .NET Language
    Incorrect. A couple of years ago i did a demo of drawing GDI+ to the full screen area. Its not that hard. Do a forum search and you'll find it.
    I don't live here any more.

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    I couldn't find anything too useful in the forum search (searched for CreateDC).

    I managed to figure it out, but I'm having issues here with redrawing. The line gets drawn but then it disappears whenever something goes over it.

    Btw, here's what I came up with after reading up on CreateDC():

    Code:
            private void DrawLineToScreen(Color color, float width, int x1, int y1, int x2, int y2)
            {
                IntPtr hDC = WindowsAPI.GDI.CreateDC("Display", null, null, IntPtr.Zero);
    
                Graphics g = Graphics.FromHdc(hDC);
    
                g.DrawLine(new Pen(color, width), x1, y1, x2, y2);
    
                WindowsAPI.GDI.DeleteDC(hDC);
                g.Dispose();
            }

  7. #7
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    Yes, this is a tricky problem. If you don't redraw the line, it won't be redrawn if the region it's inside of is invalidated.

    However, if you constantly redraw the line, you'll create an annoying flickering effect.

    The perfect solution is to only redraw your line when the desktop receives a WM_PAINT message which says that the region is invalidated. Unfortunantly, this is not an easy task. I assume you would have to use DLL injection to load some code into explorer.exe and then change the window procedure for the desktop so that you can intercept WM_PAINT messages.

    This is probably way too complicated to bother with.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    Yikes. I was hoping to hear "yeah, that's no problem, just set the graphics object to double buffer, did you not see the method in the intillisense?".

    Hmm, that seems like a bit much. Any ideas on how to remove that weird shadow on the form? I can't live with a 2 pixel line when it has to be 1 pixel.

    I'm a newbie to GDI. I guess I'll need to intercept some things and draw the form myself (?), but it's going to require some hand holding for me to figure it out.

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Drawing lines/etc outside of a form's coords?

    Ah, 3 hours of MSDN later...

    Seems I simply have to set the Region of the form. Funky shadow is gone, and the form is truly 1x5.

    Code:
            protected override void OnPaint(PaintEventArgs e)
            {
                Rectangle rc = new Rectangle();
    
                rc.Width = 1;
                rc.Height = 5;
    
                this.Region = new Region(rc);
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width