Question

Fixing third party controls that use user32.dll

public partial class Form1 : Form
{
    TheForm theForm;//empty form
    public Form1()
    {
        InitializeComponent();
        theForm = new TheForm();
    }

    internal const int SWP_SHOWWINDOW = 0x0040;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    internal static extern int ShowWindow(IntPtr hWnd, short cmdShow);        

    private void button1_Click(object sender, EventArgs e)//shows theForm when it wants to
    {
        ShowWindow(theForm.Handle, SWP_SHOWWINDOW);
    }

    private void button2_Click(object sender, EventArgs e)//shows theForm always
    {
        theForm.Show();
    }
}

Why after call user32 ShowWindow, most of times window "stuck in limbo"? It captures losing of focus and disposes, but doesn't capture mouse events and doesn't paint itself.

Was it helpful?

Solution

Im no expert, but i think you need to show your form by calling a Show() method outside of the button click event handler methods. if theres no form to see, you cant click a button to show the form.

Im not sure theres a reason to use user32.dll in C# to make winforms applications. Because this is done for you by C#, in a mannor of speaking.

All you need to do is allow visual studio to create the form code for you in the drag and drop forms designer. Then to show and use your form without creating a new source file just for your Main() method, just write the Main() method right there in the generated code.

Heres a great article about using the windows forms designer.

http://msdn.microsoft.com/en-us/library/360kwx3z(v=vs.90).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top