Click here to Skip to main content
15,868,306 members
Articles / Desktop Programming / Windows Forms
Article

Workaround for flicker/flashing when programmatically activating MDI child forms

Rate me:
Please Sign up or sign in to vote.
4.83/5 (19 votes)
8 Jul 2007CPOL2 min read 116.6K   33   26
This short class demonstrates how to activate an MDI child form without the fireworks that you see if you just set the ActiveMdiChild property of the MDI parent form.

Introduction

This article shows how to activate an MDI child form maximized, without the fireworks that erupt when you activate an MDI child form using the Active() or Select() methods of the child form.

Background

If you've used MDI in WinForms or Win32, you've probably come across this very annoying issue, where MDI child forms that are activated through code, paint themselves in the restored window state they always have when they are non-active, just before they are activated and maximized (this happens only when the current MDI child form is maximized).

Because this issue was so annoying, and resulted in a very unprofessional 'look and feel', I became determined to find a workaround. The one I found to work best involves no subclassing or message handling. I discovered this when I noticed that if I activate the next MDI child form via Ctrl+F6 or the MDI system menu, there was none of the usual fireworks that I was seeing when a child form is activated by calling its Activate() method.

That lead me to the WM_MDINEXT Windows message which, at first glance, seemed only useful for activating the next MDI child form in the sequence. However, it can be used to activate any MDI child form by passing the handle of the next or previous MDI child in the Z-order along with a flag telling it whether to activate the next or previous MDI child in the Z-order.

Using the code

The following sample MDI parent Form class implements the workaround via a method that can be called and passed the MDI child form to be activated:

C#
//  
//  MyMDIParent.cs
//

using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyNamespace
{
   public partial class MyMDIParent : Form
   {
      public MyMDIParent()
      {
         InitializeComponent();
      }

      public void ActivateMdiChild( Form childToActivate )
      {
         if( this.ActiveMdiChild != childToActivate )
         {
            MdiClient mdiClient = GetMDIClient();
            int count = this.MdiChildren.Length;
            Control form = null;  // next or previous MDIChild form

            int pos = mdiClient.Controls.IndexOf( childToActivate );
            if( pos < 0 )
               throw new InvalidOperationException( "MDIChild form not found" );
            if( pos == 0 )
               form = mdiClient.Controls[1];  // get next and activate previous

            else
               form = mdiClient.Controls[pos - 1];  // get previous and activate next


            // flag indicating whether to activate previous or next MDIChild
            IntPtr direction = new IntPtr( pos == 0 ? 1 : 0 );
            
            // bada bing, bada boom
            SendMessage( mdiClient.Handle, WM_MDINEXT, form.Handle, direction );
         }
      }

      public MdiClient GetMDIClient()
      {
         foreach( Control c in this.Controls )
         {
            if( c is MdiClient )
               return (MdiClient) c;
         }
         throw new InvalidOperationException( "No MDIClient !!!" );
      }

      [System.Security.SuppressUnmanagedCodeSecurity]
      [DllImport( "user32.dll", CharSet = CharSet.Auto )]
      public static extern IntPtr SendMessage( IntPtr hWnd, int msg, 
                                  IntPtr wParam, IntPtr lParam );

      public const int WM_MDINEXT = 0x224;
   }
}

Points of interest

This solution works fine for me, when I need to activate a child window via code, but I haven't looked at how to get the MDI child form menu entries inserted into the menu by the framework, to use it. Any insight or advice on that would be welcome.

History

  • Initial submission - 7/09/07.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
zerochangjj4-Nov-12 16:07
zerochangjj4-Nov-12 16:07 
QuestionMemory management in MDI forms Pin
Ali91Asadi28-Sep-12 5:40
Ali91Asadi28-Sep-12 5:40 
GeneralThank you Pin
ajhuddy28-Oct-09 10:34
ajhuddy28-Oct-09 10:34 
GeneralMy vote of 1 Pin
Jon Burchel31-Aug-09 10:50
Jon Burchel31-Aug-09 10:50 
General[My vote of 1] CRAP Pin
Jon Burchel31-Aug-09 10:50
Jon Burchel31-Aug-09 10:50 
Questionchanging vs form look Pin
Member 46116445-Mar-09 18:44
Member 46116445-Mar-09 18:44 
GeneralSimilar Problem Pin
johannesnestler7-May-08 22:13
johannesnestler7-May-08 22:13 
GeneralRe: Similar Problem Pin
ravtos8-Dec-08 3:48
ravtos8-Dec-08 3:48 
GeneralRe: Similar Problem Pin
johannesnestler8-Dec-08 22:51
johannesnestler8-Dec-08 22:51 
QuestionRe: Similar Problem Pin
Member 46116445-Mar-09 18:47
Member 46116445-Mar-09 18:47 
"if you want to draw a nice background for yourself etc..."
please rovide more details
thanks
Yosi
AnswerRe: Similar Problem Pin
johannesnestler6-Mar-09 5:25
johannesnestler6-Mar-09 5:25 
GeneralRe: Similar Problem Pin
jasonhh14-Apr-09 20:05
jasonhh14-Apr-09 20:05 
QuestionI have a question please Pin
sdev5-Oct-07 8:13
sdev5-Oct-07 8:13 
GeneralFW: &quot;A flicker issue in MDI applications&quot; Pin
Marcus Deecke17-Aug-07 11:54
Marcus Deecke17-Aug-07 11:54 
GeneralVery good - looking forward to the MDI menu working Pin
Martin Berriman26-Jul-07 1:34
Martin Berriman26-Jul-07 1:34 
GeneralRe: Very good - looking forward to the MDI menu working [modified] Pin
tonyt18-Aug-07 14:37
tonyt18-Aug-07 14:37 
Generalstill MDI Questions Pin
User 151565622-Aug-07 14:59
User 151565622-Aug-07 14:59 
GeneralRe: Very good - looking forward to the MDI menu working Pin
jefrubio19-Feb-09 8:01
jefrubio19-Feb-09 8:01 
GeneralRe: Very good - looking forward to the MDI menu working Pin
sprice8629-May-12 0:20
professionalsprice8629-May-12 0:20 
Generalconverted to VB and adjusted Pin
dmbf1b517-Jul-07 10:27
dmbf1b517-Jul-07 10:27 
GeneralRe: converted to VB and adjusted Pin
Jason Newland16-Feb-09 23:52
Jason Newland16-Feb-09 23:52 
GeneralRe: converted to VB and adjusted [modified] Pin
Jordan Deyton15-Jul-09 3:38
Jordan Deyton15-Jul-09 3:38 
Questionhow to SendMessage in VB.net? Pin
dmbf1b517-Jul-07 5:21
dmbf1b517-Jul-07 5:21 
AnswerRe: how to SendMessage in VB.net? Pin
CalifBreton17-Jul-07 8:33
CalifBreton17-Jul-07 8:33 
GeneralNever seen any fireworks.... Pin
KellyLeahy9-Jul-07 6:21
KellyLeahy9-Jul-07 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.