Ryan
Ryan

Reputation: 8005

Subclass a ParentForm WndProc from a Control

I am integrating a customized MenuStrip control into another .NET application. The MenuStrip works fine except for a focus issue. If the form hosting the MenuStrip is not focused, the user is required to click twice - once to activate the form and another time to select the menu items. The host form is a part of the separate .NET application which I cannot modify. I can only modify my control's source code.

I found a solution at the WinForm level which overrides the Form WndProc method, except I do not have source code access to the WinForm nor can I recompile the host application and form.

Is there a way to subclass the ParentForm of a Control, so that it automatically Activates the ParentForm if it is not focused?

Upvotes: 3

Views: 1824

Answers (1)

Ryan
Ryan

Reputation: 8005

I was able to use ContainerControl.ParentForm to get a reference to the host form, however in the production environment the ParentForm returns null and I still need to find a solution. I put the NewWndProc handlers in try/catch just in case it threw any exceptions although I'm not sure what (if anything) it could throw. I may have to just use Win32 functions and not use the Form.Focused and Form.Activate() .NET methods. Anyway, here is the code:

public class FocusFormWrapper
{
    #region DllImport
    public const int GWL_WNDPROC = (-4);
    public const UInt32 WM_CLOSE = 0x0010;
    public const UInt32 WM_PARENTNOTIFY = 0x0210; 

    public delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindow(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string _ClassName, string _WindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    #endregion DllImport

    private Form myForm = null;
    private IntPtr hWndTarget = IntPtr.Zero;
    private IntPtr oldWndProc = IntPtr.Zero;
    private WndProcDelegate newWndProc;

    private FocusFormWrapper()
    {
    }

    public FocusFormWrapper(Form sourceForm)
    {
        if (sourceForm == null)
            throw new ArgumentNullException("sourceForm");
        if (!IsWindow(sourceForm.Handle))
            throw new ArgumentException("sourceForm IsWindow failed");

        myForm = sourceForm;
        hWndTarget = myForm.Handle;
        AddSubclass();
    }

    ~FocusFormWrapper()
    {
        RemoveSubclass();
        myForm = null;
        newWndProc = null;
    }

    private int AddSubclass()
    {
        int result = -1;
        if (myForm != null && newWndProc == null)
        {
            newWndProc = new WndProcDelegate(NewWndProc);
            oldWndProc = GetWindowLong(hWndTarget, GWL_WNDPROC);
            result = SetWindowLong(hWndTarget, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(newWndProc));
        }
        return result;
    }

    public int RemoveSubclass()
    {
        int result = -1;
        if (myForm != null && newWndProc != null)
        {
            result = SetWindowLong(hWndTarget, GWL_WNDPROC, oldWndProc);
            newWndProc = null;
        }
        return result;
    }

    public IntPtr NewWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        try
        {
            if (msg == WM_PARENTNOTIFY && !myForm.Focused)
            {
                // Make this form auto-grab the focus when menu/controls are clicked 
                myForm.Activate();
            }
            if (msg == WM_CLOSE)
            {
                RemoveSubclass();
            }
        }
        catch
        {
        }

        return CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam);
    }
}

Upvotes: 2

Related Questions