将MultipleMonitor应用程序从WinForms转换为WPF

时间:2014-10-06 07:49:42

标签: c# wpf winforms wpf-controls

我有一点问题,并试图解决它近6到8个小时,但我找不到任何匹配的答案。我是WPF的新手,所以请指出我做的任何错误。

首先,我在App.xaml.cs中有以下内容:

namespace WpfVideowand
{
  public partial class App : Application
  {
    ...
    private void Application_Startup(object sender, StartupEventArgs e)
    {
      foreach (System.Windows.Forms.Screen MyScreen in System.Windows.Forms.Screen.AllScreens)
      {
        List<string> MyStrings = Xml.GetScreens(i);
        if (MyStrings[1] == "true")
        {
          OpenWindow(MyScreen, MyStrings[0], i);
        }
        i++;
        Shelf MyShelf = new Shelf(MyScreen, i, MyStrings[0]);
        MyShelf.Show();
      }
    }
    private void OpenWindow(System.Windows.Forms.Screen myScreen, string configName, int screenNumber)
    {
      Shelf NewShelf = new Shelf(myScreen, screenNumber, configName);
    }
  }
}

在Shelf.xaml.cs中它看起来像这样:

namespace WpfVideowand
{
  public partial class Shelf : Window
  {
    [DllImport("user32.dll")]
    static extern IntPtr GetActiveWindow();
    System.Windows.Forms.Screen _Screen { get; set; }
    ...
    public Shelf(System.Windows.Forms.Screen myScreen, int screenNumber, string configName)
    {
      InitializeComponent();
      _Screen = myScreen;
      ShowOnMonitor(screenNumber);
      ...
    }
    private void ShowOnMonitor(int screenNumber)
    {
      System.Windows.Forms.Screen[] ScreenArray;
      ScreenArray = System.Windows.Forms.Screen.AllScreens;
      int XCoord = Convert.ToInt32(ScreenArray[screenNumber].Bounds.Left);
      this.Left = XCoord;
      int YCoord = Convert.ToInt32(ScreenArray[screenNumber].Bounds.Top);
      this.Top = XCoord;
      IntPtr active = GetActiveWindow();
      System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).Name = "Monitor" + screenNumber.ToString();
      System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).WindowState = WindowState.Maximized;
    }
    ...
  }
}

上述方法在Windows Forms Application中运行良好。在WPF中,我遇到了问题,即我收到错误消息,即矩形(窗口)没有Top或Left属性。

我甚至在其他方面尝试过,比如用

创建
System.Windows.Forms.Screen _screen = System.Windows.Forms.Screen.FromControl(this);

一个对象,它有.Top和.Left。但是我收到消息,我无法将Shelf-object转换为System.Windows.Forms.Control。 有人提出建议,如何让我的屏幕显示在显示器上应该是什么?

1 个答案:

答案 0 :(得分:0)

好的,我自己发现了...... 对于有兴趣在这里找到这个问题的答案的人来说,它是: 首先,看看您是否已实施正确的参考。为此,您需要System.Drawing和System.Windows.Forms。 (之后你必须声明很多东西,比如System.Windows.Controls.Button而不是Button等) 然后看到你用Startup =“Application_Startup”而不是uri启动应用程序,因为你想要启动多个表单而不只是一个表单。 之后绝对绝对不要将Windowstyle设置为XAML中的最大化(这确实花了我近4个小时。在我之间我长了56个白发)。在Code-Behind中使用它: System.Windows.Application.Current.Windows.OfType()。SingleOrDefault(window =&gt; new WindowInteropHelper(window).Handle == active).WindowState = WindowState.Maximized;