Author Topic: Location of WPF Window in AutoCAD  (Read 4824 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Location of WPF Window in AutoCAD
« on: October 28, 2012, 04:19:35 AM »
Windows 8 x64 Rus
AutoCAD 2013 SP1.1 Enu
***
When I open WPF Window in AutoCAD, I can't set location for it, because AutoCAD ignores my settings of location. Why it happens? How can I solve it problem?

Code - C#: [Select]
  1. ...
  2. using WinForms = System.Windows.Forms;
  3. using wpf = System.Windows;
  4. using acad = Autodesk.AutoCAD.ApplicationServices.Application;
  5. using AcWin = Autodesk.AutoCAD.Windows;
  6. ...
  7. const Int32 offset = 10;
  8. ...
  9. // Create new WPF window for using its as context menu
  10. wpf.Window win = new wpf.Window();
  11. wpf.Controls.StackPanel sp = new wpf.Controls.StackPanel();
  12. win.Content = sp;
  13.  
  14. wpf.Controls.MenuItem _clear = new wpf.Controls.MenuItem();
  15. _clear.Header = "Clear result";
  16. _clear.Click += mi_Click;
  17. sp.Children.Add(_clear);
  18.  
  19. wpf.Controls.MenuItem _report = new wpf.Controls.MenuItem();
  20. _report.Header = "Show Report";
  21. sp.Children.Add(_report);
  22.  
  23. win.SizeToContent = wpf.SizeToContent.WidthAndHeight;
  24. win.WindowStyle = wpf.WindowStyle.None;
  25. win.ResizeMode = wpf.ResizeMode.NoResize;
  26. win.WindowStartupLocation = wpf.WindowStartupLocation.Manual;
  27.  
  28. // Below are located the both methods for location of Window,
  29. // but they ain't working:
  30.  
  31. // 1. I have tried such method:
  32. win.Left = wpf.SystemParameters.FullPrimaryScreenWidth - 30;
  33. win.Top = wpf.SystemParameters.FullPrimaryScreenHeight - 30;
  34.  
  35. // 2. And I have tried such method:
  36. // win.Left = System.Windows.Forms.Cursor.Position.X + offset;
  37. // win.Top = System.Windows.Forms.Cursor.Position.Y + offset;
  38.  
  39. win.Deactivated += win_Deactivated;
  40. acad.ShowModelessWindow(win); // Open WPF window

Through WPF Window using I try solve this problem: when the events not happens for MenuItem.Click. Through WPF Window I solved its problem, but I got other problem - with Window's location...
« Last Edit: October 28, 2012, 04:28:33 AM by Andrey »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Location of WPF Window in AutoCAD
« Reply #1 on: October 28, 2012, 05:39:49 AM »
Problem is solved. I understand, it is not clear solution, but it's working. If my variant of solution is interesting for anybody, then you can read my code below.

I have registered event of Window.Activated, and have assigned a Window's location in this code.

Code - C#: [Select]
  1. using wpf = System.Windows;
  2. using acad = Autodesk.AutoCAD.ApplicationServices.Application;
  3. ...
  4. const Int32 offset_x = 0;
  5. const Int32 offset_y = 40;
  6. static Double _x, _y;
  7. ...
  8. public void Initialize() {
  9.         // Create new WPF window for using its as context menu
  10.         wpf.Window win = new wpf.Window();
  11.         wpf.Controls.StackPanel sp = new wpf.Controls.StackPanel();
  12.  
  13.         wpf.Controls.MenuItem _clear = new wpf.Controls.MenuItem();
  14.         _clear.Header = "Clear result";
  15.         _clear.Click += mi_Click;
  16.         sp.Children.Add(_clear);
  17.  
  18.         wpf.Controls.MenuItem _report = new wpf.Controls.MenuItem();
  19.         _report.Header = "Show Report";
  20.         sp.Children.Add(_report);
  21.  
  22.         win.Content = sp;
  23.         win.SizeToContent = wpf.SizeToContent.WidthAndHeight;
  24.         win.WindowStyle = wpf.WindowStyle.None;
  25.         win.ResizeMode = wpf.ResizeMode.NoResize;
  26.         win.WindowStartupLocation = wpf.WindowStartupLocation.Manual;
  27.                                
  28.         _x = System.Windows.Forms.Cursor.Position.X;
  29.         _y = System.Windows.Forms.Cursor.Position.Y;            
  30.  
  31.         win.Activated += win_Activated;
  32.         win.Deactivated += win_Deactivated;
  33.         acad.ShowModelessWindow(win);
  34. }
  35.  
  36. void win_Activated(object sender, EventArgs e) {
  37.     wpf.Window win = (wpf.Window)sender;
  38.     win.Left = _x - win.ActualWidth - offset_x;
  39.     win.Top = _y - win.ActualHeight - offset_y;
  40. }
  41.  
  42. void win_Deactivated(object sender, EventArgs e) {    
  43.         ((wpf.Window)sender).Close();
  44. }
  45.  
  46. void mi_Click(object sender, wpf.RoutedEventArgs e) {
  47.     history.Reset();
  48.     wpf.Window win = ((wpf.Window)((wpf.Controls.StackPanel)
  49.         ((wpf.Controls.MenuItem)sender).Parent).Parent);
  50.     win.Deactivated -= win_Deactivated;
  51.     win.Close();
  52. }
  53.  
  54. void clear_Click(object sender, EventArgs e) {
  55.     history.Reset();
  56. }
  57.  
Result screen: