Is there an equivalent of Mac OS X Document modal sheet in .NET?
Asked Answered
G

2

2

My application has been getting more and more requests to have certain dialogs behave similar to Mac OS X Document modal Sheet functionality, where a dialog is modal to just the parent control/dialog, and not the whole application (see http://en.wikipedia.org/wiki/Window_dialog).

Current windows ShowDialog() is insufficient for the needs of my application, as I need to have a dialog be modal to another dialog in the application, but still allow the user to access other areas of the application.

Is there an equivalent to Document modal Sheet in C# .NET? Or even a close implementation someone has done, or am I on my own to try and implement this functionality? I tried searching Google and SO to no avail.

Thanks,

Kyle

Guyette answered 12/10, 2011 at 13:43 Comment(0)
G
2

After revisiting this issue, I did some digging and found a hybrid solution that will work for my needs.

I took the suggestion by p-daddy: https://mcmap.net/q/301644/-is-it-possible-to-use-showdialog-without-blocking-all-forms

And I modified the code to work for 32-bit and 64-bit compiles using the suggestion by hans-passant: https://mcmap.net/q/304019/-how-do-i-pinvoke-to-getwindowlongptr-and-setwindowlongptr-on-32-bit-platforms

The result is the following:

const int GWL_STYLE   = -16;
const int WS_DISABLED = 0x08000000;

public static int GetWindowLong(IntPtr hWnd, int nIndex)
{
    if (IntPtr.Size == 4)
    {
        return GetWindowLong32(hWnd, nIndex);
    }
    return GetWindowLongPtr64(hWnd, nIndex);
}

public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong)
{
    if (IntPtr.Size == 4)
    {
        return SetWindowLong32(hWnd, nIndex, dwNewLong);
    }
    return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
private static extern int GetWindowLong32(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]
private static extern int GetWindowLongPtr64(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
private static extern int SetWindowLongPtr64(IntPtr hWnd, int nIndex, int dwNewLong);


public static void SetNativeEnabled(IWin32Window control, bool enabled)
{
    if (control == null || control.Handle == IntPtr.Zero) return;

        NativeMethods.SetWindowLong(control.Handle, NativeMethods.GWL_STYLE, NativeMethods.GetWindowLong(control.Handle, NativeMethods.GWL_STYLE) &
            ~NativeMethods.WS_DISABLED | (enabled ? 0 : NativeMethods.WS_DISABLED));
}

public static void ShowChildModalToParent(IWin32Window parent, Form child)
{
    if (parent == null || child == null) return;

    //Disable the parent.
    SetNativeEnabled(parent, false);

    child.Closed += (s, e) =>
    {
        //Enable the parent.
        SetNativeEnabled(parent, true);
    };

    child.Show(parent);
}
Guyette answered 18/4, 2013 at 16:45 Comment(0)
D
1

The Form.ShowDialog method allows you to specify an owner when you call it. In this case the form is modal only to the given owner.

EDIT: I tried this with mixed results. I created a simple Windows Forms app with a main form, and two others. From a button click on the main form, I opened Form2 using the Show method. Form2 has a button on it as well, and when clicked, I opened Form3 using the ShowDialog method, passing in Form2 as it's owner. While Form3 did seem to be modal to Form2, I could not switch back to Form1 until I closed Form3.

Dragon answered 12/10, 2011 at 14:33 Comment(4)
It still blocks the application though doesn't it. The poster is asking for a way to have it just block one window of the application while allowing other windows from the same application to continue processing normally.Orientate
tcarvin is correct. I will update my question with more details.Guyette
Try opening a second form (Form2a) from the main form, using Show, then proceed as before. See if you can switch to Form2a.Wilburnwilburt
@John I was unable to switch to the second form until I closed the modal dialog.Dragon

© 2022 - 2024 — McMap. All rights reserved.