1 Attachment(s)
[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?
Attachment 178268
Thank you,
Kind regards,
Viktor
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
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
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?
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
LaVolpe
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
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
beic
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>
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
wqweto
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
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
beic
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>
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
wqweto
Do you mean resizing with maximize/restore buttons in upper right corner?
Yes, that's correct!
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>
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.
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
wqweto
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
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>
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...
1 Attachment(s)
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.
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
SeabrookStan
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.
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.
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
SeabrookStan
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!
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?
Re: PictureBox flickering while Resizing the Form
Quote:
Originally Posted by
SeabrookStan
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