.NET

.NET

Reply
Contributor
16 Posts
0 Kudos
Registered: ‎10-20-2013
Post 1 of 7
Accepted Solution

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

874 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
248 Posts
44 Kudos
Registered: ‎07-20-2011
Post 2 of 7

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
16 Posts
0 Kudos
Registered: ‎10-20-2013
Post 3 of 7

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
248 Posts
44 Kudos
Registered: ‎07-20-2011
Post 4 of 7

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*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 5 of 7

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
16 Posts
0 Kudos
Registered: ‎10-20-2013
Post 6 of 7

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
16 Posts
0 Kudos
Registered: ‎10-20-2013
Post 7 of 7

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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!