.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MainWindow to System.Windows.Window

6 REPLIES 6
Reply
Message 1 of 7
ow
Contributor
3840 Views, 6 Replies

MainWindow to System.Windows.Window

I'm trying to set the OwnerWindow-proerty on our License system to ensure that any dialog from the license system is displayed in the foreground. This system expects a System.Windows.Window object, but AutoCAD only provides a Autodesk.AutoCAD.Windows.Window-property as far as I can see (Application.MainWindow)

 

So I've tried casting it, to no luck, and I've tried to convert it using: HwndSource.FromHwnd(Application.MainWindow.Handle); -But it returns null.

 

Is there another way this can be done? 

 

(I have one work-around wich uses Application.ShowModelessWindow on a temp wpf window, which in turn is used as owner window for the license system, but it's less than ideal..

 

 

6 REPLIES 6
Message 2 of 7
Balaji_Ram
in reply to: ow

Sorry for the delay.

 

AutoCAD main window is MFC based the .Net API is a wrapper around it which provides access to the window.

 

So, type casting to System.Windows.Window is not valid.

 

The workaround that you already mentioned which uses a WPF window is the way to get around it.

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 7
daniel.balogh
in reply to: Balaji_Ram

Hello,

 

I'm also facing this problem in another context. I'm writing Plugins for Acad, which use WPF windows as UI. If I set the "Topmost" property of my WPF-Window, it stays on top of everything, since I cannot declare the acad Window as parent/owner - i guess.

So with the plugin runnig and opening an Explorer Window this will be overlaid by my form... This is annoying and not really professional.... 😞

I'd like to show it on top of Acad only (modeless windows, though) but not on top of anything else....

Is there really no way, to show those Windows in the expected manner? Or am I just missing something?

Any hints are appreciated.

Thanks,

Daniel

 

Message 4 of 7
khoa.ho
in reply to: daniel.balogh

Since AutoCAD main window is MFC based which is native window, we should use NativeWindow class to wrap it in .NET and set it is a parent of your plugin window. The following code will help:

 

private void ShowModelessWindow()
{
    // Replace with your plugin WPF window
    var pluginForm = new System.Windows.Forms.Form();

    // Get AutoCAD main window handle
    IntPtr acadHandle = Application.MainWindow.Handle;

// Create a new .NET native window wrapper of AutoCAD var nativeWindow = new System.Windows.Forms.NativeWindow(); nativeWindow.AssignHandle(acadHandle); // Show the plugin on top of AutoCAD pluginForm.Show(nativeWindow); }

 

Message 5 of 7
norman.yuan
in reply to: khoa.ho

Wouldn't the "Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog/Window()" suppose to show a window floting on top of AutoCAD main window, but not on top other windows applications?

Message 6 of 7
khoa.ho
in reply to: norman.yuan

Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog is a built-in method in AutoCAD. While NativeWindow.FromHandle (static method of (new NativeWindow()).AssignHandle) is the generic method to show a modeless form on top of a specified host application. I guess they will work the same. If not, try the generic method.

Message 7 of 7
MexicanCustard
in reply to: khoa.ho

To cast Application.MainWindow.Handle to a Win32 HWND you can use the following class

 

using System;
using Autodesk.AutoCAD.ApplicationServices.Core;

namespace Utility
{
    class AcadMainWindow : System.Windows.Forms.IWin32Window
    {
        public IntPtr Handle
        {
            get { return Application.MainWindow.Handle; }
        }
    }
}

 

Then you can use a new AcadMainWindow() object in place of any MFC/Win32 HWND

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost