Solved

MDI: Change CTRL-Tab behaviour

Posted on 2008-10-21
6
1,568 Views
Last Modified: 2013-12-17
Hi experts

How can I change the ctrl-tab behaviour of a MDI-Container.
Normally it just "loops" through all the mdi-children, but need to exclude some of them.
Where and how can I change this behaviour?

I'm using .NET 2.0, VS2008, c# and WinForms
0
Comment
Question by:Arikael
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22768537
Hi Arikael,

In your MdiParent Form, the gray area where the MdiChild forms appear is known as the MdiClient window.  Whenever you press Ctrl-Tab, this window will get a WM_MDINEXT message which causes it to select the next MdiChild.

See WM_MDINEXT:
http://msdn.microsoft.com/en-us/library/ms644918(VS.85).aspx

In my example below, whenever an MdiChild of Type Form2 is selected as a result of a Ctrl-Tab, I send another WM_MDINEXT message causing the next MdiChild to be selected.  This technically doesn't SKIP Form2 since it is actually selected for a split second before the next MdiChild gets selected.

Sorry that it is in VB.Net...I'm still coming to grips with coding in C#.  =\

Hopefully you can make sense of what is going on...
Public Class Form4
 
    Private client As MyMdiClient
 
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is MdiClient Then
                client = New MyMdiClient(ctl, Me)
                Exit For
            End If
        Next
 
        Dim f1 As New Form1
        f1.MdiParent = Me
        f1.Show()
 
        Dim f2 As New Form2
        f2.MdiParent = Me
        f2.Show()
 
        Dim f3 As New Form3
        f3.MdiParent = Me
        f3.Show()
    End Sub
 
    Private Class MyMdiClient
        Inherits NativeWindow
 
        Private MyMdiParent As Form
        Private Const WM_MDINEXT As Integer = &H224
 
        Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" _
            (ByVal handle As IntPtr, ByVal wMsg As Integer, _
            ByVal wParam As Integer, ByVal lParam As Integer) As Integer
 
        Public Sub New(ByVal client As MdiClient, ByVal parent As Form)
            Me.AssignHandle(client.Handle)
            Me.MyMdiParent = parent
        End Sub
 
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_MDINEXT
                    Debug.Print("WM_MDINEXT")
                    Debug.Print("Before: " & Me.MyMdiParent.ActiveMdiChild.Name)
 
                    MyBase.WndProc(m)
                    Application.DoEvents()
 
                    Debug.Print("After: " & Me.MyMdiParent.ActiveMdiChild.Name)
 
                    ' Skip over to the next MdiChild if the current one is of Type Form2
                    If TypeOf Me.MyMdiParent.ActiveMdiChild Is Form2 Then
 
                        ' *** BE CAREFUL! ***
 
                        ' If all the MdiChildren are of Type Form2 then this
                        ' will cause an infinite loop...
 
                        SendMessage(Me.Handle, WM_MDINEXT, 0, 0)
                    End If
                    Exit Sub
 
            End Select
            MyBase.WndProc(m)
        End Sub
 
    End Class
 
 
End Class

Open in new window

0
 
LVL 3

Author Comment

by:Arikael
ID: 22773677
thanks, I will have a look at it today
0
 
LVL 3

Author Comment

by:Arikael
ID: 22774580
here is the code I used (c#)
0x0224 is 548 in the decimal system.
And I put the code into the MdiParent, into my class from which every MdiChild inherits but the code within

if(m.Msg == WM_MDINEXT)

never gets executed, because the m.Msg is never 548.
Any ideas why this could happen?
private const int WM_MDINEXT = 0x0224;
 
protected override void WndProc(ref Message m)
        {
            System.Diagnostics.Debug.WriteLine(m.Msg);
            if(m.Msg == WM_MDINEXT)
            {
                base.WndProc(ref m);
 
                if(this.ActiveMdiChild is MetaDetail)
                {
                    Application.DoEvents();
                    PostMessage(this.Handle, WM_MDINEXT, 0, 0);
                }
            }
 
            base.WndProc(ref m);
        }
 
        [DllImport("user32.dll")]
        public static extern System.IntPtr PostMessage(System.IntPtr hwnd, int msg, int wParam, int lParam);

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22776541
As I said before, the WM_MDINEXT message gets sent to the MdiClient window (the gray area in the MdiParent).  This is a child window of the main MdiParent.  If you look in the Load() event, I am iterating over the Controls() collection looking for this control:

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is MdiClient Then
                client = New MyMdiClient(ctl, Me)
                Exit For
            End If
        Next
    End Sub

I pass the MdiClient to an instance of the MyMdiClient() class...which Inherits from NativeWindow().  So in my example, WndProc() is actually for the MdiClient ONLY and not the entire MdiParent.

I'll code up a working example in C# 2005 Express today if you can't figure it out.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 22777990
Here is a working C# version:  (C# 2005 Express)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
namespace WindowsApplication1
{
    public partial class frmMdiParent : Form
    {
        public frmMdiParent()
        {
            InitializeComponent();
        }
 
        private MyMdiClient client = null;
 
        private void frmMdiParent_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is MdiClient)
                {
                    this.client = new MyMdiClient((MdiClient)ctl, this);
                }
            }
 
            Form1 f1 = new Form1();
            f1.MdiParent = this;
            f1.Show();
 
            Form2 f2 = new Form2();
            f2.MdiParent = this;
            f2.Show();
 
            Form3 f3 = new Form3();
            f3.MdiParent = this;
            f3.Show();
        }
 
        private class MyMdiClient : NativeWindow
        {
 
            private Form MyMdiParent = null;
            private const int WM_MDINEXT = 0x224;
 
            [DllImport("user32.dll")]
            public static extern int PostMessage(IntPtr handle, int msg, int wParam, int lParam);
            
            public MyMdiClient(MdiClient mdiClient, Form mdiParent)
            {
                this.AssignHandle(mdiClient.Handle);
                this.MyMdiParent = mdiParent;
            }
 
            protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case WM_MDINEXT:
                        Debug.Print("WM_MDINEXT");
                        Debug.Print("Before: " + this.MyMdiParent.ActiveMdiChild.Name);
 
                        base.WndProc(ref m);
                        Application.DoEvents();
 
                        Debug.Print("After: " + this.MyMdiParent.ActiveMdiChild.Name);
 
                        // Skip over Form2 when Ctrl-Tab is pressed
                        if (this.MyMdiParent.ActiveMdiChild is Form2)
                        {                            
                            PostMessage(this.Handle, WM_MDINEXT, 0, 0);
                        }
 
                        return;
 
                }
 
                base.WndProc(ref m);
            }
 
        }
 
    }
}

Open in new window

0
 
LVL 3

Author Comment

by:Arikael
ID: 22783564
ah, sorry, I didn't unterstand the term MdiClient at first.
Now everything works as expected

I changed the code in MdiParent to (see code below), client was not defined in the c# code.

P.S. I raised the points to 500, thank you very much for your help :-)
  MyMdiClient client;
 
            foreach(Control ctl in this.Controls)
            {
                if(ctl is MdiClient)
                {
                    client = new MyMdiClient((MdiClient)ctl, this);
                    break;
                }
            }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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