CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 1999
    Posts
    40

    Move "+" without erasing something already drawn

    I use Line function to draw line in a picture box, but here is a problem.
    I want to use some kind of mark that moves on the line I have drawn previously.
    I am not sure if I should use a letter, "+" sign, because it will erase the line that has been drawn. How to move some kind of mark without erasing something that has been drawn already.

    Please help me.




  2. #2
    Join Date
    Jul 1999
    Location
    Sandhem, Sweden
    Posts
    35

    Re: Move "+" without erasing something already drawn

    You can do this using the XOR function for drawing. The XOR works like this, it changes all 0's to 1's, and all 1's to 0's (binary).

    If you have a 2 colored bitmap with a cross, where the cross is colored white and the surrounding area is black, then you XOR this bitmap at the current position of your mouse. In this same event you store the X and Y coordinate. At the next time you enter the drawing event you XOR the same bitmap at the previously stored coorindate to remove the cross. Then you XOR it again at the current position and store the coordinates.
    So when you moving your mouse around on the bitmap you will have the cross following you without destroying what you have drawn.
    You can do the XOR draw using the BitBlit API.

    Hope this helps you a bit.

    Regards,
    Michael Nattfalk



  3. #3
    Join Date
    Dec 1999
    Posts
    40

    Re: Move "+" without erasing something already drawn

    Thanks for help.
    But as far as I know, Bitblt is not supported by VB.
    I guess I have to use DLL or something.
    How can I do that with XOR? Somebody?


  4. #4
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Move "+" without erasing something already drawn

    You don't need to use the API, you can set the DrawMode to vbInvert.

    Here's an Example I put together which demonstrates the use of vbInvert to get a Rubber Band Effect for Cropping an Image:

    On a Form with 2 Pictureboxes, Load a Picture into Picture1..

    private oX as Integer
    private oY as Integer
    private lX as Integer
    private lY as Integer

    private Sub Form_Load()
    Picture1.ScaleMode = vbPixels
    Picture2.ScaleMode = vbPixels
    Picture2.AutoRedraw = true
    End Sub

    private Sub Picture1_MouseDown(Button as Integer, Shift as Integer, X as Single, Y as Single)
    If Button = vbLeftButton then
    With Picture1
    .DrawMode = vbInvert
    oX = X
    oY = Y
    lX = oX
    lY = oY
    End With
    End If
    End Sub

    private Sub Picture1_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single)
    If Button = vbLeftButton then
    Picture1.Line (oX, oY)-step(lX - oX, lY - oY), , B
    Picture1.Line (oX, oY)-step(X - oX, Y - oY), , B
    lX = X
    lY = Y
    End If
    End Sub

    private Sub Picture1_MouseUp(Button as Integer, Shift as Integer, X as Single, Y as Single)
    Dim iTmp as Integer
    Dim iW as Integer
    Dim iH as Integer
    If Button = vbLeftButton then
    Picture1.Line (oX, oY)-step(lX - oX, lY - oY), , B
    Picture2.Cls
    If X < oX then
    iTmp = oX
    oX = X
    X = iTmp
    End If
    If Y < oY then
    iTmp = oY
    oY = Y
    Y = iTmp
    End If
    If X = oX then X = 1
    If Y = oY then Y = 1
    iW = X - oX
    iH = Y - oY
    Picture2.Width = ScaleX(iW, vbPixels, vbTwips)
    Picture2.Height = ScaleY(iH, vbPixels, vbTwips)
    Picture2.PaintPicture Picture1.Image, 0, 0, iW, iH, oX, oY, iW, iH
    SavePicture Picture2.Image, InputBox("Save Cropped Image as..", "Save as..", "C:\Cropped.bmp")
    End If
    End Sub




    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  5. #5
    Join Date
    Jul 1999
    Location
    Sandhem, Sweden
    Posts
    35

    Re: Move "+" without erasing something already drawn

    The BitBlt API is located in the GDI32.DLL.

    The declare looks like this:
    Declare Function BitBlt Lib "gdi32" (byval hDestDC as Long, byval x as Long, _
    byval y as Long, byval nWidth as Long, byval nHeight as Long, byval hSrcDC as _
    Long, byval xSrc as Long, byval ySrc as Long, byval dwRop as Long) as Long



    Where:
    hDestDC .. is the handle of the destinations Device Context.
    x .. is the left position on the x axis
    y .. is the top position on the y axis
    nWidth .. is the destination width on the BitBlt'ed image
    nHeight .. is the destination height on the BitBlt'ed image
    hSrcDC .. is the handle of the source's Device Context
    xSrc .. is how much from the source image to copy, on the x axis
    ySrc .. is how much from the source image to copy, on the y axis
    dwRop .. is the Raster Operations. Here you define that you want to use
    the XOR function.

    The Device Context can be retrieved using the GetDC/ReleaseDC API in USER.DLL, with the PictureBox's hWnd as parameter.

    ROP's (Raster Operations)
    -------------------------
    Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
    Const SRCPAINT = &HEE0086 ' (DWORD) dest = source OR dest
    Const SRCAND = &H8800C6 ' (DWORD) dest = source AND dest
    Const SRCINVERT = &H660046 ' (DWORD) dest = source XOR dest
    Const SRCERASE = &H440328 ' (DWORD) dest = source AND (NOT dest)



    These are only the most common ROP's.

    Hope this helps you a bit more. There are lot's of examples out there on the net. Just search and I bet you'll find some.

    Regards,
    Michael Nattfalk



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

Featured