Results 1 to 7 of 7

Thread: [RESOLVED] How to draw a straight line on an image control?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    77

    Resolved [RESOLVED] How to draw a straight line on an image control?

    Any ideas on how to draw a line on a transparent image control, then how to remove it? TIA, Bob.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,161

    Re: How to draw a straight line on an image control?

    Image controls don't support drawing methods. You could use a shape control. It would probably help if you explained not just what you were trying to accomplish but why.

    Perhaps you can adapt the following code:
    Code:
    Option Explicit
    
    Private Type POINTAPI
       x As Long
       y As Long
    End Type
    
    Private Declare Function GetDC Lib "User32.dll" (ByVal hwnd As Long) As Long
    Private Declare Function LineTo Lib "Gdi32.dll" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function MoveToEx Lib "Gdi32.dll" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
    Private Declare Function ReleaseDC Lib "User32.dll" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    
    Private Sub Form_Click()
    Dim dc As Long
    Dim prevpoint As POINTAPI
    
    dc = GetDC(Me.hwnd)
    MoveToEx dc, 0, 0, prevpoint
    LineTo dc, 3000, 1000
    
    ReleaseDC Me.hwnd, dc
    
    End Sub
    It works on a form. And, the LineTo function returns a non-zero value when used on an image control's picture handle, nothing appears however.

  3. #3
    Addicted Member
    Join Date
    Jul 2020
    Posts
    168

    Re: How to draw a straight line on an image control?

    The Image object lacks the Line method and the hDC property, which excludes drawing a line in it. You can get the hDC using the IOleInPlaceSiteWindowless interface, but I haven't figured out how.
    The easiest way is to load an image into the control and then delete it.
    For example, like this:
    Code:
    Private Sub Command1_Click()    'Deleting an Image 
    Image1.Picture = LoadPicture()
    End Sub
    
    Private Sub Form_Load()
    Image1.Picture = LoadPicture("d:\Downloads\1.jpg")
    End Sub
    Last edited by Argus19; Apr 19th, 2021 at 12:25 PM.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,172

    Re: How to draw a straight line on an image control?

    Or you can roll your own enhanced replacement for the Image control, e.g.:

    Name:  sshot.png
Views: 71
Size:  2.6 KB

    And if you don't need the transparent GIF as a backdrop to "Fromage" controls you can rip out all of the stuff supporting the Picture property, making it even simpler. If you only need to draw a single line it gets simpler than that. If the color and width of the line are fixed then it gets even simpler yet.

    It can also be tweaked as a windowed control if you need it to rise on top of other control(s) that are windowed. Those live in a different z-order space from things like Image controls.


    You didn't give us a lot to go on.
    Attached Files Attached Files
    Last edited by dilettante; Apr 19th, 2021 at 09:39 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    77

    Re: How to draw a straight line on an image control?

    This looks very promising. Thanks.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,172

    Re: [RESOLVED] How to draw a straight line on an image control?

    It may not be what you need but there might be some ideas there.

    Here's a windowed version. I added a Timer to animate moving Fromage1 right and left, which lets you see how z-order works with the button controls.

    Between the two versions maybe you'll have enough ideas to get what you need.
    Attached Files Attached Files

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,172

    Re: [RESOLVED] How to draw a straight line on an image control?

    Here's another variation. It is a windowless control meant to "overlay" other windowless controls. In this case the "other" is called a "Doodad."

    The Fromage overlay is transparent but can have drawn lines added or cleared by assigned "ID" values. Form1 has a bunch of Doodads, a green one as ddDest and a control array of pink ones as ddSource.

    Left-click a ddSource and a line gets drawn within Fromage from the center of that ddSource to the center of the ddDest control. Right-click to clear each one.

    Name:  sshot.png
Views: 25
Size:  1.5 KB

    Doodad is a very simple control to replace rectangular Shape controls and adds MouseDown/MouseUp events and a crosshair cursor that a Shape lacks. It also carries an ID property that normally matches its Name (including Index if any) or can also be assigned explicit values. Form1 uses the ID of each ddSource to tag the lines drawn within Fromage's DrawnLines Collection.


    This is sort of a stunt and not really that great for anyting extensive. All of this is much better done without controls, using graphics methods instead. Controls are not drawing tools, sprites, or animabubs of any kind and should only be used as such in very simple quick and dirty programs.

    Others here can give you better advice, I'm not a graphics guy at all and only dabble at it here and there myself.
    Attached Files Attached Files

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