.NET

Reply
Contributor
$o-olitarY
Posts: 16
Registered: ‎10-20-2013
Message 1 of 7 (720 Views)
Accepted Solution

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

720 Views, 6 Replies
12-07-2013 07:40 AM

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.

 

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
}

 

Mentor
SENL1362
Posts: 174
Registered: ‎07-20-2011
Message 2 of 7 (712 Views)

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

12-07-2013 08:17 AM 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();
}
}
Contributor
$o-olitarY
Posts: 16
Registered: ‎10-20-2013
Message 3 of 7 (702 Views)

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

12-07-2013 10:51 AM in reply to: SENL1362

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

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

Mentor
SENL1362
Posts: 174
Registered: ‎07-20-2011
Message 4 of 7 (694 Views)

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

12-07-2013 01:44 PM 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);
}

*Expert Elite*
_gile
Posts: 2,087
Registered: ‎04-29-2006
Message 5 of 7 (684 Views)

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

12-08-2013 12:50 AM 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
Contributor
$o-olitarY
Posts: 16
Registered: ‎10-20-2013
Message 6 of 7 (680 Views)

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

12-08-2013 12:59 AM in reply to: SENL1362

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

Contributor
$o-olitarY
Posts: 16
Registered: ‎10-20-2013
Message 7 of 7 (677 Views)

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

12-08-2013 01:11 AM in reply to: _gile

Thank you very much.

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

Need installation help?

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