Solved

.Net Windows form - Reduce screen flicker

Posted on 2006-06-16
22
1,150 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
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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now