Solved

.Net Windows form - Reduce screen flicker

Posted on 2006-06-16
22
1,185 Views
Last Modified: 2008-01-09
Hi,

  I am working on a windows application (VB.net). I noticed that there are flickers when the form open, and also, when I hit the tab key the first time, it cause flickers also.

  Can anyone tell me how to reduce screen flickers?

  Thanks in advance.
0
Comment
Question by:Friends
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
22 Comments
 
LVL 9

Expert Comment

by:tolgaong
ID: 16923797
try using doubleBuffer (.net 2005)
set it to true?
0
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16925755
If vb.net 2003, add the double buffering to Public Sub New() as shown below:

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

'ADD THIS:
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
            ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
    End Sub

'.................. Rest of form code
0
 

Author Comment

by:Friends
ID: 16933970

   Thank you for your input (both of you).

   Actually, I tried to set doublebuffer to true before, and it didn't work. (I am using Visual Studio 2005).  Also I am using MDI container for the application.

   
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16934866
1) How much processing are you doing with the form?

2) Any GDI+ stuff?

3) What type of controls are on the form?

Bob
0
 

Author Comment

by:Friends
ID: 16936060

The form holds a tab control with three tab pages. Each tab page holds some regular controls, such as labels, text boxes, buttons, comboboxes,  datagridview,etc. The flickers appear when the form loads (before any process involves);

also, after the form is loaded, the first time I press tab key to move focus, the flicker appears (after that, no flicker when tab key is pressed).

Regarding DGI, I am using following code to paint the the page (to change to background color):

Private Sub tabpage1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles tabpage1.Paint

Dim rec As Rectangle = New Rectangle(0, 0, tabpage1.Width, tabpage1.Height)
        Dim brush As LinearGradientBrush = New LinearGradientBrush(rec, Color.White, Color.FromArgb(72, 125, 204), 90)
        e.Graphics.FillRectangle(brush, rec)
end sub

Thanks .

0
 

Author Comment

by:Friends
ID: 17061426
Hi,

  If there is no response for this question, I am going to ask the administrator to close the question.

  Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17062193
You might be able to use the WinAPI SetRedraw to control the flicker.

Bob
0
 

Author Comment

by:Friends
ID: 17072443
Can you provide examples (sample code) for using SetRedraw?

Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17072467
0
 

Author Comment

by:Friends
ID: 17090709
Bob, thanks for the link.

I am not familar with winapi, should I put the code in my parent form? I tried, but it has problem with the first line code(I am using vb.net).

Any suggestions?

Thanks.

  <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
    End Function

    Private Const WM_SETREDRAW As Integer = &HB

    Private m_canRedraw As Boolean = True
    <Category("Behavior"), _
     Description("Get/set whether the control will repaint itself.")> _
    Public Property Redraw() As Boolean
      Get
        Return m_canRedraw
      End Get
      Set(ByVal Value As Boolean)
        If Value <> m_canRedraw Then
          Me.SendMessage(Me.Handle, WM_SETREDRAW, Value, 0)
        End If
        m_canRedraw = Value
      End Set
    End Property  'Redraw'
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17090979
This is the pertinent code:

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
End Function

' Turn off redraw
Me.SendMessage(control.Handle, WM_SETREDRAW, False, 0)

' Do something here

' Turn on redraw
Me.SendMessage(control.Handle, WM_SETREDRAW, True, 0)

Bob
0
 

Author Comment

by:Friends
ID: 17091349
Bob,

  I put the code on my parent form, it gave me error "DllImport is not defined", do I need to add some kind reference in order to user DLLImport"?

  ALso, where should I put those code to turn off and or redraw?

  Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17091912
1) Forgot this line at the top:
  Imports System.Runtime.InteropServices

2) There is no hard and fast rule about turning on/off redraw, so you will have to give me some detailed background on what you are trying to accomplish.

3) It sounds like turning off redraw for the form on Form_Load, and then turning on redraw in the Form_Activated might be a good place, but it is impossible to tell without more information.

Bob
0
 

Author Comment

by:Friends
ID: 17092933
Bob,

  Now it gave me the error "System.Runtime.InteropService DllImport Attribute cannot be applied to instance method.

below is the simple code I have:

Imports System.Runtime.InteropServices

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
    End Function

End Class


Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17092990
Try adding the Shared keyword:

DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      ' Turn off redraw
      Me.SendMessage(Me.Handle, WM_SETREDRAW, False, 0)

    End Sub


It's strange that I haven't seen that exception.  Are you using 2003 or 2005?

Bob
0
 

Author Comment

by:Friends
ID: 17093507
Bob,

  I am using 2005.

  Now I am getting the error says "WM_SETREDRAW" is not declared.

  Should I import something?
 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17093692
Oops, not my best day:

   Private Const WM_SETREDRAW As Integer = &HB

Bob
0
 

Author Comment

by:Friends
ID: 17149491
Bob,

  Sorry for the late response. I was busy with something else for a while.

  I tried the way you suggested, but seems that it didn't work. It changed the way the screen displays, but flicker is still there.

  Someone told me that the flicker was caused by the MDI, what do you think?  If MDI causes flickers, what can be done to improve it?

  Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17150961
Are you wrapped around the axle with MDI forms?  I am not a big fan of them, and I have found ways around them, such as embedded child forms.

Bob
0
 

Author Comment

by:Friends
ID: 17153495

What do you mean by "around the axle with MDI forms"?  


My application has a main form. On the main form, there is a side menu bar with several function buttons. When a function button is clicked, the corresponding MDI child form will show (and other MDI children will hide). During this show and hide process, the flickers appear (especially the first time).
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 300 total points
ID: 17153589
How meant how heavily invested are you in using it?  Would you be willing to examine other possibilities?

Example:

Dim f As New Form2
f.TopLevel = False
f.Parent = Me
f.Show()

The main form is not an MDI container, and you have so much more control over things like the background color for the form, how menus are handled, etc.

Bob
0
 

Author Comment

by:Friends
ID: 17179416
Bob, I think maybe you are right, maybe I should try other possibilities.

Anyway, thanks for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question