.NET

Reply
Distinguished Contributor
HJohn1
Posts: 170
Registered: ‎09-26-2008
Message 1 of 11 (497 Views)

AutoCAD Application Focus

497 Views, 10 Replies
08-14-2013 08:30 AM

Is there a way to know when the main autocad application is on focus, the main application window is active.  I need to know when the user switches to any other application and when autocad is back on focus. I am trying to use the DocumentCollection events but I can't figure out when the main application is active.

Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 2 of 11 (488 Views)

Re: AutoCAD Application Focus

08-14-2013 08:52 AM in reply to: HJohn1

I got some tech support on this days ago.  pay forward.

 

 

In .Net you would use "Editor.StartUserInteraction" to begin selection. Disposing the "EditorUserInteraction" returned by "Editor.StartUserInteraction" will bring the focus back to your dialog.

 

Here is a blog post that you can refer to : 

 

http://adndevblog.typepad.com/autocad/2012/05/taking-mouse-inputs-from-a-modal-dialog-box.html

 

Learning as I go
Distinguished Mentor
BlackBox_
Posts: 773
Registered: ‎02-25-2013
Message 3 of 11 (475 Views)

Re: AutoCAD Application Focus

08-14-2013 10:07 AM in reply to: HJohn1

To the best of my knowledge, you need to hook SetWindowHooksEx, or Automation API's exposed global focus changed event.



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Distinguished Contributor
HJohn1
Posts: 170
Registered: ‎09-26-2008
Message 4 of 11 (440 Views)

Re: AutoCAD Application Focus

08-14-2013 09:19 PM in reply to: BlackBox_

BlackBox could you please explain a bit about


Automation API's exposed global focus changed event


Is there any way from within the Autocad .NET API to determine if AutoCAD is in focus?

Distinguished Mentor
BlackBox_
Posts: 773
Registered: ‎02-25-2013
Message 5 of 11 (428 Views)

Re: AutoCAD Application Focus

08-14-2013 10:54 PM in reply to: HJohn1
Firstly, I am no expert - I've never actually needed to do something like this, but found it to be an interesting topic.

As I'm on my iPhone, I'll have to check into this a bit more once I'm back at my dev laptop.

In a quick google search I came across this example, which instead uses a Timer.Elapsed event handler to query the active Process:

http://vbcity.com/forums/t/107487.aspx

I still think it would be far more efficient to instead hook an application-specifi. Event (if one does exist), but I wanted to at least offer something while away.

Cheers


"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Distinguished Contributor
HJohn1
Posts: 170
Registered: ‎09-26-2008
Message 6 of 11 (411 Views)

Re: AutoCAD Application Focus

08-15-2013 06:04 AM in reply to: BlackBox_

In VBA there is an object AcadApplication that has two events AppActivate and AppDeactivate.  I am looking for something like this in the .NET API. I asked about this on a previous post Application Events Help. However, the DocumentCollection doesn't let me know if the main application gets or loses the focus.

*Expert Elite*
norman.yuan
Posts: 998
Registered: ‎04-27-2009
Message 7 of 11 (404 Views)

Re: AutoCAD Application Focus

08-15-2013 06:44 AM in reply to: HJohn1

I am not sure I understand what you really want to know/do here. Say, user works with an AutoCAD session, and then turn to work with another application, suck as a wbe browser. Granted, AutoCAD window would lose focus. From this point on, only 2 way to get AutoCAD window gain the focus again: user click AutoCAD window again, or user double-clicks a *.dwg file in Windows Explorer to open it in AutoCAD. Either way is a direcct user action that causes AutoCAD window back to focus. So, my question is, why do you want to know when AutoCAD window has focus back? Or, say it in different way, do you want some other application that runs inside AutoCAD or outside AutoCAD to be notified when AutoCAD is back to focus? Could you explain it in a concrete user story?

 

 

 

Distinguished Contributor
HJohn1
Posts: 170
Registered: ‎09-26-2008
Message 8 of 11 (393 Views)

Re: AutoCAD Application Focus

08-15-2013 08:28 AM in reply to: norman.yuan

Thanks Norman for taking your time to look into this issue, I really appreciate it.  However, it seems to me that you are very clear about what I want/need to do, but you want to know why I want to do that.  One thing I can tell you, developing a program to spy on my coworkers (including myself) is not and will never be on my plans.  The IT department and perhaps NSA have enough tools to spy on everyone of us (it includes you too), they don't need my help. Do you know to catch when AutoCAD is active or not?  Thanks.

*Expert Elite*
norman.yuan
Posts: 998
Registered: ‎04-27-2009
Message 9 of 11 (384 Views)

Re: AutoCAD Application Focus

08-15-2013 09:43 AM in reply to: HJohn1

Ah, big brother wants to watch how CAD users work. It is not an uncommon thing to have something run behind the scene to monitor how AutoCAD is used for variety of purposes. As matter of fact, I have written a few AutoCAD usage monitering programs for different purpose.

 

For your specific requirement, ya, I have not found good event(s) in .NET API that can be used easily. However, if you do not mind to use COM API (the main drawback is it is more version dependent than .NET API code, I am not going to discuss it more here), you can easily handle AcadApplication.AppActivated/Deactivated events. I put together some quick code here (make sure to have Acad COM type library referenced (I use AutoCAD2012 here).

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Interop;

namespace AcadFocused
{
    public class AcadFocusWatcher
    {
        private static AcadApplication _app = null;

        public AcadFocusWatcher()
        {
            _app = Application.AcadApplication as AcadApplication;
   
            //Wait for the first focus losing event
            _app.AppDeactivate += app_AppDeactivate;
        }

        void app_AppDeactivate()
        {
            if (AcadLostFocus!=null)
            {
                AcadLostFocus(this, EventArgs.Empty);
            }

            _app.AppDeactivate -= app_AppDeactivate;
            _app.AppActivate+=app_AppActivate;
        }

        void app_AppActivate()
        {
            if (AcadGotFocus != null)
            {
                AcadGotFocus(this, EventArgs.Empty);
            }

            _app.AppActivate -= app_AppActivate;
            _app.AppDeactivate += app_AppDeactivate;
        }

        public event System.EventHandler AcadGotFocus;
        public event System.EventHandler AcadLostFocus;
    }
}

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(AcadFocused.MyCommands))]

namespace AcadFocused
{
    public class MyCommands
    {
        [CommandMethod("WatchFocus")]
        public static void WhatchAcadFocus()
        {
            AcadFocusWatcher watcher = new AcadFocusWatcher();
            watcher.AcadGotFocus += watcher_AcadGotFocus;
            watcher.AcadLostFocus += watcher_AcadLostFocus;
        }

        static void watcher_AcadLostFocus(object sender, EventArgs e)
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            dwg.Editor.WriteMessage("\n====================");
            dwg.Editor.WriteMessage("\nAcad lost focus!");
            dwg.Editor.WriteMessage("\n====================");

            //Log the time when focus is lost here

            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

        static void watcher_AcadGotFocus(object sender, EventArgs e)
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            dwg.Editor.WriteMessage("\n====================");
            dwg.Editor.WriteMessage("\nAcad got focus!");
            dwg.Editor.WriteMessage("\n====================");

            //Log the time when focus is obtained here

            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }
    }
}

To run this code and see the effect, I started AutoCAD, NETLOAD the compiled DLL, and entered command "WATCHFOCUS". Then I switch focus between AutoCAD and other apps (web browser, windows explorer, or whatever) by either cliccking on Acad window, or other app window. Attached is the output in AutoCAD text window, and you can see the code works well. Hope this helps.

 

Untitled.png

Distinguished Mentor
BlackBox_
Posts: 773
Registered: ‎02-25-2013
Message 10 of 11 (377 Views)

Re: AutoCAD Application Focus

08-15-2013 10:14 AM in reply to: norman.yuan

Thanks, Norman... I didn't think to look at AutoCAD COM, and assumed one would have to go to System for this.

 

Cheers



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.