-1

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.

1 Answers1

1

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

kbzombie
  • 322
  • 2
  • 12
  • The reason is i'm trying to fix third party controls, and honestly have no idea why the heck they do it that way. – Rusty Shackelford Nov 16 '12 at 05:17
  • Depending on what there trying to do and how they need to do it, it would be easier on everyone if you could convince them to ditch the dll's. then you could work some magic and have some fun with the winforms designer. its a win win situation! :) – kbzombie Nov 16 '12 at 05:25
  • I have full sources of it, and our suddenly broken project need to fix it. I'm tired and crying btw. – Rusty Shackelford Nov 16 '12 at 05:33
  • @RustyShackelford: do you think you can replace all the calls to `ShowWindow` with proper .NET calls? Please tell me they aren't also using user32 calls for the rest of the UI. – siride Nov 16 '12 at 14:42