views:

235

answers:

0

Hello all,

Following on the debate found at Cursor.Current vs. this.Cursor in .Net (C#), I've added to my NativeMethods class the following declarations:

static class NativeMethods
{
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

    public static void SwitchWaitCursor(IntPtr handle)
    {
        Application.UseWaitCursor = !Application.UseWaitCursor;
        SendMessage(handle, 0x20, handle, (IntPtr)1);
    }

    /*...*/
}

This is being used to give me a wait cursor when calling a Windows.Form that needs a while to process data before loading and displaying. The usage pattern is something like this:

// Calling the slow form
NativeMethods.SwitchWaitCursor(this.Handle);
using (SlowForm diag = new SlowForm())
{
    diag.ShowDialog(this);
}

// OnShow() of the SlowForm
private void SlowForm_Shown(object sender, EventArgs e)
{
    NativeMethods.SwitchWaitCursor(this.Handle);
}


It is working exactly as expected. However, I do have one thought nagging me. Is it alright for this SendMessage usage pattern, for hWnd to refer to two different windows?