dcsimg
Results 1 to 36 of 36

Thread: Smearing problem moving Pictureboxes and a lot of my own User Controls are present

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Smearing problem moving Pictureboxes and a lot of my own User Controls are present

    I have two Picturebox controls on the Form. I also have other system controls here and there on the Form but there are none of my own UC's at this time. When I move one of the Pictureboxes both will move as you can see from the posted code below. When I move the two pictureboxes they move nicely and smoothly even over the other controls. Now, I click on button and all of my User Controls are loaded onto the Form. Now when I move the two pictureboxes they smear very badly even if I don't move them over my controls. If I unload all of my UC's and they no longer are on the Form the two pictureboxes now move without smearing. So, it appears that the problem only occurs when my user controls are on the Form.

    Is there something about my user controls that is different from the system controls that causes this smearing side affect. Other than simple code my user controls contain one bitmap image per control. Could that be the reason. If so, anyway to correct this problem


    Code:
    Private Sub Picture2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       lx = X
       ly = Y
     End If
    End Sub
    
    Private Sub Picture2_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
         Picture2(0).Move Picture2(0).Left + ((X)) - lx, Picture2(0).Top + ((Y)) - ly
         Picture2(1).Move Picture2(1).Left + ((X)) - lx, Picture2(1).Top + ((Y)) - ly
      
     End If
    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    The smearing you are talking about typically occurs when stuff is drawn on the container/form and not refreshed as it is moved around. Transparency being the common culprit along with windowless controls

    I generally don't like to do this, but you might want to set the Form's AutoRedraw to true and see if it helps. The reason I don't like to do it is twofold: 1) forms can be large, that's a big image being cached by the system if not needed, 2) doing so generally is a band-aid for a different problem, hiding the real problem
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    .AutoRedraw doesn't do anything but .Refresh does. I'll use this approach until adding more controls changes things. Thanks for the tip

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Quote Originally Posted by LaVolpe View Post
    ...
    I generally don't like to do this, but you might want to set the Form's AutoRedraw to true and see if it helps. The reason I don't like to do it is twofold: 1) forms can be large, that's a big image being cached by the system if not needed, ...
    Technically, regardless the size of the form, using AutoRedraw on the form is slower than using AutoRedraw on a picturebox, at least for the default sizable form. The AutoRedraw buffer for the form is the size of the screen (or desktop, I don't remember for sure), so operations on that backbuffer can be a fair amount slower. The AutoRedraw buffer of a picturebox is the size of the picturebox, so is faster generally.

    The mouse moving events have a higher priority than the paint event, so if you're dragging a picturebox over a complicated form, the form updates are delayed so you get a trail of old images of your moving control. Of course, using Refresh forces the form to redraw inline, so looks better, although it is technically slower. But, usually, a cleaner look is worth the extra time spent redrawing at the maximum speed possible.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    As I said, AutoRedraw doesn't help but Refresh does...up to a point. I'm currently using a link-list approach to attach each piece together when I start to move them. If I drag 3 pieces it appears OK but when I add the 4th piece it starts to wobble just a tiny bit. Now, I add the 5th piece it's still OK but slightly more wobble and when I add the 6th piece wobbling is starting to get kind of messy and as I add more pieces the wobbling get worse and worse. Ever notice the wobbling affect when you have a lot of controls on the Form and you draw a selection rectangle around them and move them to another location on the Form. Well, that's what this is starting to look like. I guess there's no real solution to this problem unless I go back to my original plan which doesn't have any of this whatsoever no matter how many pieces are attached to each other but that is somewhat of a headache to figure out how to attach the pieces.
    Last edited by Code Dummy; Jun 26th, 2019 at 04:19 PM.

  6. #6
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    382

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    The way I would do it is with two or more PictureBoxes with no UserControls whatsoever. Drawing happens on a hidden PictureBox(with AutoRedraw=True) acting as a back buffer, which gets BitBlt-ed to the one the user sees. A third hidden PictureBox holds the pieces. This is called double buffering. But since I almost don't do games, my way may not be the easiest way.

    Off topic: I have been trying to learn Unreal game engine, and one of templates creates a Jigsaw puzzle. Language options are either C++ or a visual flow chart method called Blueprint. Here is a YouTube video showing how to do a Jigsaw puzzle(Turn on CC if you can't understand what he is saying). The other popular engine is Unity, and the only option is C#. Both engines are free until you hit a revenue threshold.
    Last edited by qvb6; Jun 26th, 2019 at 08:30 PM.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Might want to play with these 3 APIs (they work together). Only applies if your UCs have hWnds
    BeginDeferWindowPos, EndDeferWindowPos & DeferWindowPos
    Here's an example on allapi.mentalis.org: http://allapi.mentalis.org/apilist/1...8367D1474.html
    You will want to find other examples too. Those APIs basically create a batch of move/sizing calls.

    Edited: Because you are talking about UCs, do not recommend using APIs to resize a UC, from outside the UC -- moving is ok. See this thread regarding problems when the APIs are used for resizing also
    Last edited by LaVolpe; Jun 27th, 2019 at 06:10 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    @LaVolpe

    I assume your post was meant for me. OK, not knowing anything about those three API's I just took that example and applied the code to my MouseMove event (not sure what other event it could be). The output is terrible so I can assume that I am not using them correctly.

    Code:
    Const WS_BORDER = &H800000
    Const WS_DLGFRAME = &H400000
    'http://allapi.mentalis.org/apilist/16873D37FD06A83A57A07668367D1474.html
    Const WS_THICKFRAME = &H40000
    Const WS_CAPTION = &HC00000                  ' WS_BORDER Or WS_DLGFRAME
    Const HWND_BOTTOM = 1
    Const HWND_TOP = 0
    Const HWND_TOPMOST = -1
    Const HWND_NOTOPMOST = -2
    Const SWP_SHOWWINDOW = &H40
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Declare Function AdjustWindowRect Lib "user32" (lpRect As RECT, ByVal dwStyle As Long, ByVal bMenu As Long) As Long
    Private Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long
    Private Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long
    
    Dim lx As Integer
    Dim ly As Integer
    
    Private Sub ucPuzzlePiece_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       lx = X
       ly = Y
     End If
    End Sub
    
    Private Sub ucPuzzlePiece_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     Dim r As RECT, hDWP As Long
     
     If Button = vbLeftButton Then
       r.Left = ucPuzzlePiece(Index).Left + ((X)) - lx
       r.Top = ucPuzzlePiece(Index).Top + ((Y)) - ly
       r.Bottom = r.Top + ucPuzzlePiece(Index).Height
       r.Right = r.Left + ucPuzzlePiece(Index).Width
    
       ' Not sure about this one 
       AdjustWindowRect r, WS_THICKFRAME Or WS_CAPTION, False
        
       hDWP = BeginDeferWindowPos(1)    '1 = number of windows
        
       DeferWindowPos hDWP, ucPuzzlePiece(Index).hWnd, HWND_TOP, r.Left, r.Top, r.Right, r.Bottom, SWP_SHOWWINDOW
    
       EndDeferWindowPos hDWP
       
       Picture1.Refresh
     End If
    End Sub
    
    Private Sub ucPuzzlePiece_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       '
     End If
    End Sub

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    I've never used those APIs, so tried something simple using pictureboxes on a form. The following works and changes highlighted in blue. By the way, AdjustWindowRect not in play here

    Code:
    Const SWP_NOSIZE As Long = &H1
    Const SWP_NOZORDER As Long = &H4
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long
    Private Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long
    
    Dim lx As Single
    Dim ly As Single
    
    Private Sub ucPuzzlePiece_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       lx = X
       ly = Y
     End If
    End Sub
    
    Private Sub ucPuzzlePiece_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     Dim r As RECT, hDWP As Long, i As Long
     
     If Button = vbLeftButton Then
        
       hDWP = BeginDeferWindowPos(1)    '1 = number of windows
        
       X = X - lx: Y = Y - ly
       For i = ucPuzzlePiece.LBound To ucPuzzlePiece.UBound
       
       r.Left = ucPuzzlePiece(i).Left + X
       r.Top = ucPuzzlePiece(i).Top + Y
       hDWP = DeferWindowPos(hDWP, ucPuzzlePiece(i).hWnd, 0, r.Left, r.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER)
    
       Next
    
       EndDeferWindowPos hDWP
       
       Picture1.Refresh
     End If
    End Sub
    
    Private Sub ucPuzzlePiece_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       '
     End If
    End Sub
    For each piece in your linked list that is to be moved, do a DeferWindowPos call. Notice that we need to keep the return value which is fed into the next call & finally the EndDerWindowsPos call. In my example, I just used a loop (10 items being moved). Obviously you'd supply the indexes from your linked list. You may want to create a separate custom function, call it to set up the deferred window position & execute. Just to clean up the MouseMove event and make future editing a bit easier?

    The SWP_NOSIZE & SWP_NOZORDER is in play since you are not changing those. And because we use those flags, we don't need to supply/calculate values for the respective DeferWindowPos parameters

    The parameter for BeginDeferWindowPos should be close to correct, but it will be increased as needed by Windows.

    I changed the cached X,Y variables to Single vs Integer since the X,Y params passed to mouse events are always single.

    In my tests, the picboxes (puzzle pieces) scalemode was same as their container. That's pretty important else scale the new X,Y coordinates as needed
    Last edited by LaVolpe; Jun 27th, 2019 at 06:54 PM. Reason: fix garbbled text
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    382

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    From what I understand, Begin/DeferWindowPos/End functions update the window size and position, but without moving it, so the puzzle piece seems to stay where it was, and doesn't follow the mouse pointer, until you drop it(Call EndDeferWindowPos), so it seems to jump from start to finish without smooth movement. That's probably not what the OP wants.

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Quote Originally Posted by qvb6 View Post
    From what I understand, Begin/DeferWindowPos/End functions update the window size and position, but without moving it, so the puzzle piece seems to stay where it was, and doesn't follow the mouse pointer, until you drop it(Call EndDeferWindowPos), so it seems to jump from start to finish without smooth movement. That's probably not what the OP wants.
    That's not what my example does. Try it. Add an array of picboxes on the form named ucPuzzlePiece. Set their scalemode to pixels and the form's scalemode to pixels & play
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    I did exactly what you said and when I mouse down they all dissappear. Kind of like what happens when one object is Twips and the other is Pixel but my Form, and my 10 picture boxes are all in Pixels

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Quote Originally Posted by Code Dummy View Post
    I did exactly what you said and when I mouse down they all dissappear. Kind of like what happens when one object is Twips and the other is Pixel but my Form, and my 10 picture boxes are all in Pixels
    Their container (the form? another picbox?) needs to be the same scalemode. If they all disappear, either the scalemode is off or the starting X,Y is not being cached.

    That sample I supplied works perfectly. If it doesn't for you, then something simple is amiss.

    If I need to, I can zip up a sample project, but shouldn't have to since what I posted isn't complex.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    I don't understand how it works for you. I found the problem on my end. You had cx and cy set to 0 and when I changed them to the width and height of the picturebox it works so how does it work for you if you are using 0 for cx and cy

  15. #15
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Those 2 new SWP constants I added. See post #9 again. Here's a test project. If it doesn't work for you, as-is, would like to know
    Attached Files Attached Files
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    You still have cx and cy set to 0 and that was what caused my picture boxes to dissappear

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    It works great with the picture boxes but still it doesn't work well with the puzzle pieces user controls maybe because they are not all the same size which I believe I have to have them the size of the image (no borders) but I'm not sure that's the reason

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    I was assuming the method of drawing the user control was more involved so slows the rendering,
    I still favor a drawing to backbuffer and then refreshing, rather than using controls.
    Since I had the extra time because of my wife's stay at the hospital, and I worked on it there to keep me entertained, I've pretty much got my version working well enough to demo, for a fixed 5x5 puzzle.

    But I was thinking of cleaning up the code a little and use my previously posted version to add the capability of the user selecting the picture to be turned into a puzzle, and and perhaps allowing a the same selection of sizes 2x2 to 7x7 which I had earlier. Of course, with a jigsaw, perhaps it should be more flexible, and not be limited to a square sized grid, but I probably have taken this about as far as I want to. I've thought about a way to make the pieces a bit more distinctive, but it would still be based on squarish pieces with a checkerboard pattern of pieces with ears and pieces with matching gaps for the ears to fit into.

    I ended up not making a simple linked list to link pieces together that have been locked, but each pieces has a four element array identifying the piece (if it exist) on its left, above, right and below it, i.e. the pieces that should be there when the puzzle is solved. That way, when you're dragging a piece, it only has to check those pieces that are its neighbors to see if it has been positioned close enough to be locked into place. There is a parallel Boolean array of four whose corresponding value is set true when that neighbor is locked to the piece.

    Thus, when you start to drag a piece, it calls a recursive sub to move the piece, which calls itself for each neighbor that is locked to that piece to move by the same amount. Since this is recursive, all the pieces that are locked together eventually get moved. When a pieces is moved, it is added to an array, which is checked by the recursive routine to ensure the pieces is not processed a second time, since a single piece can be linked to by four other pieces, and any piece linked to may be linked back to that piece, the piece could be passed to the recursive routine many times. It may not be the most efficient, I don't really know, I haven't analyzed how many times the routine recurses and to what depth, but seems fast enough for my 5x5 test.

    The recursive routine does count how many pieces are added to the array as it traverses the hierarchy so uses that to know when the puzzle is solved, since if all the pieces are added to the array, then all the pieces are locked together so the puzzle is done.

    The pieces are allow to be rotated freely 360 degrees, and are initially placed at random amounts of rotation. When you rotate them, by design, they do stick a bit at 0, 90, 180 and 270, to make them easier to align with the grid. But in order to lock, you have to have them properly oriented, with top up within a couple of degrees of 0, and the adjacent piece has to be within small number of pixels of the correct position. When they lock, the rotation of the locked pieces are forced to 0 degrees of rotation and are no longer allowed to be rotated.

    Currently the picture is scaled to fit in a 600x600 pixel picturebox. When the image is cut up into pieces, they are place on a 1200x1200 picturebox (although distributed in the original 600x600 size area). This allow the user to resize the form to see more of a working area, although many users may not have 1200 pixels vertically to work with. They can spread the pieces out and group them within the area they see fit. Actually, since you can assemble the pieces anywhere you want (as long as the result is 0 degree of rotation), I could just resize the picturebox used to hold the pieces to whatever size would fit in the area of the form they resized it to, rather than use a fixed 1200x1200 size.

    I initially just used the left mouse button to move, and the right button to rotate, but also allowed pressing a "shift" key (i.e. shift, ctrl or alt) when using the left mouse button to switch it between move mode and rotate mode. It has the interesting side effect that while in rotation mode, the mouse can be moved away from the piece to allow finer control of the rotation, and when the shift key is released the piece is still moved relative to the mouse, but the mouse doesn't have to be over the piece, so can be out of the way when positioning the piece.

    Anyway, I expect I'll clean the code up a bit, and at least add the user chosen image option and go ahead post the project sometime tomorrow.

    p.s. Another change I did. Originally, I was rotating each piece on the fly as it was drawn using the GDI advance drawing capability and the GDI matrix transform. GDI+ may be better and faster for this, but I haven't done any GDI+ with VB6, really, so stuck with using the GDI api calls that have been around for much longer. But that does slow down the drawing process. So, I changed the process to draw the pieces rotated in individual pictureboxes whenever they were rotated by the user, so I didn't have to rotate them when drawn to the puzzle surface. That sped up the drawing quite a bit. I didn't try any further speed enhancements, such as separating the drawing into two layers, one for pieces not being moved, and one for pieces being moved, and using a blit from one layer to the other to "clear" a subsection of the drawing, and then only draw the moving pieces. I just redraw the whole thing each time.
    Last edited by passel; Jun 27th, 2019 at 11:34 PM.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Quote Originally Posted by LaVolpe View Post
    Those 2 new SWP constants I added. See post #9 again. Here's a test project. If it doesn't work for you, as-is, would like to know
    Works perfectly. I changed my code to use the two SWP constants then mine works so I guess that was the problem. However, although it works using picture boxes it doesn't work with user controls. Never a dull moment with this programming stuff.

    PS. Never mind my post #16

  20. #20
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Quote Originally Posted by Code Dummy View Post
    However, although it works using picture boxes it doesn't work with user controls. Never a dull moment with this programming stuff.

    PS. Never mind my post #16
    regarding post 16... Ignored it when I saw it -- knew it was something simple you didn't do

    As for the uc's not working... still jumpy? curious what isn't working. Just a guess, but refreshing after each mouse move can't be efficient. Kinda curious as to why you need to refresh so often anyway. My guess is that the masking you are using in your UC to produce transparency requires it.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    It's not the Refreshing and it's not transparency
    Attached Files Attached Files

  22. #22
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,234

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Well, I got bad news and I got, well, nothing else. I don't understand why it's doing what it's doing other than it's a VB usercontrol and the API may be interfering with communication between VB & the UC.

    I see what's happening. If you add "Debug.Print X, Y" before your BeginDeferPos call and starting dragging the UCs, you will see something odd. The X, or Y, or both before/after coordinates are jumping around wildly. Now do the same with the pictureboxes. Although I see it, I don't have any immediate ideas as to why. Maybe if you spent serious time with Spy++ , you may get an idea of what's happening, unless someone jumps in here and knows why.

    Anyway, if you print out the X,Y for the pictureboxes, you'll see a much smaller difference between before/after coordinates which is why it looks relatively smooth and not jumpy when you drag them.

    Though not a real solution, I tried to do the moves from within the UC vs from the raised event. Didn't fair any better. I tried variations of AutoRedraw, Refresh, and other property values with no joy.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  23. #23
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    I'm not quite sure what is wrong your code, couldn't see anything, but I decided just to try moving the controls the way I normally would and it seemed to work fine. All done in the mouseMove, no form scope variables needed.
    Code:
    Private Sub UserControl11_MouseMove(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
    
      Static lpx As Single, lpy As Single
      Dim dx As Single, dy As Single
      Dim i As Integer
      
      If Button = vbLeftButton Then
        dx = x - lpx
        dy = Y - lpy
        For i = UserControl11.LBound To UserControl11.UBound
         With UserControl11(i)
           .Move .Left + dx, .Top + dy
         End With
        Next
      Else
        lpx = x: lpy = Y
      End If
      
    End Sub
    I guess that is probably close to what you were doing before and you feel that the usercontrol ripples excessively?
    Last edited by passel; Jun 29th, 2019 at 12:27 AM.

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    @passel

    The method you posted is what I am using (coded slightly different, but basically the same). I can move 4 to 5 pieces at same time and it is not too bad but once I start adding 6, 7, 8, etc the wobbling gets worse

    Code:
    Private Sub ucPuzzlePiece_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       lx = X
       ly = Y
     End If
    End Sub
    
    Private Sub ucPuzzlePiece_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     Dim pieceNo As Integer
     Dim n As Integer 
    
     If Button = vbLeftButton Then
       For n = 0 To 35
         If LinkedMoveList(Index, n) <> 0 Then
           pieceNo = LinkedMoveList(Index, n)
       
           With ucPuzzlePiece(pieceNo)
             .Move .Left + ((X)) - lx, .Top + ((Y)) - ly: .ZOrder 0:
           End With
         End If
       Next n
       
       Picture1.Refresh
     End If
    End Sub
    Later, I tried what LaVolpe suggested above

    Code:
    Private Sub ucPuzzlePiece_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       lx = X
       ly = Y
     End If
    End Sub
    
    Private Sub ucPuzzlePiece_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     Dim r As RECT, hDWP As Long, i As Long
     
     If Button = vbLeftButton Then
       hDWP = BeginDeferWindowPos(ucPuzzlePiece.UBound)
        
       x = x - lx: Y = Y - ly
       
       For i = ucPuzzlePiece.LBound To ucPuzzlePiece.UBound
         r.Left = ucPuzzlePiece(i).Left + x
         r.Top = ucPuzzlePiece(i).Top + Y
         hDWP = DeferWindowPos(hDWP, ucPuzzlePiece(i).hWnd, 0, r.Left, r.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER): UserControl11(i).ZOrder 0
       Next
    
       EndDeferWindowPos hDWP
        
       Picture1.Refresh
     End If
    End Sub
    Using this approach turned out disastrous. Even moving one piece is bad. So, I went back to my original method (first example). However, I believe that as the player starts locking more and more pieces together and then moving that total locked array of pieces the quality of movement is going to be an eye sore. Don't know how to get around it for now
    Last edited by Code Dummy; Jun 29th, 2019 at 04:09 PM.

  25. #25
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Well, I guess my version of a jigsaw program is far enough along that I can post it for you to try out.
    I'm getting a bit tired of it, so not planning on improving it much beyond its current state.
    I didn't add the drag and drop picture that I had in my earlier version of the tile shuffle puzzle.
    I just used the old File and Directory list controls to allow selecting a picture, so a bit clunky. I didn't want to use the File Dialog because of the added dependency, although, it may not be that big of deal. I just never had to do to many programs where a file would have to be chosen by the user, so don't have an established pattern for how I would like to do that.

    I described the program above a bit already, so won't repeat that here.
    One thing I found is that some pictures I chose, had a lot of white in them, and I had a white background for the puzzle surface, which made it hard to distinguish the pieces. So, I added three color bars, and a small picturebox, where the user could change the color of the small picturebox, to whatever they liked by dragging left and right on the three color bars. They would then click on the colored picturebox to assign that color as the background color of the puzzle.

    Not intuitive, but it gets the job done.

    When rotating a piece, if you click near the center of the piece, the angle can change rapidly, and may seem odd. I added a red reference line from the mouse pointer to the center of the piece, so the user can see how the rotation is being modified with the mouse movement.
    Also added processing so the arrow keys can move the active piece (with shift to move five pixels at a time). Ctrl with left or right arrow will rotate the active piece. You can't select the active piece using the keyboard. It is whatever piece you last clicked on with the mouse.

    Since I'm drawing all the pieces on a picturebox with AutoRedraw set True, you won't see any wavering since all the pieces are exposed in their new positions at the same time.

    I think I mentioned before that while the left mouse button moves the piece, if you hold the ctrl key, then it switches to rotating the piece, so you can switch between rotating and dragging modes without releasing the left mouse button. Since the rotation allows you to move away from where you clicked on the piece, to vary the resolution of the amount of mouse movement to rotation of the piece.

    Once you've chosen a picture, you can change the number of pieces in the puzzle by hitting the Initialize button again and choosing a different size. You don't have to choose the picture again in that case. Also, if you want to restart the current puzzle with the current number of pieces, you can just hit the scramble button.

    There may be bugs, but it works well enough to post, I think.Name:  puzzleGen.jpg
Views: 108
Size:  72.4 KB
    Attached Files Attached Files

  26. #26
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,318

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    FWIW -
    I've just uploaded a Vector-Path-based approach into the CodeBank, which shows how to do such stuff in a "more curvy style"...
    http://www.vbforums.com/showthread.p...ng-Cairo-Paths

    Implementation-wise the approach is worth studying IMO, because of the way the Class-Hierarchy is used:
    - a Form-Class cfMain which holds a "Root-Collection", named PieceGroups
    - and two Helper-Classes cPieceGroup and cPiece, to manage interactions and the recombinations conveniently
    Lines of code in each of the 3 Classes: averaging at roughly 80.

    Here a ScreenShot:


    HTH

    Olaf

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    @passel

    I downloaded your puzzle but this is what I get. This is right out of the box. Other than setting a color to the background of picPuzzle so I could see how the pieces looked I made no changes whatsoever. Any ideas about it

    P.S. I even downloaded the zip file a second time just a few minutes ago and it does the same thing
    Attached Images Attached Images   
    Last edited by Code Dummy; Jul 2nd, 2019 at 07:31 PM.

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    I found out what causes that. It's the picture size you load. Too big it screws your pieces up

  29. #29
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Does it? How big is too big?

    It looks like the image you picked is one that has two smaller images in it, with a lot of white space around them. The program is just cutting up the image given, it doesn't try to crop the image in any way.

    I just downloaded the zip file in another directory and tried it, using the Petalo.jpg image from your ShuffleTiles V3 example, and it looks fine to me.

    Note, you can change the color of the background of the puzzle by dragging on the color bars above the picture, then clicking on the square which is showing you the color you're creating. Since the default background is white, i.e. full red, green and blue, you would drag to the left to reduce the color components initially.

    Name:  PetaloExample.jpg
Views: 70
Size:  79.0 KB

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    OMG, your'e right. I didn't even notice that it was a bitmap with two images in it, LOL. I thought your program was splitting it up like that. I haven't gone through it yet to see everything you have done but I plan on it.

  31. #31

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Hope I'm not putting my foot in my mouth[again] but I just noticed that the pieces do not move with the mouse; they move moments later after you stop moving but before you release the mouse. I thought when I first tried your program the pieces moved smoothly with the mouse but now they don't. Could it be a problem with my system or something else. Check it out and let me know

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    OK, here's what I see. I'm running Task Manager -> Performance

    Pieces are just sitting-----> CPU Usage is 0 to 10%
    Mouse Down on Piece-----> CPU Usage goes up to 100% then drops down to 0 to 10%
    Drag Piece---------------> CPU Usage goes up to 100% and holds it until I stop moving mouse(piece)
    Stop Dragging------------> CPU usage drops to 10% and then the piece moves to new location

    Do you think this is normal

  33. #33

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    All is well. It occurs when you have a big picture (like the one I've been using, Petalo.jpg) and make it a 2 x 2 puzzle.

  34. #34
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    p.s. I was working on this post, and investigating some things, so your post #33 occurred while I was working on this one, so I didn't see it before this post.
    I was assuming it might be the recursion, which would be worse for a larger number of pieces, so was testing with a 7x7. Since you now say it was with a 2x2, then recursion is definitely not the issue. I think it has to be drawing time. I would try to add the Refresh as mentioned in the post below. I made the text bold to be easier to find...

    {original post}
    I don't think it is normal, and I don't get any results like that. The performance on my machine, for the puzzle task, generally sits at 0% when not interacting with it, and may go up to 17% if rapidly dragging.
    On the current machine, an I7 with four cores, so 17% would be a fair amount of one core.

    That said, I can believe that some condition with the recursive method used to find the pieces to be dragged, could take more time than needed.
    But, you say you get the jump in processing when you MouseDown as well, and the MouseDown doesn't call the recursive routine so that wouldn't be a common think.
    The MouseDown does call findPiece, which would involve a loop, but not an extensive one, and findPiece is not called in the MouseMove, so is also not common to both events.

    Of course, the sub DrawThePieces is called in both events, and if for some reason the Drawing is taking a lot of processing time on your machine, then that is an issue.
    In that case, you may get a more responsive looking application by adding a picPuzzle.Refresh as the last line in the DrawThePieces sub.

    I didn't plan on using a recursive routine, but just sort of fell into while writing the code and it seemed to work fine so I left it. I knew starting off that continually doing that recursive process call for every movement of the pieces would be a relatively large bit of processing and I would probably want to change that back to a list of some sort that gets created to link the pieces, as I've done in the past.

    I was a little surprised when doing that recursive call with every movement of the dragged piece didn't seem to impact the performance as much as I expected it to, so I didn't do anything to improve it. Perhaps I should.
    Last edited by passel; Jul 5th, 2019 at 09:48 PM.

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    548

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    @passel

    I have been going through your code logic and I must say I am very impressed. I am now in the process of making changes to the code so that the pieces will have a irregular shape to them (like you see in commercial puzzles). Progress is very slow and for now I am using pre-cut shapes for the picMask, picsBlack, picsWhite, picRotWhite, and picRotBlack pictures but the results are what I want. I think if I were to download some templates off the net I might figure out a way to auto-cut the pieces based on the templates. That's my next step.

    The problem I am now having is that because the pieces are no longer square like you make them the locking no longer works. I don't suppose you have any idea how I might remedy this problem.

  36. #36
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,273

    Re: Smearing problem moving Pictureboxes and a lot of my own User Controls are presen

    Well, there are any number of ways that it could be managed.
    Even the familiar puzzle (I have worked on some puzzles which weren't quite regular, so would be harder to deal with) is still oriented in rows and columns of pieces.
    So, you should be able to place a grid of evenly spaced lines vertically and evenly spaced lines horizontally that intersect at some center point in each piece.
    So instead of checking for a single distance in both X and Y directions, you would check for a different distance in X versus Y.

    The X and Y where the grid lines intersect would be the center of the piece, which is the point it would rotate around.
    So, where there is a single value now, ltOffset (which stands for LeftTopOffset) for where the center point of the square is, now you would have two values, left and top offset, or perhaps you would go with cX and cy (or spell it out CenterX and CenterY), etc.

    Anyway, kind of busy and its taken hours to just get this little post to this point, with all the interruptions, so I'll send it out rather than have it sitting in the browser any longer and possibly loosing it somehow.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width