Results 1 to 20 of 20

Thread: [RESOLVED] PictureBox flickering while Resizing the Form

  1. #1

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Resolved [RESOLVED] PictureBox flickering while Resizing the Form

    Hi there,

    I have some trouble resizing the Form and the controls inside the PictureBox, it flickers while resizing the Form.

    I have tried already LockWindowUpdate, WM_SETREDRAW, AutoRedraw and can't solve my issue.

    Can someone please look at my attached project?
    Demo Resize.zip

    Thank you,

    Kind regards,
    Viktor

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

    Re: PictureBox flickering while Resizing the Form

    Each time you individually set the Width and Height you are performing 2 sizing actions. Do it just once and that should help
    Code:
     objCtrl.Move objCtrl.Left, objCtrl.Top, [newWidth], [newHeight]
    Above is a template of sorts.

    Now not all controls can be moved that way, i.e., Line controls. Comboboxes typically have a fixed height (depending on properties) and trying to change the height results in an error in that case.

    Edited: same applies when moving an item vs. moving & resizing. Use the Move method and supply the new left/top coordinates. Otherwise, setting the Top and Left properties individually results in 2 move actions
    Last edited by LaVolpe; Aug 12th, 2020 at 01:00 PM.
    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
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Hi LaVolpe,

    Thank you for the fast feedback!

    I tried your suggestion and changed the mentioned line in the CenterObject Sub, but didn't improved the flickering issue.

    Do you have any other proposition or advice?

    Kind regards,
    Viktor

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: PictureBox flickering while Resizing the Form

    I had to change the form's borderstyle so I can resize it to see the problem.

    The problem is the way you are trying to prevent the form from be resized. Each time someone tries to size the form, you tell the form to go back to another size. This is at least 2 to 4 resizing actions and maybe more, each time the mouse is dragging the border edges for sizing.

    What is your goal regarding the user sizing the form?
    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}

  5. #5

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by LaVolpe View Post
    What is your goal regarding the user sizing the form?
    I'm trying to develop an application for Windows Tablet (Win10 Pro), and I wish the Form has only two size, first initial size (full screen), but to be able to have second size (fixed windowed 800x600 pixels), and all application controls (TextBox, Labels, CommandButtons, ListView, etc...) would be placed into the PictureBox (Container), so, the PictureBox would be always centered in the Form.

    That's why I set the Form BorderStyle to be fixed, not sizable by mouse by user.

    Kind regards,
    Viktor

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by beic View Post
    I have some trouble resizing the Form and the controls inside the PictureBox, it flickers while resizing the Form.
    What flickers? Do you mean the form is flickering when you are setting its position? The PictureBox is not flickering and the controls inside of it are not flickering. You have to provide a better demo and instructions how to produce the flickering you want to prevent. Usually it's as simple as putting your controls inside a PictureBox and using WM_SETREDRAW -- very simple if there is a reproducable sample code of the problem.

    Just don't use LockWindowUpdate API for anything -- it's not designed to prevent flicker in any way. Au contraire -- it *forces* your desktop icons to flicker. Just don't use it for anything, forget about it. I'll petition MS to remove it or make it a noop in future versions of Windows.

    cheers,
    </wqw>

  7. #7

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by wqweto View Post
    What flickers? Do you mean the form is flickering when you are setting its position? The PictureBox is not flickering and the controls inside of it are not flickering.
    The PictureBox (as a Container) flickers while you are resizing the Form, you clearly see the inside controls how they are moving.

    Kind regards,
    Viktor

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by beic View Post
    The PictureBox (as a Container) flickers while you are resizing the Form
    The form has no resizable border. Do you mean resizing with maximize/restore buttons in upper right corner?

    cheers,
    </wqw>

  9. #9

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by wqweto View Post
    Do you mean resizing with maximize/restore buttons in upper right corner?
    Yes, that's correct!

  10. #10
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: PictureBox flickering while Resizing the Form

    Ok, I see what the problem is with this approach. Form's Resize event is too late to prevent flicker as the form is already positioned/sized and so controls get repainted when moved around at this point.

    You'll need to handle WM_SYSCOMMAND specifically wParam = SC_MAXIMIZE and wParam = SC_RESTORE if you are willing to implement form subclassing that is.

    What might work is first disable WM_SETREDRAW before passing WM_SYSCOMMAND to next SubclassProc, then position controls in form's Resize event as already implemented and finally enabling WM_SETREDRAW before returning from SubclassProc.

    Edit: No, this didn't work.

    What worked on my monitor was this

    Code:
    Public Function SubclassProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long, Handled As Boolean) As Long
        Select Case wMsg
        Case WM_SYSCOMMAND
            If wParam = SC_RESTORE Then
                Picture1.Move 0, 0
            ElseIf wParam = SC_MAXIMIZE Then
                Picture1.Move 9360, 3666
            End If
        End Select
    End Function
    Of course the position on SC_MAXIMIZE has to be calculated based on monitor estimated size (the one the form is placed on) and form's border/title size which is an effort in its own right.

    cheers,
    </wqw>

  11. #11
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: PictureBox flickering while Resizing the Form

    another way is to use fading.
    fade out, hide, resize, show, fade in

    that way u also get an effect while changing size.

  12. #12

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by wqweto View Post
    What worked on my monitor was this

    Code:
    Public Function SubclassProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long, Handled As Boolean) As Long
        Select Case wMsg
        Case WM_SYSCOMMAND
            If wParam = SC_RESTORE Then
                Picture1.Move 0, 0
            ElseIf wParam = SC_MAXIMIZE Then
                Picture1.Move 9360, 3666
            End If
        End Select
    End Function
    Okay, any idea to be able to start with it, implementing calling, destroying it after or?

    Const WM_SYSCOMMAND = &H112
    Const SC_MAXIMIZE = &HF030
    Const SC_RESTORE = &HF120

  13. #13
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: PictureBox flickering while Resizing the Form

    Just FYI, as flickerless as I could get it: Demo Resize.zip

    Had to remove Form_Resize implementation altogether and position Picture1 inside WM_SYSCOMMAND. Not the best of code, more like total kludge, don't use in production.

    cheers,
    </wqw>

  14. #14

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Thank you, really improved the PictureBox still effect, but the same resize flicker is there...

    Maybe should I go with MID form and a simple centered Form inside.

    Don't know yet, need to decide...

  15. #15
    Junior Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    29

    Re: PictureBox flickering while Resizing the Form

    Hi beic - I see the flicker when changing your form position.
    I tried wqweto suggestion on subclassing, and used iPrank’s code (https://www.vbforums.com/showthread....on-in-MDI-Form) to capture the repositioning event prior to form repainting. During that capture, the module1 repositions your Picture1. Flicker appears to be gone.

    There is minimal change to your code. All you need to add is the module1, and

    pOldWindPoc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)

    in your Form_Load event.

    ===============================
    EDIT:
    AND add un-hook to Form_Unload event, such as:

    Private Sub Form_Unload(Cancel As Integer)
    ' Un-subclass the form
    SetWindowLong Me.hwnd, GWL_WNDPROC, pOldWindPoc

    Demo attachment has been updated.
    Attached Files Attached Files
    Last edited by SeabrookStan; Aug 13th, 2020 at 09:27 PM.

  16. #16
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by SeabrookStan View Post
    Hi beic - I see the flicker when changing your form position.
    I tried wqweto suggestion on subclassing, and used iPrank’s code (https://www.vbforums.com/showthread....on-in-MDI-Form) to capture the repositioning event prior to form repainting. During that capture, the module1 repositions your Picture1. Flicker appears to be gone.

    There is minimal change to your code. All you need to add is the module1, and

    pOldWindPoc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)

    in your Form_Load event.
    You forgot Unhook in Unload.

  17. #17
    Junior Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    29

    Re: PictureBox flickering while Resizing the Form

    DaveDavis - thank you for noticing the missing un-hook. The post #15 and attachment have been updated with an un-hook during Form_Unload.

  18. #18

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by SeabrookStan View Post
    Hi beic - I see the flicker when changing your form position.
    I tried wqweto suggestion on subclassing, and used iPrank’s code (https://www.vbforums.com/showthread....on-in-MDI-Form) to capture the repositioning event prior to form repainting. During that capture, the module1 repositions your Picture1. Flicker appears to be gone.

    There is minimal change to your code. All you need to add is the module1, and

    pOldWindPoc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)

    in your Form_Load event.

    ===============================
    EDIT:
    AND add un-hook to Form_Unload event, such as:

    Private Sub Form_Unload(Cancel As Integer)
    ' Un-subclass the form
    SetWindowLong Me.hwnd, GWL_WNDPROC, pOldWindPoc

    Demo attachment has been updated.
    Wow, nice work, thank you!

    Btw... How can I use the same method on moving Child Form in MIDForm?

    Thank you!

  19. #19
    Junior Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    29

    Re: PictureBox flickering while Resizing the Form

    Hi beic- I don't have any experience with MDIForms, sorry about that. However, I imagine the principle would be the same, per form. I think you would have to 'hook' and 'un-hook' each form's resize (or reposition) event independently. And the Module would need to be re-written so that the Module would know which form to respond to. While the last solution I provided worked to remove the flicker, I admit it is inelegant. I think a nicer solution would be a general purpose module or class written to provide a generic "Pre-Resize" event, that could provide that service to any form for any similar purpose in the future. But before going through all that hassle, does the MDIForm Child even flicker?
    Last edited by SeabrookStan; Aug 16th, 2020 at 06:14 PM.

  20. #20

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: PictureBox flickering while Resizing the Form

    Quote Originally Posted by SeabrookStan View Post
    But before going through all that hassle, does the MDIForm Child even flicker?
    Yes, it can be seen while the Child are being resized and moved inside MDIForm using same functions ResizeForm and CenterForm inside my example project.

    Thank you for your support and effort!

    Kind regards,
    Viktor

Tags for this Thread

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