How to set a focus to AutoCAD main window from my form of C# .NET plug-in?

How to set a focus to AutoCAD main window from my form of C# .NET plug-in?

Anonymous
Not applicable
14,165 Views
14 Replies
Message 1 of 15

How to set a focus to AutoCAD main window from my form of C# .NET plug-in?

Anonymous
Not applicable

Hello,

 

How to switch focus to AutoCAD document window from my form of C# .NET plug-in.

After user pressed the button, I invite him to input a point, but my form still on top.

And it is nessesary to press minimize button, next to set a Focus on Autocad window, then to pick a point.

It is not handy enough.

 

I did not find a property to set a focus in class of Application.MainWindow.

How can I do it?

 

Thank you, in advance.

 

0 Likes
Accepted solutions (1)
14,166 Views
14 Replies
Replies (14)
Message 2 of 15

SENL1362
Advisor
Advisor
:::Form1.cs:::
...
private void Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void Select_Click(object sender, EventArgs e)
{
Document doc = Acad.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (EditorUserInteraction preventFlikkering = ed.StartUserInteraction(this))
{
PromptPointOptions getPntPrmpt = new PromptPointOptions("\nGet Center");
//Modal Form: will hide because of ed.GetPoint
PromptPointResult getPnt = ed.GetPoint(getPntPrmpt);
ed.WriteMessage("\nPoint: {0}", getPnt.Value);
ed.Regen();
preventFlikkering.End();
}
}
0 Likes
Message 3 of 15

Anonymous
Not applicable

Thanks, but I'm using modeless autocad dialog method.

Is there explicit call of any method to change a focus?

0 Likes
Message 4 of 15

SENL1362
Advisor
Advisor
[DllImport("user32.dll")]
private static extern System.IntPtr SetFocus(System.IntPtr hwnd);
...

//For Modeless Forms
System.IntPtr hwnd = adsw_acadDocWnd();
SetFocus(hwnd);
//Required when Modeless form
using (DocumentLock theLock = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
PromptPointResult getPnt = ed.GetPoint(getPntPrmpt);
}

Message 5 of 15

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Assuming the modeless form is launched from an autoCAD command :

 

        [CommandMethod("Test")]
        public void Test()
        {
            Application.ShowModelessDialog(new ModelesssForm());
        }

 

In the ModelessForm class, the button  click event handler may be:

 

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide(); // this is not mandatory

            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            using (doc.LockDocument()) // this is needed from a modeless form
            {
                // set focus to AutoCAD
                Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();

                // do your stuff here
                PromptPointResult ppr = ed.GetPoint("\nPick a point: ");
                // ...
            }

            this.Show(); // this is mandatory if the form have been hidden
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 15

Anonymous
Not applicable

What library should I to import to use adsw_acadDocWnd () function?

0 Likes
Message 7 of 15

Anonymous
Not applicable

Thank you very much.

I searched th solution for a month. Now it is exactly what I need.

0 Likes
Message 8 of 15

viper_007
Advocate
Advocate
Thank you! It really helps me.
聚傲气于一身,显傲骨之精华
0 Likes
Message 9 of 15

CADbloke
Advocate
Advocate

from AutoCAD 2014 onwards it is 

Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Focus();
- - - - - - -
working on all sorts of things including www.tvCAD.tv & www.CADreplace.com
Message 10 of 15

Anonymous
Not applicable

In the solution from SENL1362 you can replace adsw_acadDocWnd() with:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Window.Handle

It can be used in AutoCAD 2010 or even earlier I think.

 

But the other solutions Utils.SetFocusToDwgView() and MainWindow.Focus() are much more straightforward.

 

0 Likes
Message 11 of 15

Anonymous
Not applicable

When i do this using Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView(); afterbutton click on modeless form if my form is not over the acad window i go straight into the editor, if my form was even partially over the acad window i have to click in the window once to get into the editor.

 

Any ideas?

0 Likes
Message 12 of 15

_gile
Consultant
Consultant

@Anonymous  a écrit :

When i do this using Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView(); afterbutton click on modeless form if my form is not over the acad window i go straight into the editor, if my form was even partially over the acad window i have to click in the window once to get into the editor.

 

Any ideas?


Did you try @CADbloke's suggestion in its upper reply?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 13 of 15

Anonymous
Not applicable

Yes, I can see his is for 2014 and later, they both actually produce the same result in 2017.  

 

Its weird if I run the command on the form then move my cursor completely outside of autocad and back again I get the crosshairs.  But for the period between the form and the boundary of the acad window it’s just a regular windows cursor.

 

I also have the mouse_leave function on my form so doubt it’s that.

 

0 Likes
Message 14 of 15

_gile
Consultant
Consultant

From Kean Walmsley blog:

"Once again there’s our important rule of thumb when it comes to implementing a modeless UI: rather than manually locking the current document, it’s safer to define a command – which will implicitly lock the current document – and call that from the UI via SendStringToExecute()."



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 15 of 15

Anonymous
Not applicable

nice one that does the trick.

thanks very much that was a head scratcher.

0 Likes