Entireweb

Free Search Engine Submission

Konga Deals

Saturday, 28 March 2015

Docking a WPF window inside AutoCAD – Part 3


In this post we’re wrapping up this mini-series on docking, which is part of a much broader story arc around a “command-line helper” tool, of course. But then we’re reaching the end of that, too, I suspect, as the app’s just about done. Hopefully it’s ready for posting to Autodesk Labs, at least.
Last time we added right-click dragging to allow our keywords window to be moved around without interrupting the active command. Now we’re taking it a step further to preview docking at one of the four corners of the drawing window, as well as to actually dock the dialog when the right mouse-button gets released, of course.
Here’s a recording of this in action:

CmdLineHelper with draggable docking
And here’s the “completed” project (we’ll see what changes need to be made once we start to get feedback from users).
One really interesting problem took some debugging: we’re storing a dictionary of docking locations and their corresponding enumeration values so we can check on each “mouse move” to see whether the cursor is near enough to one of the locations to preview it. Because this list’s contents will change with the size or position of the screen, we recreate it each time a drag operation begins. Very curiously, calling Clear() on this dictionary from CommandEnded() (we have a StopDragging() helper that gets called from there, just in case “on right mouse-button up” doesn’t get called) resulted in our “unknown command” event handler not firing! I’m used to the need to be careful about drawing modifications from CommandEnded() – these can kill your undo file, for instance – but this was a completely non-AutoCAD-centric data structure. Setting it to null rather than clearing it causes the code to work properly, but this was a really interesting (and obscure) bug that I thought it was worth mentioning. Please post a comment if you’ve experienced something similar and have an idea about what’s going on.
It took a little work to add the docking preview, itself: I used a new, transparent WPF Window – sized at exactly the drawing area – to which we add a Canvas containing a Rectangle. The Window gets resized whenever a drag operation starts (in case the outer window has changed size) and whenever we find the mouse is close enough to one of our dock locations we set the rectangle to the right size & location. I ended up choosing a standard gray – matching the colour used for the command-line docking – but that’s a simple detail to change if you need to make your own docking preview more visible.
All in all it works well: the ability to move the dialog around during a command – docking, as needed – does make the app much more usable. Hopefully you’ll also find this useful for your own “dockable” windows inside AutoCAD.

No comments:

Post a Comment