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.
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
To the best of my knowledge, you need to hook SetWindowHooksEx, or Automation API's exposed global focus changed event.
"How we think determines what we do, and what we do determines what we get."
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?
"How we think determines what we do, and what we do determines what we get."
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.
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?
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.
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.
Thanks, Norman... I didn't think to look at AutoCAD COM, and assumed one would have to go to System for this.
Cheers
"How we think determines what we do, and what we do determines what we get."
Thank you Norman for your help and "contribution to make the workplace a much friendly and transparent environment" . I really hoped for a native .NET API solution, too bad there is not one available.