MainWindow to System.Windows.Window

MainWindow to System.Windows.Window

ow
Enthusiast Enthusiast
5,032 Views
6 Replies
Message 1 of 7

MainWindow to System.Windows.Window

ow
Enthusiast
Enthusiast

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..

 

 

0 Likes
5,033 Views
6 Replies
Replies (6)
Message 2 of 7

Balaji_Ram
Alumni
Alumni

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

0 Likes
Message 3 of 7

Anonymous
Not applicable

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

 

0 Likes
Message 4 of 7

Anonymous
Not applicable

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
Mentor
Mentor

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?

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 7

Anonymous
Not applicable

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.

0 Likes
Message 7 of 7

MexicanCustard
Contributor
Contributor

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

0 Likes