locked
Restrict Form within Excel application bounds RRS feed

  • Question

  • I am trying to popup a SizableToolWindow (Notification.cs) within an Excel application, but constrain it to within the bounds of the Excel application... at the present the form can be moved outside of the application.

    This is what I have:

    Code Block

    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {

    Notification notification = new Notification();


         NativeWindow xlWindow = new NativeWindow();

         xlWindow.AssignHandle(new IntPtr(this.Application.Hwnd));
                

         notification.Show(xlWindow);

         xlWindow.ReleaseHandle();

    }


    This does half of what I want, in that it keeps the notification window on top of this Excel application, but the notification window can still be moved outside of the main Excel app.

    I have seen a .NET Excel application (no access to source code) that does this so I know it's possible, but I can't work out how to do it.

    I have tried setting the .MdiParent property of the notification window, but haven't worked out how to do it, or if it would work.

    Cheers
    Sunday, January 20, 2008 11:06 PM

Answers

  • Hi,

     

    You need to use Windows API SetParent to set xlWindow to be the parent window of your notification window. Codes I wrote worked fine on my end.

    For your information:

     

    Code Block

            [DllImport("user32.dll")]
            private static extern int SetParent(IntPtr chwnd, int phwnd);


            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Form1 fm = new Form1();
                fm.Show();
                SetParent(fm.Handle, this.Application.Hwnd);           
            }

     

     

     

     

    Thanks

    Ji

     

    Tuesday, January 22, 2008 3:29 AM

All replies

  • Hi,

     

    You need to use Windows API SetParent to set xlWindow to be the parent window of your notification window. Codes I wrote worked fine on my end.

    For your information:

     

    Code Block

            [DllImport("user32.dll")]
            private static extern int SetParent(IntPtr chwnd, int phwnd);


            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Form1 fm = new Form1();
                fm.Show();
                SetParent(fm.Handle, this.Application.Hwnd);           
            }

     

     

     

     

    Thanks

    Ji

     

    Tuesday, January 22, 2008 3:29 AM
  • Hi,

    I had given up on this problem several months ago, today I decided to give it one more go and stumbled upon this answer to my original post which works perfectly, so thanks!!

    One more thing, how do I center the child form to the parent using this method. I've tried setting the StartPosition property to CenterParent but that doesn't work.

    Any ideas?


    Tuesday, April 29, 2008 11:44 PM