A very old post but can this be done? A block in the drawing doesn't update until I move the curser from a palette to the drawing area.
The following code snippet moves the cursor and set its focus to the editor window. In this case the cursor moves to the center of the current dwg window.
Note: You need to add a project reference to System.Drawing.dll for this to work.
Document doc = Application.DocumentManager.MdiActiveDocument;
System.Drawing.Point location = doc.Window.Location;
System.Drawing.Size size = doc.Window.Size;
System.Drawing.Point pt = new System.Drawing.Point(location.X + (size.Width / 2), location.Y + (size.Height / 2));
System.Windows.Forms.Cursor.Position = pt;
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
Hope this assists you.
Art
Can I bump this post
I have a palette where if I make a change it asks for some dynamic input.
To do this the cursor needs to be in the drawing area.
Why would I be seeing this?
In the early stage of AutoCAD .NET API (at least before Acad2009, as far as I can remember), there was an issue with PaletteSet to maintain focus on the PaletteSet. So, in my earler PaletteSet code, I had to always set its "KeepFocus" property to "true". Then, when you have situation that you want to user to click a control inside the PaletteSet and then interact with Acad editor (you refer it as "dynamic input"), there is need to have code to set focus onto the editor, thus the need to the code
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
However, things have changed since.
Firstly, since Acad2013, you do not need to us "unsupported" ...Internal.. namespace for this any more. Instead, use Application.MainWindow.Focus()
Secondly, in most cases, you probably do not need to set PaletteSet.KeepFocus=true. In this case, whenever the mouse cursor moves outside the PaletteSet, the editor automatically has the focus.
However, if you ask user to first to click a control inside the PaletteSet, and then want the editor has the control for input, AND WHEN the PaletteSet is docked, you do need to call MainWindow.Focus() to transfer the focus to the editor; while if the PaletteSet is floating (undocked) and its KeepFocus=false, there is no need to call MainWindow.Focus(), but you cal always call it regardless the PaletteSet has KeepFocus being true or not, being floating or not.
Now come to exactly your question: I guess the code you quoted is not only to set focus to the editor, but also to try to set the cursor at the position where the user clicks on the PaletteSet. It only makes sense if the PaletteSet is floating and is set to invisible (otherwise, the cursor could not be set to behind the PaletteSet with focusing on editor).
My rules of thumb is:
1. Do not set KeepFocus=true, unless you have specific need for it;
2. If the PaletteSet is floating, when an editor input is initialized from the PalatteSet, set it Visible to false, and then set it back as soon as the editor input is done. In this way, the cursor would stay where is was on the PaletteSet.
For example:
public sealed class MyPaletteSet : PaletteSet { private MyPalette _palette = null; private Size _size = new Size(400, 500); public MyPaletteSet() : base("", null, new Guid("9BD53D9B-6FCB-40E6-B96F-E28747E18CFA")) { Style = PaletteSetStyles.ShowCloseButton | PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.NoTitleBar;
_palette = new MyPalette(); _palette.Parent = this; Add("My Palette", _palette); //KeepFocus = true; } }
public partial class MyPalette : UserControl { public MyPalette() { InitializeComponent(); } public MyPaletteSet Parent { set; get; } private void btnMe_Click(object sender, EventArgs e) { var hide = false; if (Parent.Dock == Autodesk.AutoCAD.Windows.DockSides.None) { hide = true; } try { if (hide) Parent.Visible = false; else CADApp.MainWindow.Focus(); GetInput(); } finally { if (hide) Parent.Visible = true; } } private void GetInput() { var ed = CADApp.DocumentManager.MdiActiveDocument.Editor; ed.GetString("\nPress any key to return..."); } }
Took me several months to get around to applying this. Sorry for not replying earlier and thank you!