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

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

14 REPLIES 14
SOLVED
Reply
Message 1 of 15
$o-olitarY
10299 Views, 14 Replies

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

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.

 

14 REPLIES 14
Message 2 of 15
SENL1362
in reply to: $o-olitarY

:::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();
}
}
Message 3 of 15
$o-olitarY
in reply to: SENL1362

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

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

Message 4 of 15
SENL1362
in reply to: $o-olitarY

[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
in reply to: $o-olitarY

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
$o-olitarY
in reply to: SENL1362

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

Message 7 of 15
$o-olitarY
in reply to: _gile

Thank you very much.

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

Message 8 of 15
viper_007
in reply to: _gile

Thank you! It really helps me.
聚傲气于一身,显傲骨之精华
Message 9 of 15
CADbloke
in reply to: _gile

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
henk.loonstra
in reply to: $o-olitarY

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.

 

Message 11 of 15
phil4GCRJ
in reply to: _gile

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?

Message 12 of 15
_gile
in reply to: phil4GCRJ


@phil4GCRJ  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

Message 13 of 15
phil4GCRJ
in reply to: _gile

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.

 

Message 14 of 15
_gile
in reply to: phil4GCRJ

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
phil4GCRJ
in reply to: _gile

nice one that does the trick.

thanks very much that was a head scratcher.

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