Results 1 to 17 of 17

Thread: Draw line on windows

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Draw line on windows

    Draw Line On Windows
    Does anyone know how to make it so that something draws a line on windows using the variables x1,x2,y1 and y2. e.g
    Code:
    drawLineWindows(x1,x2,y1,y2,color)
    Picture below for example.


    Thanks, Nuclear112
    Last edited by nuclear112; Jun 12th, 2007 at 01:27 AM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    Can someone please give me a hand

  3. #3
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Draw line on windows

    I believe you will need to use SetPixels or some other API i will see what i cna come up with

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Draw line on windows

    you want to do this on the desktop (or form), or on an image or form within your program?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Draw line on windows

    Here is a wonderfully messy method to draw non persistent lines on your desktop... This method is safe but by no means great, the only way to delete what you draw is to force a desktop refresh.
    Code:
    Option Explicit
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
    Private Declare Function LineTo Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    
    Private Sub Command1_Click()
      DrawLineOnDesktop 100, 100, 400, 400, vbRed, 5
    End Sub
    
    Private Sub DrawLineOnDesktop(ByVal X1 As Long, Y1 As Long, X2 As Long, Y2 As Long, Optional ByVal Colour As Long = -1, Optional ByVal Wdth As Long = 0)
    Dim Wnd As Long, DC As Long
    Dim hPen As Long, hOld As Long
     
      Wnd = GetDesktopWindow
      DC = GetWindowDC(Wnd)
      
      If Colour <> -1 Then
        hPen = CreatePen(0, Wdth, Colour) 'create a new pen
        hOld = SelectObject(DC, hPen) 'our new pen is selected, hOld is the old object handle
      End If
    
      MoveToEx DC, X1, Y1, ByVal &H0
      LineTo DC, X2, Y2
      
      If Colour <> -1 Then DeleteObject SelectObject(DC, hOld) 'delete new pen and reselect the old one
      ReleaseDC Wnd, DC
    End Sub
    An alternative and perhaps much better way would be to create a large borderless form and have that form pretend to be the desktop either by copying the desktop DC onto it or by creating a transparent form (SetWindowLayerAttributes win2k+)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    Greats thanks for the code... Can someone edit this code so that the line can be deleted?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    Can someone please help me?

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Draw line on windows

    As Milk already stated
    the only way to delete what you draw is to force a desktop refresh.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    But isn't there way to rescript it so that it can turn it on and off?
    Have have seen it done before...

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Draw line on windows

    If have seen a line or something similar, drawn onto the desktop, which could be switched on and off, it was either not on the desktop (maybe an tranparent form) or the swichting-off was just the refreshing of the desktop!
    The code presented by milk just drwas a line onto something (the dektop), it's not an control which has a .visible property!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    Oh... Cause what im trying to achieve is... In my program I want the user to click and select a window and when they select the window a box will flash around the windows handle(hwnd).

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Draw line on windows

    Quote Originally Posted by nuclear112
    In my program I want the user to click and select a window and when they select the window a box will flash around the windows handle(hwnd).
    I take it, you don't want to activate that window (in other words, your applications window is activated but the selected is "highlighted").
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    Na its ok if the other window gets activated

  14. #14
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Draw line on windows

    OK, but why invent something new, windows does highlight the activated window, if you don't like the color of the the activated windowtitle just change the color-setting for that.
    Or am I missing a point?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    mmm, I think we are thinking about 2 different things

  16. #16
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Draw line on windows

    Hi,

    I am not sure this is the thread that I was trying to remember in my post -
    http://www.vbforums.com/showthread.p...73#post2910573

    But I will post a link to my text solution -
    http://www.planet-source-code.com/vb...53509&lngWid=1
    It may help, as it appears to be using similar API's

    If someone can provide the tweak I mention in the top link (write over a form that was Topmost) , I would appreciate it.
    Last edited by RobCrombie; Jun 14th, 2007 at 07:15 AM.
    Rob C

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Draw line on windows

    Thanks you are heading on the right track... But can you make it so that it draws a line instead of text?

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